1
+ #include < string.h>
2
+ #include < cstdio>
3
+ #include < cstdlib>
4
+ #include < cmath>
5
+
6
+ #include < iostream>
7
+ #include < string>
8
+ #include < vector>
9
+ #include < map>
10
+ #include < set>
11
+ #include < bitset>
12
+ #include < list>
13
+ #include < stack>
14
+ #include < queue>
15
+ #include < algorithm>
16
+ #include < numeric>
17
+ #include < sstream>
18
+
19
+
20
+ using namespace std ;
21
+
22
+ #define FOR ( i, L, U ) for (int i=(int )L ; i<=(int )U ; i++ )
23
+ #define FORD ( i, U, L ) for (int i=(int )U ; i>=(int )L ; i-- )
24
+ #define SQR (x ) ((x)*(x))
25
+
26
+ #define INF INT_MAX
27
+
28
+
29
+ #define READ (filename ) freopen(filename, " r" , stdin);
30
+ #define WRITE (filename ) freopen(filename, " w" , stdout);
31
+
32
+ typedef long long ll;
33
+ typedef unsigned long long ull;
34
+ typedef vector<int > vi;
35
+ typedef vector<ll> vll;
36
+ typedef vector<double > vd;
37
+ typedef vector<char > vc;
38
+ typedef vector<string> vs;
39
+ typedef vector<vector<int > > vvi;
40
+ typedef vector<vector<int > > vvc;
41
+ typedef map<int , int > mii;
42
+ typedef map<string, int > msi;
43
+ typedef map<int , string> mis;
44
+ typedef map<string, string> mss;
45
+ typedef map<string, char > msc;
46
+
47
+ #define WHITE 0
48
+ #define GRAY 1
49
+ #define BLACK 2
50
+ #define MAX_NODES 100009
51
+ vi g[MAX_NODES];
52
+ mii hash;
53
+ int lycans, vampires;
54
+ int color[MAX_NODES];
55
+ void bfs (int src){
56
+ queue<int > q;
57
+ int u,v,i;
58
+ color[src] = GRAY;
59
+ q.push (src);
60
+ lycans++;
61
+ while (!q.empty ()){
62
+ u = q.front ();
63
+ q.pop ();
64
+ for (i=0 ;i<g[u].size ();i++){
65
+ v = g[u][i];
66
+ if (color[v]==WHITE){
67
+ if (color[u]==GRAY){
68
+ vampires++;
69
+ color[v] = BLACK;
70
+ }
71
+ else {
72
+ lycans++;
73
+ color[v] = GRAY;
74
+ }
75
+ q.push (v);
76
+ }
77
+ }
78
+ }
79
+ }
80
+ int main ()
81
+ {
82
+ // READ("input.txt");
83
+ // WRITE("output.txt");
84
+ int test,e,i,st,en,index ,cs=1 ,sum;
85
+ scanf (" %d" , &test);
86
+ while (test--){
87
+ scanf (" %d" , &e);
88
+ index =0 ;
89
+ hash.clear ();
90
+ for (i=1 ;i<=e;i++){
91
+ scanf (" %d %d" , &st,&en);
92
+ if (!hash[st]){hash[st]=++index ;g[hash[st]].clear ();}
93
+ if (!hash[en]){hash[en]=++index ;g[hash[en]].clear ();}
94
+ g[hash[st]].push_back (hash[en]);
95
+ g[hash[en]].push_back (hash[st]);
96
+ }
97
+ for (i=1 ;i<=index ;i++)color[i]=WHITE;
98
+ sum =0 ;
99
+ for (i=1 ;i<=index ;i++){
100
+ if (color[i]!=WHITE)continue ;
101
+ lycans = vampires = 0 ;
102
+ bfs (i);
103
+ sum += max (lycans,vampires);
104
+ }
105
+ printf (" Case %d: %d\n " , cs++, sum);
106
+ }
107
+ return 0 ;
108
+ }
109
+
0 commit comments