Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2015
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.50 KB | None | 0 0
  1. package com.example.unionfind;
  2.  
  3. /**
  4. * Created by gnusosa on 2/5/15.
  5. */
  6. public class QuickFindUF {
  7.  
  8. private int[] id;
  9.  
  10. public QuickFindUF(int N)
  11. {
  12. id = new int[N];
  13. for (int i = 0; i < N; i++)
  14. id[i] = i;
  15. }
  16. public boolean connected(int p, int q)
  17. { return id[p] == id[q]; }
  18.  
  19. public void union(int p, int q)
  20. {
  21. int pid = id[p];
  22. int qid = id[q];
  23. for (int i = 0; i < id.length; i++)
  24. if (id[i] == pid) id[i] = qid;
  25. }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement