Advertisement
Vince14

/<> 2105

Oct 5th, 2023
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 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 = 200005;
  23.  
  24.  
  25. int N, M;
  26. int v[MAXN][3];
  27. int sep[MAXN][3];
  28. int t[MAXN];
  29. vector<pii> q;
  30. int p[MAXN], sz[MAXN];
  31. set<int> group[MAXN];
  32.  
  33. int Find(int x){
  34.     if(x == p[x]) return x;
  35.     return p[x] = Find(p[x]);
  36. }
  37. void Union(int a, int b, int cur){
  38.     a = Find(a);
  39.     b = Find(b);
  40.     if(a == b) return;
  41.     if(group[a].find(1) != group[a].end()){
  42.         for(auto x : group[b]){
  43.             if(t[x] == -2){
  44.                 t[x] = cur;
  45.             }
  46.         }
  47.     }
  48.     if(group[b].find(1) != group[b].end()){
  49.         for(auto x : group[a]){
  50.             if(t[x] == -2){
  51.                 t[x] = cur;
  52.             }
  53.         }
  54.     }
  55.     if(sz[a] > sz[b]) swap(a, b);
  56.     group[b].merge(group[a]);
  57.     p[a] = b;
  58.     sz[b] += sz[a];
  59. }
  60.  
  61. void init(){
  62.     for(int i = 1; i <= N; i++){
  63.         p[i] = i;
  64.         sz[i] = 1;
  65.         t[i] = -2;
  66.         group[i].insert(i);
  67.     }
  68. }
  69.  
  70.  
  71. int main() {
  72.     FAST;
  73.     cin >> N >> M;
  74.     init();
  75.     for(int x, y, i = 1; i <= N; i++){
  76.         cin >> x >> y;
  77.         v[i][1] = x;
  78.         v[i][2] = y;
  79.     }
  80.     for(int x, y, i = 0; i < M; i++){
  81.         cin >> x >> y;
  82.         q.push_back({x, y});
  83.         sep[x][y] = 1;
  84.     }
  85.     for(int i = 1; i <= N; i++){
  86.         if(sep[i][1] == 0 and v[i][1] != -1){
  87.             Union(i, v[i][1], 0);
  88.         }
  89.         if(sep[i][2] == 0 and v[i][2] != -1){
  90.             Union(i, v[i][2], 0);
  91.         }
  92.     }
  93.     for(int i = 1; i <= N; i++){
  94.         if(Find(i) == Find(1)){
  95.             t[i] = -1;
  96.         }
  97.     }
  98.     for(int i = M - 1; i >= 0; i--){
  99.         Union(q[i].first, v[q[i].first][q[i].second], i);
  100.     }
  101.     for(int i = 1; i <= N; i++){
  102.         cout << t[i] << "\n";
  103.     }
  104. }
  105.  
  106. /*
  107. 3 2
  108. -1 3
  109. 3 -1
  110. 1 2
  111. 1 2
  112. 3 1
  113.  */
  114.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement