-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path10505.cpp
65 lines (58 loc) · 1.32 KB
/
10505.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
#include <iostream>
#include <queue>
#include <set>
#define op(a) (((a)+1)%2)
using namespace std;
typedef pair<int, int> ii;
int n;
bool graph[201][201];
bool visited[201];
int color(int node) {
visited[node] = true;
queue<ii> q;
q.push(make_pair(node, 0));
set <int> colors[2];
bool impossible = false;
while(not q.empty()) {
ii c = q.front(); q.pop();
colors[c.second].insert(c.first);
for (int i = 0; i < n; i++) {
if (graph[c.first][i]) {
if (colors[c.second].find(i) != colors[c.second].end()) impossible = true;
if (not visited[i]) {
visited[i] = true;
q.push(make_pair(i, op(c.second)));
}
}
}
}
return (impossible) ? 0 : max(colors[0].size(), colors[1].size());
}
int colorAll() {
int m = 0;
for (int i = 0; i < n; i++)
if (not visited[i]) m += color(i);
return m;
}
int main() {
int tc; cin >> tc;
while (tc--) {
cin >> n;
for (int i = 0; i < n; i++) {
visited[i] = false;
for (int j = 0; j < n; j++) graph[i][j] = false;
}
for (int i = 0; i < n; i++) {
int ne; cin >> ne;
for (int j = 0; j < ne; j++) {
int e; cin >> e; e--;
if (e < n) {
graph[i][e] = true;
graph[e][i] = true;
}
}
}
cout << colorAll() << endl;
}
return 0;
}