-
Notifications
You must be signed in to change notification settings - Fork 0
/
효율적인 해킹.java
84 lines (57 loc) · 1.48 KB
/
효율적인 해킹.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import java.util.*;
import java.io.*;
public class Main {
static int N,M;
static boolean[] visited;
static ArrayList<Integer> A[];
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());
A = new ArrayList[N+1];
for(int i =1; i<=N; i++) {
A[i] = new ArrayList<Integer>();
}
for(int i =0; i<M; i++) {
st = new StringTokenizer(br.readLine());
int S = Integer.parseInt(st.nextToken());
int E = Integer.parseInt(st.nextToken());
A[E].add(S);
}
int answer = -1;
ArrayList<Integer> answerList = new ArrayList<>();
for(int i =1; i<=N; i++) {
int count = bfs(i);
if(answer < count) {
answerList.clear();
answerList.add(i);
answer= count;
}
else if(answer == count) {
answerList.add(i);
}
}
for(int a : answerList) {
System.out.print(a+" ");
}
}
static int bfs(int node) {
Queue<Integer> q = new LinkedList<>();
visited = new boolean[N+1];
int count = 1;
q.add(node);
visited[node] = true;
while(!q.isEmpty()) {
int now = q.poll();
for(int nownode : A[now]) {
if(visited[nownode]== false) {
visited[nownode]= true;
q.add(nownode);
count ++;
}
}
}
return count;
}
}