-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcses_1668.cpp
More file actions
66 lines (61 loc) · 1.2 KB
/
cses_1668.cpp
File metadata and controls
66 lines (61 loc) · 1.2 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
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int MAXN = 100087;
bool flag;
int p[MAXN];
int re[3] = {0, 2, 1};
vector<int> G[MAXN];
queue<int> q;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, a, b;
cin >> n >> m;
for (int i = 0; i < m; i++)
{
cin >> a >> b;
G[a].emplace_back(b);
G[b].emplace_back(a);
}
for (int i = 1; i <= n; i++)
{
if (p[i])
continue;
q.emplace(i);
p[i] = 1;
while (!q.empty())
{
if (flag)
break;
for (auto x : G[q.front()])
{
if (p[q.front()] == p[x])
{
flag = true;
break;
}
if (p[x]) continue;
q.emplace(x);
p[x] = re[p[q.front()]];
}
q.pop();
}
}
if (flag)
cout << "IMPOSSIBLE\n";
else
{
for (int i = 1; i <= n; i++)
{
cout << p[i];
if (i == n)
cout << "\n";
else
cout << " ";
}
}
return 0;
}