bipart 455 B

123456789101112131415161718192021222324252627
  1. /* Determine if a graph is bipartite or not.
  2. */
  3. BEG_G{
  4. int vc, c, color[node_t];
  5. node_t v;
  6. edge_t e;
  7. $tvtype = TV_dfs;
  8. $tvroot = fstnode($);
  9. }
  10. N{
  11. if ($tvedge == NULL)
  12. color[$] = 1;
  13. if (color[$] == 1)
  14. c = 2;
  15. else
  16. c = 1;
  17. for (e = fstedge($); e; e = nxtedge(e,$)) {
  18. v = opp(e,$);
  19. vc = color[v];
  20. if (vc == 0)
  21. color[v] = c;
  22. else if (vc != c) {
  23. printf(2, "Not bipartite\n");
  24. exit(1);
  25. }
  26. }
  27. }