-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1009 - Back to Underworld.cpp
100 lines (89 loc) · 2.14 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define EPS 0.00000001
#define PI 2*acos(0.0)
#define MOD 1000000007
#define ck(XX) cout<<XX<<endl
#define set(XX,POS) XX|(1<<POS)
#define reset(XX,POS) XX&(~(1<<POS))
#define check(XX,POS) (bool)(XX&(1<<POS))
#define toggle(XX,POS) (XX^(1<<POS))
#define Fin freopen("input.txt","r",stdin)
#define Fout freopen("output.txt","w",stdout)
#define MS(ARRAY,VALUE) memset(ARRAY,VALUE,sizeof(ARRAY))
#define RT printf("Run Time : %0.3lf seconds\n", clock()/(CLOCKS_PER_SEC*1.0))
vector <int> edge[100010];
int mark[20010];
int visited[20010];
int n;
int bfs(int source)
{
int uu;
int count1=0, count2=0;
queue <int> Q;
Q.push(source);
visited[source] = 1;
count1++;
while(!Q.empty())
{
uu = Q.front();
Q.pop();
for(int i=0; i<edge[uu].size(); i++)
{
if(visited[edge[uu][i]]==0)
{
Q.push(edge[uu][i]);
if(visited[uu] == 1)
{
visited[edge[uu][i]] = 2;
count2++;
}
else if(visited[uu] == 2)
{
visited[edge[uu][i]] = 1;
count1++;
}
}
}
}
return max(count1,count2);
}
int main()
{
int tc, cn=0;
int x,y;
scanf("%d",&tc);
while(tc--)
{
//Reset
for(int i=0; i<100010; i++) edge[i].clear();
MS(mark,0);
MS(visited,0);
vector <int> node;
scanf("%d",&n);
for(int i=1 ; i<=n; i++)
{
scanf("%d%d",&x,&y);
edge[x].push_back(y);
edge[y].push_back(x);
if(mark[x]==0)
{
mark[x] = 1;
node.push_back(x);
}
if(mark[y]==0)
{
mark[y] = 1;
node.push_back(y);
}
}
int ans = 0;
for(int i=0; i< node.size(); i++)
{
if(visited[node[i]] == 0) ans = ans + bfs(node[i]);
}
printf("Case %d: %d\n",++cn,ans);
}
return 0;
}