-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_2458.java
More file actions
65 lines (51 loc) · 1.75 KB
/
BOJ_2458.java
File metadata and controls
65 lines (51 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import java.io.*;
import java.util.*;
public class BOJ_2458 {
public static void main(String[] args) throws IOException {
int answer = 0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken()); // 학생 수
int M = Integer.parseInt(st.nextToken());
List<Integer>[] childList = new ArrayList[N+1];
List<Integer>[] parentList = new ArrayList[N+1];
for (int i = 1; i <= N; i++) {
childList[i] = new ArrayList<>();
parentList[i] = new ArrayList<>();
}
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
childList[a].add(b);
parentList[b].add(a);
}
for (int i = 1; i <= N; i++) { // 1부터 N까지 N번 반복
boolean[] visited = new boolean[N+1];
visited[i] = true;
check(i, childList, visited);
check(i, parentList, visited);
int check = 0;
for (boolean b : visited) {
if (b) check++;
}
if (check == N) {
answer++;
}
}
System.out.println(answer);
}
static void check(int start, List<Integer>[] list, boolean[] visited) {
if (list[start].isEmpty()) {
return;
}
List<Integer> node = list[start];
for (Integer i : node) {
if (!visited[i]) {
visited[i] = true;
check(i, list, visited);
}
}
}
}