-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGALLERY.cpp
More file actions
67 lines (52 loc) · 947 Bytes
/
Copy pathGALLERY.cpp
File metadata and controls
67 lines (52 loc) · 947 Bytes
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
#include <bits/stdc++.h>
using namespace std;
int G, H;
vector<vector<int>> adj;
vector<bool> visit;
const int WATCHED = 0;
const int UNWATCHED = 1;
const int INSTALLED = 2;
int installed;
int dfs(int here)
{
visit[here] = true;
bool children[3] = {};
for(size_t i=0; i < adj[here].size(); i++)
{
int there = adj[here][i];
if(!visit[there])
children[dfs(there)] = true;
}
if(children[UNWATCHED])
{
installed++;
return INSTALLED;
}
if(children[INSTALLED])
return WATCHED;
return UNWATCHED;
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &G, &H);
adj = vector<vector<int>>(G + 1);
visit = vector<bool>(G+1, false);
for(int i=0; i < H; i++)
{
int u, v;
scanf("%d%d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
installed = 0;
for(int i=0; i < G; i++)
if(!visit[i] && dfs(i) == UNWATCHED)
installed++;
printf("%d\n", installed);
}
return 0;
}