Advertisement
Vince14

/<> 28277

Oct 5th, 2023
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <vector>
  7. #include <set>
  8. #include <map>
  9. #include <stack>
  10. #include <queue>
  11. #include <deque>
  12. #include <unordered_map>
  13. #include <numeric>
  14. #include <iomanip>
  15. using namespace std;
  16. #define pii pair<int , int>
  17. #define ll long long
  18. #define FAST ios_base::sync_with_stdio(false); cin.tie(NULL)
  19. const long long dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
  20. const long long dl[2] = {1, -1};
  21. const long long MOD = 1000000007;
  22. const long long MAXN = 500005;
  23.  
  24. int N, Q;
  25. set<int> s[MAXN];
  26. int r[MAXN];
  27.  
  28.  
  29. int main() {
  30.     FAST;
  31.     cin >> N >> Q;
  32.     for(int x, i = 1; i <= N; i++){
  33.         r[i] = i;
  34.         cin >> x;
  35.         for(int y, j = 0; j < x; j++){
  36.             cin >> y;
  37.             s[i].insert(y);
  38.         }
  39.     }
  40.     for(int x, y, z, i = 0; i < Q; i++){
  41.         cin >> x;
  42.         if(x == 2){
  43.             cin >> y;
  44.             cout << s[r[y]].size() << "\n";
  45.         }
  46.         else{
  47.             cin >> y >> z;
  48.             if(s[r[y]].size() < s[r[z]].size()){
  49.                 s[r[z]].merge(s[r[y]]);
  50.                 int temp = r[z];
  51.                 r[z] = r[y];
  52.                 r[y] = temp;
  53.                 s[r[z]].clear();
  54.             }
  55.             else{
  56.                 s[r[y]].merge(s[r[z]]);
  57.                 s[r[z]].clear();
  58.             }
  59.         }
  60.     }
  61. }
  62.  
  63. /*
  64. 3 11
  65. 2 5 1
  66. 3 2 4 7
  67. 4 8 5 2 6
  68. 2 1
  69. 2 2
  70. 2 3
  71. 1 1 3
  72. 2 1
  73. 2 3
  74. 1 2 3
  75. 2 2
  76. 1 2 1
  77. 2 1
  78. 2 2
  79.  */
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement