-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11770.cpp
More file actions
76 lines (71 loc) · 1.61 KB
/
11770.cpp
File metadata and controls
76 lines (71 loc) · 1.61 KB
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
76
/**
* ____
* ____ ___ ____ ________ __/ __/
* / __ `__ \/ __ `/ ___/ / / / /_
* / / / / / / /_/ / / / /_/ / __/
* /_/ /_/ /_/\__,_/_/ \__,_/_/
*
* @link : https://the-redback.com
*/
#include <bits/stdc++.h>
using namespace std;
#define ppb pop_back
#define pb(x) push_back(x)
#define all(x) x.begin(), x.end()
#define mem(a, b) memset(a, b, sizeof(a))
#define inf 1000000000
#define eps 1e-9
#define NN 10010
vector<int> e[NN];
int color[NN];
int fl;
vector<int> arr;
void dfs1(int u) {
color[u] = 1;
for (int i = 0; i < e[u].size(); i++) {
int v = e[u][i];
if (!color[v]) dfs1(v);
}
arr.pb(u);
return;
}
void dfs2(int u) {
color[u] = 1;
for (int i = 0; i < e[u].size(); i++) {
int v = e[u][i];
if (!color[v]) dfs2(v);
}
return;
}
int main() {
ios_base::sync_with_stdio(false);
int t = 0, tc;
int n, m, i, j, l, k;
cin >> tc;
while (tc--) {
cin >> n >> m;
int r = 0;
for (i = 0; i < m; i++) {
cin >> k >> l;
e[k].pb(l);
}
mem(color, 0);
fl = 0;
for (i = 1; i <= n; i++) {
if (!color[i]) dfs1(i);
}
mem(color, 0);
int cnt = 0;
reverse(all(arr));
for (i = 0; i < arr.size(); i++) {
if (!color[arr[i]]) {
dfs2(arr[i]);
cnt++;
}
}
printf("Case %d: %d\n", ++t, cnt);
for (i = 0; i <= n; i++) e[i].clear();
arr.clear();
}
return 0;
}