Skip to content

Commit 1760f46

Browse files
authored
Merge pull request #412 from AlgorithmWithGod/khj20006
[20250706] BOJ / P4 / 가톨릭대학교에 워터 슬라이드를?? / 권혁준
2 parents 82437ae + 7f02cc3 commit 1760f46

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
class IOController {
6+
BufferedReader br;
7+
BufferedWriter bw;
8+
StringTokenizer st;
9+
10+
public IOController() {
11+
br = new BufferedReader(new InputStreamReader(System.in));
12+
bw = new BufferedWriter(new OutputStreamWriter(System.out));
13+
st = new StringTokenizer("");
14+
}
15+
16+
String nextLine() throws Exception {
17+
String line = br.readLine();
18+
st = new StringTokenizer(line);
19+
return line;
20+
}
21+
22+
String nextToken() throws Exception {
23+
while (!st.hasMoreTokens()) nextLine();
24+
return st.nextToken();
25+
}
26+
27+
int nextInt() throws Exception {
28+
return Integer.parseInt(nextToken());
29+
}
30+
31+
long nextLong() throws Exception {
32+
return Long.parseLong(nextToken());
33+
}
34+
35+
double nextDouble() throws Exception {
36+
return Double.parseDouble(nextToken());
37+
}
38+
39+
void close() throws Exception {
40+
bw.flush();
41+
bw.close();
42+
}
43+
44+
void write(String content) throws Exception {
45+
bw.write(content);
46+
}
47+
48+
}
49+
50+
public class Main {
51+
52+
static IOController io;
53+
54+
//
55+
56+
static int N, M;
57+
static List<Integer>[] G, R;
58+
static Stack<Integer> S;
59+
static int[] C, D;
60+
static boolean[] vis;
61+
62+
public static void main(String[] args) throws Exception {
63+
64+
io = new IOController();
65+
66+
init();
67+
solve();
68+
69+
io.close();
70+
71+
}
72+
73+
public static void init() throws Exception {
74+
75+
N = io.nextInt();
76+
M = io.nextInt();
77+
G = new List[N+1];
78+
R = new List[N+1];
79+
for(int i=1;i<=N;i++) {
80+
G[i] = new ArrayList<>();
81+
R[i] = new ArrayList<>();
82+
}
83+
84+
while(M-- > 0) {
85+
int a = io.nextInt(), b = io.nextInt();
86+
G[a].add(b);
87+
R[b].add(a);
88+
}
89+
90+
S = new Stack<>();
91+
C = new int[N+1];
92+
93+
}
94+
95+
static void solve() throws Exception {
96+
97+
vis = new boolean[N+1];
98+
for(int i=1;i<=N;i++) if(!vis[i]) {
99+
vis[i] = true;
100+
dfs1(i);
101+
}
102+
103+
vis = new boolean[N+1];
104+
int cnt = 0;
105+
while(!S.isEmpty()) {
106+
int n = S.pop();
107+
if(vis[n]) continue;
108+
vis[n] = true;
109+
dfs2(n,++cnt);
110+
}
111+
112+
D = new int[cnt+1];
113+
for(int i=1;i<=N;i++) for(int j:G[i]) {
114+
if(C[i] != C[j]) D[C[j]]++;
115+
}
116+
117+
int ans = 0;
118+
for(int i=1;i<=cnt;i++) if(D[i] == 0) ans++;
119+
io.write(ans + "\n");
120+
121+
}
122+
123+
static void dfs1(int n) {
124+
for(int i:G[n]) if(!vis[i]) {
125+
vis[i] = true;
126+
dfs1(i);
127+
}
128+
S.add(n);
129+
}
130+
131+
static void dfs2(int n, int c) {
132+
C[n] = c;
133+
for(int i:R[n]) if(!vis[i]) {
134+
vis[i] = true;
135+
dfs2(i,c);
136+
}
137+
}
138+
139+
}
140+
```

0 commit comments

Comments
 (0)