forked from fatemehkarimi/uvaSolutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuva-10505.cpp
More file actions
81 lines (64 loc) · 1.77 KB
/
uva-10505.cpp
File metadata and controls
81 lines (64 loc) · 1.77 KB
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
//uva 10505
//Montesco vs Capuleto
#include <iostream>
#include <vector>
#include <queue>
#include <set>
using namespace std;
int max_possible_people(vector < set <int> > & Graph, vector <bool> & visited, int start);
int main(void)
{
int T = 0;
cin >> T;
while(T--){
int n = 0;
cin >> n;
vector < set <int> > Graph(n);
for(int i = 0; i < n; ++i){
int nn;
cin >> nn;
for(int j = 0; j < nn; ++j){
int tmp;
cin >> tmp;
--tmp;
if(tmp >= n) continue;//invalid input
Graph[i].insert(tmp);
Graph[tmp].insert(i);
}
}
int numOfPeople = 0;
vector <bool> visited(n, false);
for(int i = 0; i < n; ++i)
if(!visited[i]){
numOfPeople += max_possible_people(Graph, visited, i);
}
cout << numOfPeople << endl;
}
return 0;
}
int max_possible_people(vector < set <int> > & Graph, vector <bool> & visited, int start)
{
bool bipartite = 0;
int n = Graph.size();
int count0 = 0, count1 = 0;
vector <bool> colored(n, false);
queue <int> Queue;
visited[start] = 1;
Queue.push(start);
++count0;//starting node has color 0
while(!Queue.empty()){
int front = Queue.front();
Queue.pop();
for(auto a : Graph[front])
if(visited[a] && colored[a] == colored[front])
bipartite = 1;
else if(!visited[a]){
visited[a] = 1;
colored[a] = !colored[front];
colored[a] == 1 ? ++count1 : ++count0;
Queue.push(a);
}
}
if(bipartite) return 0;
return max(count0, count1);
}