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
| public class boj_1325 { static List<List<Integer>> graphList; static boolean[] isCheck; static int answer = 0; static int cnt = 0; 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()); graphList = new ArrayList<>();
for(int i=0; i<=n; i++){ graphList.add(new ArrayList<>()); }
for(int i=0; i<m; i++){ st = new StringTokenizer(br.readLine()); int x = Integer.parseInt(st.nextToken()); int y = Integer.parseInt(st.nextToken()); graphList.get(y).add(x); } isCheck = new boolean[n+1]; List<Integer> hackingList = new ArrayList<>(); for(int i=1; i<=n; i++){ Arrays.fill(isCheck, false); bfs(i);
if(answer == cnt){ hackingList.add(i); }else if(answer < cnt){ answer = cnt; hackingList.clear(); hackingList.add(i); } cnt = 0;
} Collections.sort(hackingList);
for (Integer hack : hackingList) { System.out.print(hack + " "); } }
private static void dfs(int x) { isCheck[x] = true; for(int i=0; i<graphList.get(x).size(); i++){ int y = graphList.get(x).get(i); if(!isCheck[y]){ cnt++; dfs(y); } } } private static void bfs(int start){ Queue<Integer> q = new LinkedList<>(); isCheck[start] = true; q.add(start); while(!q.isEmpty()){ int x = q.poll();
for(int i=0; i<graphList.get(x).size(); i++){ int y = graphList.get(x).get(i); if(!isCheck[y]){ cnt++; isCheck[y] = true; q.add(y); } } } } }
|