-
Notifications
You must be signed in to change notification settings - Fork 12
/
1009 - Back to underworld.cpp
74 lines (69 loc) · 1.29 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
72
73
#include <bits/stdc++.h>
using namespace std;
#define vi vector <int>
#define pb push_back
vi g[20100];
bool visit[20100];
bool pre[20100];
vi ff;
vi ss;
void clea()
{
for (int i=0;i<20100;i++){
g[i].clear();
visit[i]=0;
pre[i]=0;
}
ff.clear();
ss.clear();
}
void dfs(int v,int fl)
{
if (visit[v] != 0)
return;
visit[v]=1;
int nfl = ((fl==1) ? 2 : 1);
if (fl == 1)
ff.pb(v);
else
ss.pb(v);
for (int i=0;i<g[v].size();i++){
int u = g[v][i];
dfs(u,nfl);
}
}
int main ()
{
//freopen("input.txt","r",stdin);
ios_base::sync_with_stdio(false);
int t;
cin>>t;
int cas=1;
while (t--){
cout<<"Case "<<cas++<<": ";
int n;
cin>>n;
clea();
int ma=0;
for (int i=0;i<n;i++){
int u,v;
cin>>u>>v;
g[u].pb(v);
g[v].pb(u);
pre[u]=1;
pre[v]=1;
ma=max(ma,max(u,v));
}
int co=0;
for (int i=1;i<=ma;i++){
if (pre[i]==1 && visit[i]==0){
dfs(i,1);
co+=max(ff.size(),ss.size());
ff.clear();
ss.clear();
}
}
cout<<co<<endl;
}
return 0;
}