-
Notifications
You must be signed in to change notification settings - Fork 0
/
10505.cpp
146 lines (138 loc) · 2.53 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
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
/*
10505 - Montesco vs Capuleto
*/
#include<bits/stdc++.h>
using namespace std;
typedef vector<int>vt;
vector<int>graph[205];
vector<bool>visited;
vector<int>parent;
vector<int>distence;
bool isbipartite;
int zero;
int one;
void count_val()
{
for(int i=0; i<distence.size(); i++)
{
if(distence[i]==0)
zero++;
else if(distence[i]==1)
one++;
}
}
void set_zero(int node)
{
parent.assign(node,-1);
distence.assign(node,-1);
zero=0;
one=0;
}
void bfs(int start)
{
int current,next;
unsigned int i;
queue<int>vertices;
distence[start]=0;
visited[start]=true;
vertices.push(start);
isbipartite=true;
while(!vertices.empty())
{
current = vertices.front();
vertices.pop();
for(i=0; i<graph[current].size(); i++)
{
next = graph[current][i];
if(!visited[next])
{
visited[next]=true;
parent[next]=current;
distence[next]=1-distence[current];
vertices.push(next);
}
else if(distence[next]==distence[current])
{
isbipartite=false;
// break;
}
}
}
}
int main()
{
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int tc,node,total;
while (~scanf("%d", &tc))
while(tc--)
{
total=0;
scanf("%d",&node);
// graph.assign(2*node,vt());
for(int i=0; i<node; i++)
{
graph[i].clear();
}
visited.assign(node,false);
for(int i=0; i<node; i++)
{
int vtx,enemies;
scanf("%d",&enemies);
for(int j=0; j<enemies; j++)
{
scanf("%d",&vtx);
vtx--;
if(vtx>=node)continue;
graph[i].push_back(vtx);
graph[vtx].push_back(i);
}
}
set_zero(node);
for(int x=0; x<node; x++)
{
if(visited[x]==false)
{
set_zero(node);
bfs(x);
if(isbipartite)
{
count_val();
int tmp = max(zero,one);
total+=tmp;
}
}
}
printf("%d\n",total);
visited.clear();
// set_free();
}
return 0;
}
/*
Sample Input
3
5
1 3
1 1
0
1 5
0
8
2 4 5
2 1 3
0
0
0
1 3
0
1 5
3
2 2 3
1 3
1 1
Sample Output
3
5
0
*/