Advertisement
Guest User

Untitled

a guest
Jan 27th, 2015
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. void MakeSet(int);
  4. int Find(int);
  5. bool Same_Component(int, int);
  6. void Union(int, int);
  7. void UnionRank(int, int);
  8. int Components(int);
  9. int nNodes(int, int);
  10. int parent[100005];
  11. int root[100005];
  12. int rank[100005];
  13. int nodes[100005];
  14. int main() {
  15.  
  16. }
  17. void MakeSet(int n) {
  18. for(int i=0;i<n;i++) {
  19. parent[i] = i;
  20. rank[i] = 0;
  21. }
  22. }
  23. int Find(int x) {
  24. if(x != parent[x]) return parent[x] = Find(parent[x]);
  25. return x;
  26. }
  27. bool Same_Component(int x, int y) {
  28. if(Find(x) == Find(y)) return true;
  29. return false;
  30. }
  31. void Union(int x, int y) {
  32. int xroot = Find(x);
  33. int yroot = Find(y);
  34. if(rank[xroot] > rank[yroot]) parent[yroot] = xroot;
  35. else {
  36. parent[xroot] = yroot;
  37. if(rank[xroot] == rank[yroot]) rank[yroot]++;
  38. }
  39. }
  40. int Components(int n) {
  41. int nComponents = 0;
  42. for(int i=0;i<n;i++) {
  43. if(Find(i) == i) {
  44. root[nComponents] = i;
  45. nComponents++;
  46. }
  47. }
  48. return nComponents;
  49. }
  50. int nNodes(int n, int c) {
  51. for(int i=0;i<n;i++) nodes[i] = 0;
  52. for(int i=0;i<n;i++) {
  53. nodes[Find(i)]++;
  54. }
  55. return nodes[c];
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement