-
Notifications
You must be signed in to change notification settings - Fork 94
/
[15]_3.java
70 lines (58 loc) · 1.74 KB
/
[15]_3.java
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
66
67
68
69
70
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
public static int n, m, max_value;
public static ArrayList<Integer>[] adj = new ArrayList[10001];
public static boolean[] visited = new boolean[10001];
public static int[] saved = new int[10001];
public static StringBuffer result = new StringBuffer();
public static void bfs(int x) {
Queue<Integer> q = new LinkedList<Integer>();
q.add(x);
visited = new boolean[10001];
visited[x] = true;
while (!q.isEmpty()) {
int now = q.poll();
for (int i = 0; i < adj[now].size(); i++) {
int y = adj[now].get(i);
if (!visited[y]) {
q.add(y);
visited[y] = true;
// 현재 노드(now)에 도달할 수 있는 노드들의 값을 증가
saved[y]++;
}
}
}
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
for (int i = 1; i <= n; i++) {
adj[i] = new ArrayList<Integer>();
}
for (int i = 0; i < m; i++) {
st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
adj[x].add(y); // (도착 노드, 출발 노드) 순으로 삽입
}
for (int i = 1; i <= n; i++) {
bfs(i);
}
for (int i = 1; i <= n; i++) {
max_value = Math.max(max_value, saved[i]);
}
for (int i = 1; i <= n; i++) {
if (saved[i] == max_value) {
result.append(i + " ");
}
}
System.out.println(result);
}
}