ohwhatalovelyday

Untitled

Mar 21st, 2021
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.29 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define FASTER() ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  4. #define ff first
  5. #define ss second
  6. #define pb push_back
  7. #define all(a) a.begin(), a.end()
  8. #define dbg(x) cerr<<" "<<#x<<" "<<x<<endl
  9.  
  10. typedef long long ll;
  11.  
  12. using namespace std;
  13.  
  14. struct edge {
  15.     int to, cost;
  16. };
  17.  
  18. int l = 1, t = 0;
  19. const int maxn = 5e4 + 228, inf = (int)1e7 + 1;
  20. vector <edge> gr[maxn];
  21. vector <int> tin, tout, h;
  22. vector <vector <int>> up, costUp;
  23.  
  24. void dfs(int v, int par, int c, int dep) {
  25.     tin[v] = t++;
  26.     h[v] = dep;
  27.     up[v][0] = par;
  28.     costUp[v][0] = c;
  29.     if(v == 0) {
  30.         costUp[v][0] = inf;
  31.     }
  32.     for(int i = 1; i <= l; i++) {
  33.         up[v][i] = up[up[v][i - 1]][i - 1];
  34.         costUp[v][i] = min(costUp[v][i], costUp[up[v][i - 1]][i - 1]); //??
  35.     }
  36.     for(auto to : gr[v]) {
  37.         if(to.to != par) {
  38.             dfs(to.to, v, to.cost, dep + 1);
  39.         }
  40.     }
  41.     tout[v] = t++;
  42. }
  43.  
  44. bool is_upper(int a, int b) {
  45.     return tin[a] <= tin[b] && tout[a] >= tout[b];
  46. }
  47.  
  48. int lca(int a, int b) {
  49.     if(h[a] < h[b]) {
  50.         swap(a, b);
  51.     }
  52.     int ans = min(costUp[a][0], costUp[b][0]); //нужно ли?
  53.     for(int i = l; i >= 0; i--) {
  54.         if(h[a] > h[b] && h[up[a][i]] >= h[b]) { //поднимаем на одну высоту
  55.             a = up[a][i];
  56.             ans = min(ans, costUp[a][0]);
  57.         }
  58.     }
  59.     for(int i = l; i >= 0; i--) {
  60.         if(up[a][0] != up[b][0]) {
  61.             ans = min(ans, min(costUp[up[a][i]][0], costUp[up[b][i]][0]));
  62.             a = up[a][i];
  63.             b = up[b][i];
  64.         }
  65.     }
  66.     return ans;
  67. }
  68.  
  69. int main() {
  70.     FASTER();
  71.     int n;
  72.     cin >> n;
  73.     tin.resize(n);
  74.     tout.resize(n);
  75.     h.resize(n);
  76.     for(int i = 1; i < n; i++) {
  77.         int u, w;
  78.         cin >> u >> w;
  79.         u--;
  80.         gr[u].pb({i, w});
  81.         gr[i].pb({u, w});
  82.     }
  83.     while((1 << l) <= n) {
  84.         l++;
  85.     }
  86.     up.resize(n, vector <int>(l + 1));
  87.     costUp.resize(n, vector <int>(l + 1, inf));
  88.     dfs(0, 0, 0, 0);
  89.     int m;
  90.     cin >> m;
  91.     for(auto a : costUp[3]) cout << a << " ";
  92.     cout << endl;
  93.     for(int i = 0; i < m; i++) {
  94.         int a, b;
  95.         cin >> a >> b;
  96.         a--; b--;
  97.         cout << lca(a, b) << endl;
  98.     }
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment