• 문제 링크
1325번: 효율적인 해킹
첫째 줄에, N과 M이 들어온다. N은 10,000보다 작거나 같은 자연수, M은 100,000보다 작거나 같은 자연수이다. 둘째 줄부터 M개의 줄에 신뢰하는 관계가 A B와 같은 형식으로 들어오며, "A가 B를 신뢰한
www.acmicpc.net
• 풀이 과정
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
public class Main {
static Node[] graph;
static boolean[] visit;
static int[] cnt;
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder();
int n = read(), m = read();
graph = new Node[n + 1];
for (int i = 1; i <= n; i++) graph[i] = new Node(i);
for (int i = 0; i < m; i++) graph[read()].addEdge(read());
cnt = new int[n + 1];
for (int i = 1; i <= n; dfs(i++)) visit = new boolean[n + 1];
int max = 0;
for (int i = 1; i <= n; i++) max = Math.max(max, cnt[i]);
for (int i = 1; i <= n; i++) if (cnt[i] == max) sb.append(i).append(" ");
bw.write(sb.toString());
bw.flush();
}
private static void dfs(int src) {
visit[src] = true;
for (int tgt : graph[src].adj) {
if (!visit[tgt]) {
cnt[tgt]++;
dfs(tgt);
}
}
}
private static class Node {
int src;
List<Integer> adj;
Node(int src) {
this.src = src;
this.adj = new ArrayList<>();
}
public void addEdge(int tgt) {
adj.add(tgt);
}
}
private static int read() throws IOException {
int c, n = System.in.read() & 15;
while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);
return n;
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 3986 좋은 단어 - Data Structure / Java (0) | 2022.11.27 |
---|---|
[백준] 11899 괄호 끼워넣기 - Data Structure / Java (0) | 2022.11.26 |
[백준] 2583 영역 구하기 - Graph Theory / Java (0) | 2022.11.24 |
[백준] 1926 그림 - Graph Theory / Java (0) | 2022.11.23 |
[백준] 2468 안전 영역 - Brute Force / Java (0) | 2022.11.22 |
댓글