orazma

f.cpp

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