Advertisement
ivnikkk

Untitled

May 18th, 2022
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.94 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #define debug(l) cerr<<#l<<' '<<l<<'\n';
  3. #include "bits/stdc++.h"
  4. using namespace std;
  5. #define all(a) a.begin(), a.end()
  6. typedef int ll;
  7. typedef pair<ll, ll> pll;
  8. typedef long double ld;
  9. struct Centroid_decomposition {
  10.     vector<ll> tree, siz;
  11.     vector<bool> used;
  12.     vector<vector<ll>> gr;
  13.     ll root = -1;
  14.     Centroid_decomposition(ll n, vector<vector<ll>>& g) {
  15.         tree.resize(n);
  16.         siz.resize(n);
  17.         used.resize(n);
  18.         gr = g;
  19.     }
  20.     void sizes(ll v, ll p) {
  21.         siz[v] = 1;
  22.         for (ll& u : gr[v]) {
  23.             if (u != p && !used[u]) {
  24.                 sizes(u, v);
  25.                 siz[v] += siz[u];
  26.             }
  27.         }
  28.     }
  29.     ll centroid(ll v, ll p, ll s) {
  30.         for (ll& u : gr[v]) {
  31.             if (u != p && !used[u] && siz[u] > s / 2) {
  32.                 return centroid(u, v, s);
  33.             }
  34.         }
  35.         return v;
  36.     }
  37.     void build(ll v, ll p) {
  38.         sizes(v, -1);
  39.         used[v] = 1;
  40.         tree[v] = p;
  41.         for (ll& u : gr[v]) {
  42.             if (!used[u]) {
  43.                 build(centroid(u, -1, siz[u]), v);
  44.             }
  45.         }
  46.     }
  47.     void build_tree() {
  48.         ll v = 0;
  49.         sizes(v, -1);
  50.         build(centroid(v, -1, siz[v]), -1);
  51.         gr.clear();
  52.         gr.resize(tree.size());
  53.         used.clear();
  54.         siz.clear();
  55.         for (ll v = 0; v < (ll)tree.size(); v++) {
  56.             if (tree[v] == -1) {
  57.                 root = v;
  58.                 continue;
  59.             }
  60.             gr[v].push_back(tree[v]);
  61.             gr[tree[v]].push_back(v);
  62.         }
  63.     }
  64. };
  65. const ll LG = 17;
  66. vector<vector<ll>> up;
  67. vector<ll> d;
  68. vector<vector<ll>> g;
  69. vector<ll> clr;
  70.  
  71. void dfs(ll v, ll p) {
  72.     if (p == -1) {
  73.         d[v] = 0;
  74.         up[0][v] = 0;
  75.     }
  76.     else {
  77.         d[v] = d[p] + 1;
  78.     }
  79.     for (ll l = 1; l < LG; l++) {
  80.         up[l][v] = up[l - 1][up[l - 1][v]];
  81.     }
  82.     for (ll& u : g[v]) {
  83.         if (u != p) {
  84.             up[0][u] = v;
  85.             dfs(u, v);
  86.         }
  87.     }
  88. }
  89. ll get_lca(ll a, ll b) {
  90.     if (a == b)return a;
  91.     if (d[b] > d[a])swap(a, b);
  92.     for (ll i = LG - 1; i >= 0; i--) {
  93.         if (d[up[i][a]] >= d[b]) a = up[i][a];
  94.     }
  95.     if (a == b) return a;
  96.     for (ll i = LG - 1; i >= 0; i--) {
  97.         if (up[i][a] != up[i][b]) a = up[i][a], b = up[i][b];
  98.     }
  99.     return up[0][a];
  100. }
  101. ll get_dist(ll a, ll b) {
  102.     ll lca = get_lca(a, b);
  103.     return d[b] + d[a] - 2 * d[lca];
  104. }
  105. struct Node {
  106.     unordered_map<ll,multiset<ll>> colors;
  107. };
  108. struct Node_help {
  109.      vector<pll> colors;
  110. };
  111. vector<Node_help> centroid_build_vertex;
  112. vector<Node> centroid_vertex;
  113. void dfs_build_distance(ll v, ll p, vector<vector<ll>> &centroid_tree) {
  114.     centroid_build_vertex[v].colors.push_back({ clr[v],v });
  115.     for (ll& u : centroid_tree[v]) {
  116.         if (u == p)continue;
  117.         dfs_build_distance(u, v, centroid_tree);
  118.         if ((ll)centroid_build_vertex[v].colors.size() < centroid_build_vertex[u].colors.size()) {
  119.             centroid_build_vertex[v].colors.swap(centroid_build_vertex[u].colors);
  120.         }
  121.         for (pll& i : centroid_build_vertex[u].colors) {
  122.             centroid_build_vertex[v].colors.push_back(i);
  123.         }
  124.         centroid_build_vertex[u].colors.clear();
  125.     }
  126.     for (pll& i : centroid_build_vertex[v].colors) {
  127.         centroid_vertex[v].colors[i.first].insert(get_dist(i.second, v));
  128.     }
  129. }
  130. signed main() {
  131. #ifdef _DEBUG
  132.     freopen("input.txt", "r", stdin);
  133.     freopen("output.txt", "w", stdout);
  134. #endif
  135.     ios_base::sync_with_stdio(false);
  136.     cin.tie(nullptr);
  137.     cout.tie(nullptr);
  138.     ll n;
  139.     cin >> n;
  140.     g.resize(n);
  141.     clr.resize(n);
  142.     d.resize(n);
  143.     up.resize(LG, vector<ll>(n));
  144.     for (ll i = 0; i < n - 1; i++) {
  145.         ll x;
  146.         cin >> x;
  147.         g[x].push_back(i + 1);
  148.         g[i + 1].push_back(x);
  149.     }
  150.     for (ll i = 0; i < n; i++) {
  151.         cin >> clr[i];
  152.     }
  153.     dfs(0, -1);
  154.     centroid_vertex.resize(n);
  155.     centroid_build_vertex.resize(n);
  156.     Centroid_decomposition U(n, g);
  157.     U.build_tree();
  158.     dfs_build_distance(U.root, -1, U.gr);
  159.     U.gr.clear();
  160.     g.clear();
  161.     ll q;
  162.     cin >> q;
  163.     ll Node_cent_tree = U.root;
  164.     while (q--) {
  165.         ll v, c;
  166.         cin >> v >> c;
  167.         ll it = v;
  168.         ll ans = INT_MAX;
  169.         while (it != -1) {
  170.             if (!centroid_vertex[it].colors[c].empty()) {
  171.                 auto x = *centroid_vertex[it].colors[c].begin();
  172.                 ans = min(ans, x + get_dist(v, it));
  173.             }
  174.             it = U.tree[it];
  175.         }
  176.         if (ans == INT_MAX) {
  177.             cout << -1 << ' ';
  178.         }
  179.         else {
  180.             cout << ans << ' ';
  181.         }
  182.     }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement