Advertisement
Josif_tepe

Untitled

Nov 2nd, 2023
911
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <vector>
  4. #include <fstream>
  5. #include <cstring>
  6. using namespace std;
  7. const int maxn = 3e5 + 10;
  8. const int logn = 24;
  9. int n;
  10. int id_node[maxn];
  11. int parent[maxn];
  12. int depth[maxn];
  13. int up_node[maxn][logn];
  14. int LCA(int A, int B) {
  15.     if(depth[A] < depth[B]) {
  16.         swap(A, B);
  17.     }
  18.     for(int i = logn - 1; i >= 0; i--) {
  19.         if(depth[A] - (1 << i) >= depth[B]) {
  20.             A = up_node[A][i];
  21.         }
  22.     }
  23.     if(A == B) {
  24.         return A;
  25.     }
  26.     for(int i = logn - 1; i >= 0; i--) {
  27.         if(up_node[A][i] != -1 and up_node[A][i] != up_node[B][i]) {
  28.             A = up_node[A][i];
  29.             B = up_node[B][i];
  30.         }
  31.     }
  32.     return up_node[A][0];
  33.                    
  34. }
  35. int main() {
  36.     ios_base::sync_with_stdio(false);
  37.     int n;
  38.     cin >> n;
  39.     depth[0] = 0;
  40.     parent[0] = 0;
  41.     vector<int> queries(n + 1, -1);
  42.     vector<pair<int, pair<int, int> > > find_LCA;
  43.     for(int i = 1; i <= n; i++) {
  44.         char c;
  45.         cin >> c;
  46.         if(c == 'a') {
  47.             int x;
  48.             cin >> x;
  49.             x = id_node[x];
  50.             id_node[i] = i;
  51.             parent[i] = x;
  52.             depth[i] = depth[parent[i]] + 1;
  53.         }
  54.         else if(c == 'b') {
  55.             int x;
  56.             cin >> x;
  57.             x = id_node[x];
  58.             id_node[i] = parent[x];
  59.             queries[i] = x;
  60.         }
  61.         else if(c == 'c') {
  62.             int x, y;
  63.             cin >> x >> y;
  64.             x = id_node[x];
  65.             y = id_node[y];
  66.             id_node[i] = x;
  67.             find_LCA.push_back(make_pair(i, make_pair(x, y)));
  68.            
  69.         }
  70.     }
  71.     for(int i = 0; i <= n; i++) {
  72.         up_node[i][0] = parent[i];
  73.         for(int j = 1; j < logn; j++) {
  74.             up_node[i][j] = up_node[up_node[i][j - 1]][j - 1];
  75.         }
  76.     }
  77.     for(int i = 0; i < find_LCA.size(); i++) {
  78.         int a = find_LCA[i].second.first;
  79.         int b = find_LCA[i].second.second;
  80.         queries[find_LCA[i].first] = depth[LCA(a, b)];
  81.     }
  82.     for(int i = 1; i <= n; i++) {
  83.         if(queries[i] != -1) {
  84.             cout << queries[i] << endl;
  85.         }
  86.     }
  87.     return 0;
  88. }
  89.  
  90. /*
  91.  12
  92.  1 2
  93.  2 4
  94.  2 5
  95.  4 7
  96.  5 8
  97.  1 3
  98.  3 6
  99.  6 9
  100.  6 10
  101.  6 11
  102.  11 12
  103.  
  104.  **/
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement