Advertisement
ivnikkk

Untitled

May 18th, 2022
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.95 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. ll t = 0;
  69. vector<vector<ll>> g;
  70. vector<ll> clr;
  71.  
  72. void dfs(ll v, ll p) {
  73.     if (p == -1) {
  74.         d[v] = 0;
  75.         up[0][v] = 0;
  76.     }
  77.     else {
  78.         d[v] = d[p] + 1;
  79.     }
  80.     for (ll l = 1; l < LG; l++) {
  81.         up[l][v] = up[l - 1][up[l - 1][v]];
  82.     }
  83.     for (ll& u : g[v]) {
  84.         if (u != p) {
  85.             up[0][u] = v;
  86.             dfs(u, v);
  87.         }
  88.     }
  89. }
  90. ll get_lca(ll a, ll b) {
  91.     if (a == b)return a;
  92.     if (d[b] > d[a])swap(a, b);
  93.     for (ll i = LG - 1; i >= 0; i--) {
  94.         if (d[up[i][a]] >= d[b]) a = up[i][a];
  95.     }
  96.     if (a == b) return a;
  97.     for (ll i = LG - 1; i >= 0; i--) {
  98.         if (up[i][a] != up[i][b]) a = up[i][a], b = up[i][b];
  99.     }
  100.     return up[0][a];
  101. }
  102. ll get_dist(ll a, ll b) {
  103.     ll lca = get_lca(a, b);
  104.     return d[b] + d[a] - 2 * d[lca];
  105. }
  106. struct Node {
  107.     unordered_map<ll,multiset<ll>> colors;
  108. };
  109. struct Node_help {
  110.      vector<pll> colors;
  111. };
  112. vector<Node_help> centroid_build_vertex;
  113. vector<Node> centroid_vertex;
  114. void dfs_build_distance(ll v, ll p, vector<vector<ll>> &centroid_tree) {
  115.     centroid_build_vertex[v].colors.push_back({ clr[v],v });
  116.     for (ll& u : centroid_tree[v]) {
  117.         if (u == p)continue;
  118.         dfs_build_distance(u, v, centroid_tree);
  119.         if ((ll)centroid_build_vertex[v].colors.size() < centroid_build_vertex[u].colors.size()) {
  120.             centroid_build_vertex[v].colors.swap(centroid_build_vertex[u].colors);
  121.         }
  122.         for (pll& i : centroid_build_vertex[u].colors) {
  123.             centroid_build_vertex[v].colors.push_back(i);
  124.         }
  125.         centroid_build_vertex[u].colors.clear();
  126.     }
  127.     for (pll& i : centroid_build_vertex[v].colors) {
  128.         centroid_vertex[v].colors[i.first].insert(get_dist(i.second, v));
  129.     }
  130. }
  131. signed main() {
  132. #ifdef _DEBUG
  133.     freopen("input.txt", "r", stdin);
  134.     freopen("output.txt", "w", stdout);
  135. #endif
  136.     ios_base::sync_with_stdio(false);
  137.     cin.tie(nullptr);
  138.     cout.tie(nullptr);
  139.     ll n;
  140.     cin >> n;
  141.     g.resize(n);
  142.     clr.resize(n);
  143.     d.resize(n);
  144.     up.resize(LG, vector<ll>(n));
  145.     for (ll i = 0; i < n - 1; i++) {
  146.         ll x;
  147.         cin >> x;
  148.         g[x].push_back(i + 1);
  149.         g[i + 1].push_back(x);
  150.     }
  151.     for (ll i = 0; i < n; i++) {
  152.         cin >> clr[i];
  153.     }
  154.     dfs(0, -1);
  155.     centroid_vertex.resize(n);
  156.     centroid_build_vertex.resize(n);
  157.     Centroid_decomposition U(n, g);
  158.     U.build_tree();
  159.     dfs_build_distance(U.root, -1, U.gr);
  160.     U.gr.clear();
  161.     g.clear();
  162.     ll q;
  163.     cin >> q;
  164.     ll Node_cent_tree = U.root;
  165.     while (q--) {
  166.         ll v, c;
  167.         cin >> v >> c;
  168.         ll it = v;
  169.         ll ans = INT_MAX;
  170.         while (it != -1) {
  171.             if (!centroid_vertex[it].colors[c].empty()) {
  172.                 auto x = *centroid_vertex[it].colors[c].begin();
  173.                 ans = min(ans, x + get_dist(v, it));
  174.             }
  175.             it = U.tree[it];
  176.         }
  177.         if (ans == INT_MAX) {
  178.             cout << -1 << ' ';
  179.         }
  180.         else {
  181.             cout << ans << ' ';
  182.         }
  183.     }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement