orazma

a.cpp

Jul 20th, 2026
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define f first
  4. #define s second
  5. #define pb push_back
  6. #define mp make_pair
  7. #define ll long long
  8.  
  9. #define IOS ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
  10.  
  11. using namespace std;
  12. const int MAXN = 1e6 + 10;
  13. const int INF = 1e9 + 7;
  14. const int EPS = 1e-6;
  15. int timer = 0;
  16.  
  17. bool upper(int u, int v){
  18.     return (tin[u] <= tin[v] && tout[v] <= tout[u]);
  19. }
  20.  
  21. void dfs(int v, int pr = 0){
  22.     was[v] = 1;
  23.     tin[v] = ++timer;
  24.     up[v][0] = pr;
  25.     for (int i = 1; i <= L; i++){
  26.         up[v][i] = up[up[v][i - 1]][i - 1];
  27.     }
  28.  
  29.     for (int to : g[v]){
  30.         if (!was[to]){
  31.             dfs(to, v);
  32.         }
  33.     }
  34.     tout[v] = ++timer;
  35. }
  36.  
  37. int lca(int a, int b){
  38.     if (upper(a, b)){
  39.         return a;
  40.     }
  41.     if (upper(b, a)){
  42.         return b;
  43.     }
  44.     for (int i = L; i >= 0; i--){
  45.         if (!upper(up[a][i], b)){
  46.             a = up[a][i];
  47.         }
  48.     }
  49.    
  50.     return up[a][0];
  51. }
  52.  
  53. void solve(){
  54.     tin[0] = 0;
  55.     tout[0] = INF;
  56.  
  57.     dfs(1);
  58.  
  59.  
  60.  
  61. }
  62.  
  63. int main() {
  64.     IOS;
  65.     int t = 1;
  66.     cin >> t;
  67.     while(t--){
  68.         solve();
  69.     }
  70.     return 0;
  71. }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment