-
Notifications
You must be signed in to change notification settings - Fork 0
/
11504.cpp
96 lines (92 loc) · 1.48 KB
/
11504.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
/*
11504 - Dominos
*/
#include<bits/stdc++.h>
using namespace std;
#define MX 100005
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;
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);
}
for(int i=1; i<=node; i++)
{
if(!used[i])
{
dfs1(i);
}
}
int ans=0;
for(int i=0; i<node; i++)
{
int v = order[node-1-i];
if(!used2[v])
{
ans++;
dfs2(v);
}
}
printf("%d\n",ans);
}
return 0;
}
/*
Sample Input
1
3 2
1 2
2 3
Sample Output
1
*/