• 문제 링크
17848번: Flight Turbulence
Fly-By-Night Airlines is a low budget carrier specializing in late night/early morning flights. Things are run a little looser on this airline as was apparent a few nights ago. The flight from Ypsilanti, MI to Walla Walla, WA was completely sold out and al
www.acmicpc.net
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Main {
static int n, m, cnt;
static int[] arr;
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
n = read();
m = read() - 1;
arr = new int[n];
for (int i = 0; i < n; i++) arr[i] = read() - 1;
dfs(m);
bw.write(cnt++ > 0 ? String.valueOf(cnt) : "0");
bw.flush();
}
private static void dfs(int node) {
if (arr[node] == m) return;
cnt++;
dfs(arr[node]);
}
private static int read() throws IOException {
int c, n = System.in.read() & 15;
while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);
return n;
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 10328 Jury Jeopardy - Graph Theory / Java (0) | 2024.02.08 |
---|---|
[백준] 8073 Road Net - Graph Theory / Java (0) | 2024.02.07 |
[백준] 31217 Y - Graph Theory / Java (0) | 2024.02.05 |
[백준] 18422 Emacs - Graph Theory / Java (0) | 2024.02.04 |
[백준] 4351 Hay Points - Data Structure / Java (0) | 2024.02.03 |
댓글