-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildingTeams.cpp
More file actions
54 lines (44 loc) · 992 Bytes
/
BuildingTeams.cpp
File metadata and controls
54 lines (44 loc) · 992 Bytes
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
#include <bits/stdc++.h>
using namespace std;
int N = 1e5 + 2;
vector<vector<int>>adj(N);
vector<bool>check(N,false);
vector<int>ans(N);
vector<int>dist(N);
int oho = 0;
void BFS(int i){
queue<int>curr;
curr.push(i);
dist[i] = 0;
while(curr.front()){
i = curr.front();
curr.pop();
if(dist[i]%2){ans[i] = 2;}
else{ans[i] = 1;}
check[i] = true;
for(int j : adj[i]){
if(ans[i] == ans[j]){oho = 1;return;}
if(check[j])continue;
curr.push(j);
dist[j] = dist[i] + 1 ;
}
}
}
int main(){
int n,m;
cin >> n >> m;
while(m--){
int x,y;
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
for(int i = 1 ; i <= n && !oho; i ++){
if(check[i])continue;
BFS(i);
}
if(oho){cout <<"IMPOSSIBLE";return 0;}
for(int i = 1 ; i <= n ; i ++)
cout << ans[i] << " ";
return 0;
}