-
Notifications
You must be signed in to change notification settings - Fork 2
/
UVA 11504 - Dominos.cpp
56 lines (46 loc) · 963 Bytes
/
UVA 11504 - Dominos.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
#include <bits/stdc++.h>
#define vi vector<int>
#define vii vector<vector<int> >
using namespace std;
vii adj;
bool vis[10010];
stack<int> st;
void dfs(int u)
{
vis[u] = 1;
for(int i=0; i<adj[u].size(); i++){
int v = adj[u][i];
if(!vis[v])
dfs(v);
}
st.push(u);
}
int main()
{
int t;
scanf("%d", &t);
while(t--){
int n, m;
scanf("%d%d", &n, &m);
memset(vis, 0, sizeof vis);
adj.assign(n+1, vi(0) );
int a, b;
for(int i=0; i<m; i++){
scanf("%d%d", &a, &b);
adj[a].push_back(b);
}
for(int i=1; i<=n; i++)
if(!vis[i])
dfs(i);
memset(vis, 0, sizeof vis);
int cnt = 0;
while(!st.empty()){
int u = st.top(); st.pop();
if(!vis[u]){
dfs(u);
cnt++;
}
}
printf("%d\n", cnt);
}
}