forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathE.cpp
More file actions
69 lines (61 loc) · 1.23 KB
/
E.cpp
File metadata and controls
69 lines (61 loc) · 1.23 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
#include <bits/stdc++.h>
using namespace std;
const string COLOR = "RGB";
int n, ans[555];
vector <int> a[555];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
freopen("mayors.in", "r", stdin);
freopen("mayors.out", "w", stdout);
int m, x, y;
cin >> n >> m;
while (m--)
{
cin >> x >> y;
a[x].push_back(y);
a[y].push_back(x);
}
memset(ans, -1, sizeof ans);
ans[1] = 0;
while (1)
{
int maxCnt = 0, x = -1;
for (int i = 1; i <= n; i++)
if (ans[i] < 0)
{
int mask = 0;
for (int y : a[i])
if (ans[y] >= 0)
mask |= 1 << ans[y];
int cnt = __builtin_popcount(mask);
if (cnt > maxCnt)
{
x = i;
maxCnt = cnt;
}
}
if (x < 0)
break;
int has[3] = {0};
for (int y : a[x])
if (ans[y] >= 0)
has[ans[y]] = 1;
for (int i = 0; i < 3; i++)
if (!has[i])
{
ans[x] = i;
break;
}
if (ans[x] < 0)
{
cout << "Plan failed" << endl;
return 0;
}
}
cout << "Plan OK" << endl;
for (int i = 1; i <= n; i++)
cout << COLOR[ans[i]];
cout << endl;
}