-
Notifications
You must be signed in to change notification settings - Fork 1
/
10505 - Montesco vs Capuleto.cpp
151 lines (121 loc) · 2.26 KB
/
10505 - Montesco vs Capuleto.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*input
1
8
2 4 5
2 1 3
0
0
0
1 3
0
1 5
*/
#include <stdio.h>
#include <cmath>
#include <set>
#include <vector>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string.h>
#include <stack>
#include <map>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <queue>
#include <functional>
using namespace std;
#define r(input) freopen("input.txt","r",stdin)
#define w(output) freopen("output.txt","w",stdout)
#define FOR(i,a , b) for ( int i = 0 ; i < a ; i+=b )
#define sz() size()
#define printArr(array,n) for(int i = 0 ; i < n ; i++) cout << array[i]<<" ";
#define FOREACH(l) for (auto it = l.begin(); it != l.end(); it++)
#define count(i) __builtin_popcount(i)
#define Sort(v) sort(v.begin(),v.end())
#define cover(a,d) memset(a,d,sizeof(a))
#define VISITED 1
#define UNVISITED 0
#define INF 1000000000
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
inline int toInt(string s){int v; istringstream sin(s);sin>>v;return v;}
inline ll toll(string s){ll v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str();}
vi adj[200];
vi color;
vector<bool> used;
int n;
int Bipartite_bfs(int source)
{
queue<int> q;
color[source] = 1;
q.push(source);
bool Bipartite = true;
while ( !q.empty() )
{
int u = q.front();
q.pop();
for ( int i = 0 ; i < adj[u].size(); i++)
{
int v = adj[u][i];
if (color[v] == INF)
{
color[v] = 1- color[u];
q.push(v);
}
else if( color[v] == color[u])
Bipartite = false;
}
}
int blue = 0;
int red = 0;
FOR(i,n,1)
{
if(color[i] != INF && !used[i])
{
used[i] = 1;
if(color[i])
blue++;
else
red++;
}
}
if(!Bipartite)
return 0;
return max(blue, red);
}
int main()
{
int tc;
cin >> tc;
while(tc--)
{
cin >> n;
FOR(i,200,1)
adj[i].clear();
FOR(i,n,1)
{
int num;
cin >> num;
FOR(j,num , 1)
{
int tmp;
cin >> tmp;
if ( tmp > n)
continue;
adj[i].push_back(tmp - 1);
adj[tmp - 1].push_back(i);
}
}
color.assign(n, INF);
used.assign(n,0);
int cnt = 0;
FOR(i,n, 1)
if(color[i] == INF)
cnt += Bipartite_bfs(i);
cout << cnt<< endl;
}
}