Skip to content

Commit 3e17a52

Browse files
authored
Merge pull request #141 from AlgorithmWithGod/suyeun84
[20250218] BOJ / G4 / 키 순서 / 김수연
2 parents b8dfff2 + 8aff7ae commit 3e17a52

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
public class Main {
6+
7+
public static void main(String[] args) throws Exception {
8+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9+
StringTokenizer st = new StringTokenizer(br.readLine());
10+
11+
int N = Integer.parseInt(st.nextToken());
12+
int M = Integer.parseInt(st.nextToken());
13+
int answer = 0;
14+
ArrayList<ArrayList<Integer>> maxgraph = new ArrayList<>(N+1);
15+
ArrayList<ArrayList<Integer>> mingraph = new ArrayList<>(N+1);
16+
for (int i = 0; i < N+1; i++) {
17+
maxgraph.add(new ArrayList<>());
18+
mingraph.add(new ArrayList<>());
19+
}
20+
for (int i = 0 ; i < M; i++) {
21+
st = new StringTokenizer(br.readLine());
22+
int a = Integer.parseInt(st.nextToken());
23+
int b = Integer.parseInt(st.nextToken());
24+
mingraph.get(b).add(a);
25+
maxgraph.get(a).add(b);
26+
}
27+
for (int i = 1; i <= N; i++) {
28+
Queue<Integer> q = new LinkedList<>();
29+
q.add(i);
30+
boolean[] visited = new boolean[N+1];
31+
int mincnt = 0;
32+
visited[i] = true;
33+
while (!q.isEmpty()) {
34+
int curr = q.poll();
35+
for (int k : mingraph.get(curr)) {
36+
if (!visited[k]) {
37+
q.add(k);
38+
visited[k] = true;
39+
mincnt++;
40+
}
41+
}
42+
}
43+
int maxcnt = 0;
44+
q.add(i);
45+
Arrays.fill(visited, false);
46+
visited[i] = true;
47+
while (!q.isEmpty()) {
48+
int curr = q.poll();
49+
for (int k : maxgraph.get(curr)) {
50+
if (!visited[k]) {
51+
q.add(k);
52+
visited[k] = true;
53+
maxcnt++;
54+
}
55+
}
56+
}
57+
if (mincnt + maxcnt == N-1) answer++;
58+
}
59+
System.out.println(answer);
60+
}
61+
}
62+
```

0 commit comments

Comments
 (0)