minimario

Bicoloring

Jun 19th, 2015
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. #include <algorithm>
  2. #define push_back pb
  3. #include <string>
  4. #include <vector>
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. vector<int> g[1005];
  9.  
  10. int colour[1005];
  11. int visited[1005];
  12.  
  13. int a = -1;
  14. bool wrong = 0;
  15.  
  16. void dfs(int a) {
  17.     visited[a] = 1;
  18.     for (int i=0; i < g[a].size(); i++) {
  19.         if (visited[g[a][i]] == 0) {
  20.             colour[g[a][i]] = 1 - colour[a];
  21.             dfs(g[a][i]);
  22.         }
  23.         else {
  24.             if (colour[g[a][i]] == colour[a]) {
  25.                 wrong = 1;
  26.             }
  27.         }
  28.     }
  29. }
  30.  
  31.  
  32. int main() {
  33.     ios::sync_with_stdio(0);
  34.     cin.tie(0);
  35.     while (a != 0) {
  36.         wrong = 0;
  37.         for (int i=0; i < 1005; i++) {
  38.             visited[i] = 0;
  39.             colour[i] = 0;
  40.         }
  41.  
  42.         cin >> a;
  43.         if (a != 0) {
  44.             int b;
  45.             cin >> b;
  46.             for (int i=1; i < a+1; i++) {
  47.                 g[i].clear();
  48.             }
  49.  
  50.             for (int i=0; i<b; i++) {
  51.                 int x, y;
  52.                 cin >> x >> y;
  53.                 g[x].pb(y);
  54.                 g[y].pb(x);
  55.             }
  56.  
  57.             dfs(1);
  58.             if (wrong) {
  59.                 cout << "NOT BICOLOURABLE." << endl;
  60.             }
  61.             else {
  62.                 cout << "BICOLOURABLE." << endl;
  63.             }
  64.         }
  65.     }
  66.  
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment