Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- #define pb push_back
- #define mp make_pair
- #define f first
- #define s second
- int main()
- {
- ios_base::sync_with_stdio(false);
- cout.tie(0);
- cin.tie(0);
- int n, m;
- cin >> n >> m;
- vector<int> graph[n+5];
- for(int i = 0; i < m; i++){
- int a, b;
- cin >> a >> b;
- graph[a-1].pb(b-1);
- graph[b-1].pb(a-1);
- }
- vector<bool> visited(n, false);
- vector<int> col(n, -1);
- for(int i = 0; i < n; i++){
- if(col[i] != -1) continue;
- visited[i] = true;
- col[i] = 1;
- queue<int> q;
- q.push(i);
- while(!q.empty()){
- int c = q.front(); q.pop();
- for(int j = 0; j < (int) graph[c].size(); j++){
- int z = graph[c][j];
- if(col[c] != -1 and col[z] != -1 and col[c] == col[z]) {
- cout << "IMPOSSIBLE" << endl;
- return 0;
- }
- if(visited[z]) continue;
- visited[z] = true;
- if(col[z] == -1){
- if(col[c] == 1) col[z] = 2;
- if(col[c] == 2) col[z] = 1;
- q.push(z);
- }else {
- if(col[c] == col[z]){
- cout << "IMPOSSIBLE" << endl;
- return 0;
- }
- }
- }
- }
- }
- for(int i = 0; i < n; i++){
- cout << col[i] << " ";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement