Advertisement
evage

weighted quick-union

Dec 26th, 2021
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define vi vector<int>
  4. #define LEN 12
  5. #define cont continue
  6. #define bk break
  7. template <class T>
  8. void print(T a){
  9.     for (auto var : a)
  10.         cout << var <<  ' ';
  11.     cout << endl;
  12. }
  13. int main(){
  14.     vi ar(LEN);
  15.     vi sz(LEN,1);
  16.     for (int i = 0; i < LEN; ++i)
  17.         ar[i]=i;
  18.    
  19.     int a,b;
  20.     int i,j;
  21.     while(cin>>a>>b)
  22.     {
  23.         for (i = ar[a]; i != ar[i]; i = ar[i]) ;
  24.         for (j = ar[b]; j != ar[j]; j = ar[j]) ;
  25.         if (i == j) cont;
  26.         if (sz[i]<sz[j]) {ar[i]=j;sz[j]+=sz[i]}
  27.         else {ar[j]=i;sz[i]+=sz[j];}
  28.     }
  29.     print(ar);
  30. }
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement