Skip to content

[220323] BOJ_DFS_BFS_효율적인 해킹 #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions GnuPark/PS/BOJ/DFS_BFS/효율적인_해킹_1325.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package BOJ.DFS_BFS;

import java.io.*;
import java.util.*;

public class 효율적인_해킹_1325 {
static int n, m;
static int ans;
static boolean visit[];
static int arr[];
static ArrayList<Integer>[] graph;

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
graph = new ArrayList[n+1];
arr = new int[n+1];
for (int i = 1; i <= n; i++) {
graph[i] = new ArrayList<>();
}

for (int i = 0; i < m; i++) {
st = new StringTokenizer(br.readLine());
int s = Integer.parseInt(st.nextToken());
int e = Integer.parseInt(st.nextToken());

graph[e].add(s);
}

for (int i = 1; i <= n; i++) {
visit = new boolean[n + 1];
bfs(i);
}
StringBuffer sb = new StringBuffer();
for (int i = 1; i < arr.length; i++) {
if (arr[i] == ans){
sb.append(i + " ");
}
}
System.out.println(sb.toString());

}

public static void bfs(int index){
Queue<Integer> q = new LinkedList<Integer>();
q.offer(index);
visit[index] = true;

while (!q.isEmpty()) {
int now = q.poll();

for (int v : graph[now]){
if(!visit[v]) {
visit[v] = true;
arr[index] ++;
q.offer(v);
}
}
}
if (ans < arr[index]){
ans = arr[index];
}

}
}