-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11770.cpp
105 lines (100 loc) · 1.56 KB
/
11770.cpp
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
11770 - Lighting Away
*/
#include<bits/stdc++.h>
using namespace std;
#define MX 10000
vector<int>g[MX];
vector<int>order;
vector<bool>used(MX);
vector<bool>used2(MX);
int node,edge;
void clearerr()
{
for(int i=0; i<=node; i++)
{
g[i].clear();
used[i]=false;
used2[i]=false;
}
order.clear();
}
void dfs1(int u)
{
used[u]=true;
for(int i=0; i<g[u].size(); i++)
{
if(!used[g[u][i]])
{
dfs1(g[u][i]);
}
}
order.push_back(u);
}
void dfs2(int u)
{
used2[u]=true;
for(int i=0; i<g[u].size(); i++)
{
if(!used2[g[u][i]])
{
dfs2(g[u][i]);
}
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
///freopen("input.txt", "r", stdin);
/// freopen("output.txt", "w", stdout);
int t,tc=0;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&node,&edge);
clearerr();
for(int i=1; i<=edge; i++)
{
int x,y;
scanf("%d %d",&x,&y);
g[x].push_back(y);
}
int ans=0;
for(int i=1; i<=node; i++)
{
if(!used[i])
{
dfs1(i);
}
}
for(int i=0; i<node; i++)
{
int v = order[node-1-i];
if(!used2[v])
{
ans++;
dfs2(v);
}
}
printf("Case %d: %d\n",++tc,ans);
}
return 0;
}
/*
Sample Input
2
5 4
1 2
1 3
3 4
5 3
4 4
1 2
1 3
4 2
4 3
Sample Output
Case 1: 2
Case 2: 2
*/