-
Notifications
You must be signed in to change notification settings - Fork 1
/
1009 - Back to Underworld .cpp
63 lines (61 loc) · 1.42 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
#include<bits/stdc++.h>
using namespace std;
vector<int> adj[20001];
int vis[20001];
int bfs(int s)
{
vis[s] = 9;
int red=0,black=1;/// red=5,black=9
queue<int> Q;
Q.push(s);
while(!Q.empty()){
int u = Q.front();
Q.pop();
int sz = adj[u].size();
for(int i=0; i<sz; i++){
int v = adj[u][i];
if(vis[v]==0){
if(vis[u]==5){
vis[v] = 9;
black++;
}
else{
vis[v]=5;
red++;
}
Q.push(v);
}
}
}
return max(red, black);
}
int main()
{
int tc,mx,mn,n,a,b;
scanf("%d",&tc);
for(int t=1; t<=tc; t++){
mx=-1,mn=100000;
memset(vis,-1,sizeof vis);
scanf("%d",&n);
for(int i=1; i<=n; i++){
scanf("%d%d",&a,&b);
adj[a].push_back(b);
adj[b].push_back(a);
mx = max(mx,max(a,b));
mn = min(mn,min(a,b));
vis[a]=vis[b] = 0;
}
int ans = 0;
for(int i=mn; i<=mx; i++){
if(vis[i]==0){
ans+=bfs(i);
}
}
printf("Case %d: %d\n",t,ans);
if(t!=tc){
for(int i=mn; i<=mx; i++)
adj[i].clear();
}
}
return 0;
}