-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1034 - Hit the Light Switches
72 lines (69 loc) · 1.35 KB
/
1034 - Hit the Light Switches
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
#include <bits/stdc++.h>
using namespace std;
vector <int> ver[10005];
vector <int> top;
int check[10005];
void dfs_top_sort(int u)
{
check[u] = 1;
for(int i = 0; i < ver[u].size(); i++)
{
int v = ver[u][i];
if(check[v] == 0)
{
dfs_top_sort(v);
}
}
top.push_back(u);
}
void dfs(int u)
{
check[u] = 1;
for(int i = 0; i < ver[u].size(); i++)
{
if(check[ver[u][i]] == 0)
{
dfs(ver[u][i]);
}
}
}
int main()
{
int tes , o = 0;
cin >> tes;
while(tes--)
{
o++;
int x , y;
cin >> x >> y;
for(int i = 0; i < y; i++)
{
int a , b;
cin >> a >> b;
ver[a].push_back(b);
}
memset(check , 0 , sizeof check);
int cnt = 0;
for(int i = 1; i <= x; i++)
{
if(check[i] == 0)
{
dfs_top_sort(i);
}
}
reverse(top.begin(), top.end());
memset(check , 0, sizeof check);
int c = 0;
for(int i = 0; i < top.size(); i++)
{
if(check[top[i]] == 0)
{
c++;
dfs(top[i]);
}
}
printf("Case %d: %d\n", o , c);
for(int i = 1; i <= x; i++) ver[i].clear();
top.clear();
}
}