Advertisement
senb1

krsu 3403 (not my code)

Mar 29th, 2023
551
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.66 KB | None | 0 0
  1. #include "bits/stdc++.h"
  2. using namespace std;
  3.  
  4. #define ar array
  5. typedef long long ll;
  6.  
  7. const int N = 5e5 + 5;
  8. const int M = 20;
  9. const int B = 700;
  10. vector<int> edges[N], p, qq[N], sz[N];
  11. int t[N], res[N], d[N], tin[N], tout[N];
  12. int mn[N + N][M], b[N], v[N], dis[N];
  13.  
  14. void dfs(int u, int par = -1) {
  15.     tin[u] = p.size();
  16.     p.push_back(d[u]);
  17.     tout[u] = tin[u];
  18.     for (auto x : edges[u]) {
  19.         if (x == par)
  20.             continue;
  21.         d[x] = d[u] + 1;
  22.         dfs(x, u);
  23.         tout[u] = p.size();
  24.         p.push_back(d[u]);
  25.     }
  26. }
  27.  
  28. signed main() {
  29.     ios::sync_with_stdio(0);
  30.     cin.tie(0);
  31.  
  32.     int n, m;
  33.     cin >> n >> m;
  34.     for (int i = 1; i <= n; i++) {
  35.         cin >> t[i];
  36.         sz[t[i]].push_back(i);
  37.     }
  38.     for (int i = 1; i < n; i++) {
  39.         int a, b;
  40.         cin >> a >> b;
  41.         edges[a].push_back(b);
  42.         edges[b].push_back(a);
  43.     }
  44.     dfs(1);
  45.     for (int i = 0; i < (int)p.size(); i++) {
  46.         mn[i][0] = p[i];
  47.     }
  48.  
  49.     for (int j = 1; j < M; j++) {
  50.         for (int i = 0; i + (1 << (j - 1)) < (int)p.size(); i++) {
  51.             mn[i][j] = min(mn[i][j - 1], mn[i + (1 << (j - 1))][j - 1]);
  52.         }
  53.     }
  54.  
  55.     auto get = [&](int a, int b) {
  56.         if (tin[a] > tin[b])
  57.             swap(a, b);
  58.         if (tin[a] <= tin[b] && tin[b] <= tout[a]) {
  59.             return d[b] - d[a];
  60.         }
  61.         int l = tout[a], r = tin[b];
  62.         int lg = __lg(r - l + 1);
  63.         return d[a] + d[b] - 2 * min(mn[l][lg], mn[r - (1 << lg) + 1][lg]);
  64.     };
  65.  
  66.     int q;
  67.     cin >> q;
  68.     for (int i = 0; i < q; i++) {
  69.         cin >> v[i] >> b[i];
  70.         qq[b[i]].push_back(i);
  71.     }
  72.  
  73.     for (int i = 1; i <= m; i++) {
  74.         if ((int)sz[i].size() > B) {
  75.             queue<int> q;
  76.             memset(dis, 63, sizeof dis);
  77.             for (auto x : sz[i])
  78.                 q.push(x), dis[x] = 0;
  79.             while (!q.empty()) {
  80.                 int u = q.front();
  81.                 q.pop();
  82.                 for (auto x : edges[u]) {
  83.                     if (dis[x] > dis[u] + 1) {
  84.                         dis[x] = dis[u] + 1;
  85.                         q.push(x);
  86.                     }
  87.                 }
  88.             }
  89.  
  90.             for (auto x : qq[i]) {
  91.                 res[x] = dis[v[x]];
  92.             }
  93.         } else {
  94.             for (auto j : qq[i]) {
  95.                 res[j] = n;
  96.                 for (auto x : sz[i]) {
  97.                     res[j] = min(res[j], get(x, v[j]));
  98.                 }
  99.             }
  100.         }
  101.     }
  102.  
  103.     for (int i = 0; i < q; i++)
  104.         cout << res[i] << "\n";
  105. }
  106.  
  107. /*
  108.  
  109. 6 5
  110. 3 5 1 1 4 2
  111. 3 6
  112. 2 3
  113. 4 3
  114. 6 1
  115. 3 5
  116. 6
  117. 3 5
  118. 2 3
  119. 2 1
  120. 4 4
  121. 1 1
  122. 4 2
  123.  
  124. */
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement