Problem Solving/Baekjoon
[백준] 10552 DOM - Graph Theory / Java
graycode
2023. 4. 21. 08:09
• 문제 링크
10552번: DOM
The first line of input contains three integers N, M and P (1 ≤ N, M ≤ 105, 1 ≤ P ≤ M), the number of pensioners, the number of TV channels and the initial channel displayed on TV. Each of the following N lines contains two integers ai and bi (1
www.acmicpc.net
• 풀이 과정
• 풀이 코드
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
public class Main {
static int[] arr;
static boolean[] visit;
static int cnt = 0;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int p = Integer.parseInt(st.nextToken());
arr = new int[m + 1];
visit = new boolean[m + 1];
while (n-- > 0) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
if (arr[b] == 0)
arr[b] = a;
}
dfs(p);
bw.write(String.valueOf(cnt));
bw.flush();
}
private static void dfs(int idx) {
if (visit[idx]) {
cnt = -1;
return;
}
if (arr[idx] > 0) {
visit[idx] = true;
cnt++;
dfs(arr[idx]);
}
}
}