-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
77 lines (65 loc) · 2.11 KB
/
Main.java
File metadata and controls
77 lines (65 loc) · 2.11 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
66
67
68
69
70
71
72
73
74
75
76
77
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
static boolean[] visited;
static ArrayList<Integer>[] relations;
static Queue<Integer> q;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
relations = new ArrayList[N+1];
for(int i=0;i<relations.length; i++){
relations[i] = new ArrayList<Integer>();
}
for(int i=0; i<M; i++){
st = new StringTokenizer(br.readLine());
int c1 = Integer.parseInt(st.nextToken());
int c2 = Integer.parseInt(st.nextToken());
relations[c2].add(c1);
}
int maxVal = -1;
ArrayList<Integer> answer = new ArrayList<Integer>();
for(int i=1; i<N+1; i++){
int count = 0;
visited = new boolean[N+1];
Arrays.fill(visited, Boolean.FALSE);
count = bfs(i);
if(count > maxVal){
answer = new ArrayList<Integer>();
answer.add(i);
maxVal = count;
} else if (count == maxVal){
answer.add(i);
}
}
for(int num : answer){
System.out.print(num + " ");
}
br.close();
}
public static int bfs(int c){
int count = 1;
visited[c] = true;
q = new LinkedList<Integer>();
q.add(c);
while(!q.isEmpty()){
int com = q.poll();
for(int computer : relations[com]){
if(!visited[computer]){
q.add(computer);
visited[computer] = true;
count++;
}
}
}
return count;
}
}