Guest User

c.cpp

a guest
Jan 15th, 2026
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.74 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. #define f first
  4. #define s second
  5. #define pb push_back
  6.  
  7. using namespace std;
  8. const int MAXN = 2e5 + 10;
  9. const int INF = 1e9 + 7;
  10. bool was[MAXN];
  11. vector <pair <int, int> > g[MAXN];
  12. int up[MAXN][23];
  13. int minn[MAXN][23];
  14. int maxx[MAXN][23];
  15.  
  16. int tin[MAXN], tout[MAXN], l;
  17. bool upper(int a, int b){
  18.     return (tin[a] <= tin[b] && tout[a] >= tout[b]);
  19. }
  20.  
  21. int timer = 0;
  22. void dfs(int x, int len, int p = 0){
  23.     was[x] = 1;
  24.     tin[x] = ++timer;
  25.     up[x][0] = p;
  26.     for (int i = 1; i <= l; i++){
  27.         up[x][i] = up[up[x][i - 1]][i - 1];
  28.     }
  29.     minn[x][0] = len;
  30.     for (int i = 1; i <= l; i++){
  31.         minn[x][i] = min(minn[x][i - 1],
  32.                          minn[up[x][i - 1]][i - 1]);
  33.     }
  34.     if (x == 1)
  35.         maxx[x][0] = -INF;
  36.     else
  37.         maxx[x][0] = len;
  38.  
  39.     for (int i = 1; i <= l; i++){
  40.         maxx[x][i] = max(maxx[x][i - 1],
  41.                          maxx[up[x][i - 1]][i - 1]);
  42.     }
  43.  
  44.     for (int i = 0; i < g[x].size(); i++){
  45.         int to = g[x][i].f;
  46.         int len = g[x][i].s;
  47.         if (!was[to]){
  48.             dfs(to, len, x);
  49.         }
  50.     }
  51.     tout[x] = ++timer;
  52. }
  53.  
  54. pair <int, pair <int, int> > lca(int a, int b){
  55.     int mn = INF;
  56.     int mx = -INF;
  57.     int my_lca = 0;
  58.  
  59.     if (upper(a, b))
  60.         my_lca = a;
  61.     if (upper(b, a))
  62.         my_lca = b;
  63.  
  64.  
  65.     for (int i = l; i >= 0; i--){
  66.         if (!upper(up[a][i], b)){
  67.             mn = min(mn, minn[a][i]);
  68.             mx = max(mx, maxx[a][i]);
  69.             a = up[a][i];
  70.         }
  71.         if (!upper(up[b][i], a)){
  72.             mn = min(mn, minn[b][i]);
  73.             mx = max(mx, maxx[b][i]);
  74.             b = up[b][i];
  75.         }
  76.         //cout << "! " << a << ' ' << (1 << i) << ' ' << mn << '\n';
  77.     }
  78.     if (!upper(a, b)){
  79.         mn = min(mn, minn[a][0]);
  80.         mx = max(mx, maxx[a][0]);
  81.         my_lca = up[a][0];
  82.     }
  83.     if (!upper(b, a)){
  84.         mn = min(mn, minn[b][0]);
  85.         mx = max(mx, maxx[b][0]);
  86.         my_lca = up[b][0];
  87.     }
  88.  
  89.     return {my_lca, {mn, mx}};
  90. }
  91.  
  92. void solve(){
  93.     tout[0] = INF;
  94.  
  95.     int n;
  96.     cin >> n;
  97.  
  98.     l = 0;
  99.     while ((1 << l) <= n){
  100.         l++;
  101.     }
  102.  
  103.     for (int i = 0; i < n - 1; i++){
  104.         int x, y, cost;
  105.         cin >> x >> y >> cost;
  106.         g[x].pb({y, cost});
  107.         g[y].pb({x, cost});
  108.  
  109.     }
  110.     dfs(1, INF, 0);
  111.     int q;
  112.     cin >> q;
  113.     while(q--){
  114.         int x, y;
  115.         cin >> x >> y;
  116.         pair <int, pair <int, int> > z = lca(x, y);
  117.         cout << z.s.f << ' ' << z.s.s << "\n";
  118.     }
  119.  
  120. }
  121.  
  122. int main() {
  123.  
  124.     ios::sync_with_stdio(0);
  125.     cin.tie(nullptr);
  126.     cout.tie(nullptr);
  127.  
  128.     int ttt = 1;
  129.     //cin >> ttt;
  130.  
  131.     while (ttt--){
  132.         solve();
  133.     }
  134.     return 0;
  135. }
  136.  
Advertisement
Add Comment
Please, Sign In to add comment