-
Notifications
You must be signed in to change notification settings - Fork 0
/
cses_building_teams.cpp
65 lines (53 loc) · 1.32 KB
/
cses_building_teams.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
#include<bits/stdc++.h>
using namespace std;
#define deb(x) cout << #x << "=" << x << endl
//------------------------------------------------------------
vector<int> graph[100000];
vector<int> color(100000, -1);
int n, m;
bool bipartite(int node){
queue<int> q;
q.push(node);
color[node] = 1;
while (q.size()){
int u = q.front(); q.pop();
for (auto v: graph[u]){
if (color[v] == -1){
color[v] = 1- color[u];
q.push(v);
}
else if (color[v] == color[u]) return false;
}
}
return true;
}
void solve(){
cin >> n >> m;
for (int i = 0; i < m; ++i){
int a, b;
cin >> a >> b;
graph[--a].push_back(--b);
graph[b].push_back(a);
}
for (int i=0; i<n; ++i){
if (color[i] != -1) continue;
if (bipartite(i) == false){
cout << "IMPOSSIBLE";
return;
}
}
for (int i=0; i<n; ++i){
if (color[i] == 0) cout << 2 << ' ';
else cout << 1 << ' ';
}
}
// ------------------------------------------------------------
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
// cin >> t;
while (t--){
solve();
}
}