Advertisement
Josif_tepe

Untitled

Mar 14th, 2022
705
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.58 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5. #define pb push_back
  6. #define mp make_pair
  7. #define f first
  8. #define s second
  9.  
  10. int main()
  11. {
  12.     ios_base::sync_with_stdio(false);
  13.     cout.tie(0);
  14.     cin.tie(0);
  15.  
  16.  
  17.     int n, m;
  18.     cin >> n >> m;
  19.     vector<int> graph[n+5];
  20.     for(int i = 0; i < m; i++){
  21.         int a, b;
  22.         cin >> a >> b;
  23.         graph[a-1].pb(b-1);
  24.         graph[b-1].pb(a-1);
  25.     }
  26.     vector<bool> visited(n, false);
  27.     vector<int> col(n, -1);
  28.     for(int i = 0; i < n; i++){
  29.         if(col[i] != -1) continue;
  30.         visited[i] = true;
  31.         col[i] = 1;
  32.         queue<int> q;
  33.         q.push(i);
  34.         while(!q.empty()){
  35.             int c = q.front(); q.pop();
  36.             for(int j = 0; j < (int) graph[c].size(); j++){
  37.                 int z = graph[c][j];
  38.                 if(col[c] != -1 and col[z] != -1 and col[c] == col[z]) {
  39.                     cout << "IMPOSSIBLE" << endl;
  40.                     return 0;
  41.  
  42.                 }
  43.                 if(visited[z]) continue;
  44.                 visited[z] = true;
  45.                 if(col[z] == -1){
  46.                     if(col[c] == 1) col[z] = 2;
  47.                     if(col[c] == 2) col[z] = 1;
  48.                     q.push(z);
  49.                 }else {
  50.                     if(col[c] == col[z]){
  51.                         cout << "IMPOSSIBLE" << endl;
  52.                         return 0;
  53.                     }
  54.                 }
  55.             }
  56.         }
  57.     }
  58.    
  59.     for(int i = 0; i < n; i++){
  60.         cout << col[i] << " ";
  61.     }
  62.    
  63.    
  64.     return 0;
  65.  
  66. }
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement