File size: 1,058 Bytes
c4b0eef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*
   graphs > specials > bipartite > bipartite checking
   difficulty: easy
   date: 31/Jan/2020 
   by: @brpapa
*/
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int main() {
   int V;
   while (cin >> V) {
      if (V == 0) break;
      vector<int> adjList[202];

      int E; cin >> E;
      while (E--) {
         int v1, v2; cin >> v1 >> v2;
         adjList[v1].push_back(v2);
      }

      vector<int> color(V, -1); color[0] = 0;
      queue<int> q; q.push(0);
      bool isBipartite = true;

      while (!q.empty() && isBipartite) {
         int v = q.front(); q.pop();

         for (int u : adjList[v]) {
            if (color[u] == -1) {       // cor ainda não definida (não visitado)
               color[u] = 1 - color[v]; // 1 ou 0
               q.push(u);
            } else if (color[u] == color[v]) { // conflito de cor
               isBipartite = false;
               break;
            }
         }
      }
      cout << (isBipartite? "BICOLORABLE." : "NOT BICOLORABLE.") << endl;
   }
   return 0;
}