-
Notifications
You must be signed in to change notification settings - Fork 0
/
1009 - Back to Underworld.cpp
71 lines (61 loc) · 1.53 KB
/
1009 - Back to Underworld.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
#include<bits/stdc++.h>
#define max_index 20001
using namespace std;
vector<int>graph[max_index];
int visited[max_index];
int WHITE=0,VAMPIRE = 1, LYKAN = 2;
int vam,lykan;
void BFS(int x){
queue<int>q;
q.push(x);
vam++;
visited[x] = VAMPIRE;
while(!q.empty()){
x = q.front();
q.pop();
for(int i=0; i<graph[x].size(); i++){
if(visited[graph[x][i]]==WHITE){
if(visited[x]==VAMPIRE){
lykan++;
visited[graph[x][i]] = LYKAN;
}
else{
vam++;
visited[graph[x][i]] = VAMPIRE;
}
q.push(graph[x][i]);
}
}
}
}
int main()
{
int t,i,q=1,n,m,u,v,x,y,index,sum;
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
scanf("%d",&t);
while(t--){
scanf("%d",&n);
index = 0;
memset(visited,WHITE,sizeof(visited));
for(i=0; i<max_index; i++) graph[i].clear();
map<int,int>mp;
for(i=1; i<=n; i++){
scanf("%d%d",&u,&v);
graph[u].push_back(v);
graph[v].push_back(u);
mp[u] = 1;
mp[v] = 1;
index = max(index,max(u,v));
}
sum=0;
for(i=1; i<=index; i++){
if(visited[i]==WHITE && mp[i]){
vam=lykan=0;
BFS(i);
sum += max(vam,lykan);
}
}
printf("Case %d: %d\n",q++,sum);
}
}