-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuildingTeams.cpp
52 lines (44 loc) · 1.03 KB
/
BuildingTeams.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
#include<bits/stdc++.h>
#define ll long long
#define vi vector<int>
using namespace std;
bool dfs(const vector<vi> &adj, vi &vis, int v){
bool ok = true;
for(int i = 0; i < (int)adj[v].size(); i++){
int u = adj[v][i];
if(!vis[u]){
vis[u] = vis[v] * (-1);
ok = ok && dfs(adj, vis, u);
}
else if(vis[u] == vis[v]) return false;
}
return ok;
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n, m, a, b;
cin >> n >> m;
vector<vi> adj(n, vi());
vi vis(n, 0);
for(int i = 0; i < m; i++){
cin >> a >> b;
adj[a - 1].push_back(b - 1);
adj[b - 1].push_back(a - 1);
}
bool ok = true;
for(int i = 0; i < n; i++){
if(!vis[i]){
vis[i] = 1;
ok = ok && dfs(adj, vis, i);
}
}
if(ok){
for(int i = 0; i < n; i++){
cout << (vis[i] == -1 ? 2 : 1)<< ' ';
}
cout << '\n';
}
else{
cout << "IMPOSSIBLE\n";
}
}