Beingamanforever

Length of intersection of two paths

Jan 9th, 2025
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.72 KB | None | 0 0
  1. /**
  2.  *    author:  heWhoCooks
  3.  *    created: 2025-01-10 00:01:48
  4.  **/
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
  8. #define NeedForSpeed                  \
  9.     ios_base::sync_with_stdio(false); \
  10.     cin.tie(NULL);                    \
  11.     cout.tie(NULL);
  12. #define int long long
  13. #define all(x) (x).begin(), (x).end()
  14. #define rall(x) (x).rbegin(), (x).rend()
  15. typedef vector<int> vi;
  16. typedef vector<bool> vb;
  17. typedef vector<vi> vvi;
  18. typedef vector<pair<int, int>> vpi;
  19. typedef pair<int, int> pi;
  20. #define f first
  21. #define s second
  22. #define pb push_back
  23. #define pf push_front
  24. #define ppb pop_back
  25. #define mp make_pair
  26. #define sz(x) ((int)(x).size())
  27. #define set_bits __builtin_popcountll
  28. #define yes cout << "YES" << endl
  29. #define no cout << "NO" << endl
  30. #define endl "\n"
  31. const int mod1 = 1e9 + 7;
  32. const int mod2 = 998244353;
  33. const long long INF = 2e18;
  34. int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
  35. const int N = 2e5 + 5;
  36. const int L = 24; // binary lifting log2(n) + 1
  37. #ifndef ONLINE_JUDGE
  38. #include "heWhoCooks.cpp"
  39. #else
  40. #define debug(...)
  41. #define debugArr(...)
  42. #define __START__
  43. #define __END__
  44. #endif
  45. int n, q;
  46. int dep[N], par[N][L];
  47. vi adj[N];
  48. void dfs(int i, int p)
  49. {
  50.     dep[i] = dep[p] + 1;
  51.     par[i][0] = p;
  52.     for (int l = 1; l < L; ++l)
  53.     {
  54.         par[i][l] = par[par[i][l - 1]][l - 1];
  55.     }
  56.     for (int j : adj[i])
  57.     {
  58.         if (j != p)
  59.         {
  60.             dfs(j, i);
  61.         }
  62.     }
  63. }
  64.  
  65. int lca(int a, int b)
  66. {
  67.     if (dep[a] < dep[b])
  68.     {
  69.         swap(a, b);
  70.     }
  71.     for (int l = L - 1; l >= 0; --l)
  72.     {
  73.         if ((dep[a] - dep[b]) >> l)
  74.         {
  75.             a = par[a][l];
  76.         }
  77.     }
  78.     if (a == b)
  79.     {
  80.         return a;
  81.     }
  82.     for (int l = L - 1; l >= 0; --l)
  83.     {
  84.         if (par[a][l] != par[b][l])
  85.         {
  86.             a = par[a][l];
  87.             b = par[b][l];
  88.         }
  89.     }
  90.     return par[a][0];
  91. }
  92.  
  93. int dist(int u, int v)
  94. {
  95.     int l = lca(u, v);
  96.     return dep[u] + dep[v] - 2 * dep[l];
  97. }
  98. int find(int u, int v, int t)
  99. {
  100.     // u, v -> t
  101.     int num = (dist(u, t) + dist(v, t) - dist(u, v)) / 2 + 1;
  102.     return num;
  103. }
  104. void solve()
  105. {
  106.     cin >> n >> q;
  107.     for (int i = 2; i <= (n); i++)
  108.     {
  109.         int u;
  110.         cin >> u;
  111.         adj[u].pb(i);
  112.         adj[i].pb(u);
  113.     }
  114.     dfs(1, 0);
  115.     while (q--)
  116.     {
  117.         int a, b, c;
  118.         cin >> a >> b >> c;
  119.         cout << max({find(a, c, b), find(a, b, c), find(b, c, a)}) << endl;
  120.     }
  121.     return;
  122. }
  123.  
  124. signed main()
  125. {
  126.     __START__;
  127.     NeedForSpeed;
  128.     int t = 1;
  129.     // cin >> t;
  130.     while (t--)
  131.     {
  132.         solve();
  133.     }
  134.     __END__;
  135.     return 0;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment