Guest User

Untitled

a guest
Jan 19th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.42 KB | None | 0 0
  1. int findComponent(int u)
  2. {
  3. int p = parent[u];
  4. assert(p <= N && p >= 1);
  5. if(p == u)
  6. {
  7. assert(comp[p] == 0);
  8. return p;
  9. }
  10.  
  11. int head = findComponent(p);
  12. comp[u] = comp[u] ^ comp[p];
  13. return parent[u] = head;
  14. }
  15.  
  16. void merge(int u, int v)
  17. {
  18. int uc = findComponent(u);
  19. int vc = findComponent(v);
  20.  
  21. assert(parent[u] == uc && parent[v] == vc);
  22. parent[vc] = uc;
  23. assert(comp[vc] == 0);
  24. comp[vc] = comp[v] ^ 1 ^ comp[u];
  25. }
Add Comment
Please, Sign In to add comment