-
Notifications
You must be signed in to change notification settings - Fork 185
/
Copy path1668.cc
47 lines (44 loc) · 879 Bytes
/
1668.cc
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
// https://cses.fi/problemset/task/1668/
#include <bits/stdc++.h>
using namespace std;
using vi=vector<int>;
using vvi=vector<vi>;
using qi=queue<int>;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m, u, v;
cin >> n >> m;
vvi g(n);
for (int i = 0; i < m; i++) {
cin >> u >> v;
u--, v--;
g[u].push_back(v);
g[v].push_back(u);
}
vi c(n);
bool ok = 1;
for (int i = 0; i < n; i++)
if (!c[i]) {
qi q;
q.push(i);
c[i] = 1;
while (!q.empty()) {
u = q.front();
int z = c[u] % 2 + 1;
q.pop();
for (int v : g[u])
if (!c[v]) q.push(v), c[v] = z;
else if (z != c[v]) {
ok = 0;
break;
}
}
}
if(!ok) {
cout<<"IMPOSSIBLE\n";
return 0;
}
for (int i = 0; i < n; i++)
cout << c[i] << " \n"[i == n - 1];
}