forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiplomacy.cpp
70 lines (57 loc) · 1.28 KB
/
diplomacy.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
#include <bits/stdc++.h>
using namespace std;
bool same(vector<int>& color) {
vector<int> v(2,0);
for(auto& i : color) {
v[i]++;
}
return v[0] == 0 || v[1] == 0;
}
void flip(int toggle, vector<int>& color, vector<vector<int>>& adj) {
vector<bool> vis(color.size(),false);
queue<int> q;
q.push(toggle);
while(!q.empty()) {
int curr = q.front();
q.pop();
if(vis[curr]) {
continue;
}
vis[curr] = true;
for(auto next : adj[curr]) {
if(color[curr] == color[next]) {
q.push(next);
}
}
color[curr] ^= 1;
}
}
int solve(int toggle, vector<int> color, vector<vector<int>>& adj) {
int ct = 0;
while(!same(color)) {
ct++;
flip(toggle, color, adj);
}
return ct;
}
int main() {
int n, m;
cin >> n >> m;
vector<int> color(n);
for(auto& i : color) {
cin >> i;
}
vector<vector<int>> adj(n);
for(int i = 0; i < m; i++) {
int n1, n2;
cin >> n1 >> n2;
n1--; n2--;
adj[n1].push_back(n2);
adj[n2].push_back(n1);
}
int best = n+1;
for(int i = 0; i < n; i++) {
best = min(best,solve(i, color, adj));
}
cout << best << endl;
}