BipartiteTest.cpp 415 B

1234567891011121314151617
  1. //O(m);
  2. //check if the connected component of u is a bipartite graph.
  3. //color : 1 or 2. 0 for uncolored.
  4. // initially : memset(color, 0, sizeof(color));
  5. int color[maxn];
  6. VI G[maxn];
  7. bool bipartite(int u) {
  8. for (int i = 0; i < G[u].size(); i++) {
  9. int v = G[u][i];
  10. if (color[v] == color[u]) return false;
  11. if (!color[v]) {
  12. color[v] = 3 - color[u];
  13. if (!bipartite(v)) return false;
  14. }
  15. }
  16. return true;
  17. }