Advertisement
apl-mhd

disjointSet

Jul 8th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #include<cstdio>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <set>
  6. #include <functional>
  7. #include <queue>
  8. #include <utility>
  9.  
  10. #define P printf("\n");
  11.  
  12.  
  13. using namespace std;
  14.  
  15. typedef pair<int, int>PAIR;
  16.  
  17.     vector<int>dist(10, 9999);
  18.     int visited[100]={0};
  19.    
  20.     int par[7]={0,1,2,3,4,5,6};
  21.    
  22.  
  23.     int find(int r){
  24.        
  25.         if(par[r]==r)
  26.             return r;
  27.         return find(par[r]);
  28.        
  29.         }
  30.        
  31.     void unioN(int a, int b){
  32.        
  33.         if(find(a)==find(b))
  34.             cout<<"already friend\n";
  35.        
  36.         else if(find(a)==a&&find(b)!=b)
  37.                 par[a]=b;
  38.         else if(find(a)!=a&&find(b)==b)
  39.        
  40.                 par[b]=a;
  41.         else
  42.             par[b]=a;
  43.        
  44.         }
  45.  
  46.  
  47. int main(int argc, char **argv)
  48. {
  49.         par[1]=0;
  50.         par[2]=0;
  51.         par[3]=0;
  52.  
  53.         //unioN(1,2);
  54.         //unioN(3,0);
  55.         unioN(3,4);
  56.         //par[4]=3;
  57.         //par[3]=0;
  58.         unioN(6,5);
  59.        
  60.         cout<<find(5)<<"\n";
  61.        
  62.  
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement