Advertisement
evage

quick union

Dec 26th, 2021
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.54 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.     for (int i = 0; i < LEN; ++i)
  16.         ar[i]=i;
  17.     int a,b;
  18.     int i,j;
  19.     while(cin>>a>>b)
  20.     {
  21.         for (i = ar[a]; i != ar[i]; i = ar[i]) ;
  22.         for (j = ar[b]; j != ar[j]; j = ar[j]) ;
  23.         if (i == j) cont;
  24.         ar[i]=j;
  25.  
  26.     }
  27.     print(ar);
  28. }
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement