Advertisement
Romanchenko

problem426

Nov 7th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. vector<int> rng;
  6. vector<int> p;
  7.  
  8. void unite(int a, int b)
  9. {
  10.     p[a] = b;
  11.     rng[a] = 1;
  12. }
  13.  
  14. pair<int, int>  get(int v)
  15. {
  16.     if (p[v] == v)
  17.         return {v, rng[v]};
  18.     pair<int, int> x =  get(p[v]);
  19.     p[v] = x.first;
  20.     rng[v] += x.second;
  21.     return {p[v], rng[v]};
  22. }
  23.  
  24.  
  25. int main()
  26. {
  27.     ios_base::sync_with_stdio(false);
  28.     cin.tie(0);
  29.     //freopen("input.txt", "r", stdin);
  30.     int n, m;
  31.     cin >> n >> m;
  32.     rng = vector<int> (n, 0);
  33.     p = vector<int> (n, 0);
  34.     for (int i = 0; i < n; i++)
  35.         p[i] = i;
  36.     for (int i = 0; i < m; i++)
  37.     {
  38.         int t;
  39.         cin >> t;
  40.         if (t == 1)
  41.         {
  42.             int a, b;
  43.             cin >> a >> b;
  44.             a--, b--;
  45.             unite(a, b);
  46.         }
  47.         else
  48.         {
  49.             int c;
  50.             cin >> c;
  51.             cout << get(c - 1).second << endl;
  52.         }
  53.     }
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement