Advertisement
Saleh127

UVA 10938 / Binary Lift - LCA

Feb 5th, 2022
1,257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.64 KB | None | 0 0
  1. /***
  2.  created: 2022-02-05-23.25.32
  3. ***/
  4.  
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. #define ll long long
  8. #define test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
  9. #define get_lost_idiot return 0
  10. #define nl '\n'
  11. vector<ll>g[5010];
  12. ll blift[5010][20];
  13. ll depth[5020];
  14. ll v[5020];
  15. ll lg,n;
  16.  
  17. void dfs(ll node)
  18. {
  19.     v[node]=1;
  20.     for(int d:g[node])
  21.     {
  22.         if(v[d]==0)
  23.         {
  24.             depth[d]=depth[node]+1;
  25.             blift[d][0]=node;
  26.             dfs(d);
  27.         }
  28.     }
  29. }
  30.  
  31. void binarylifting()
  32. {
  33.     for(ll j=1; j<=lg; j++)
  34.     {
  35.         for(ll i=1; i<=n; i++)
  36.         {
  37.             if(blift[i][j-1]!=-1)
  38.             {
  39.                 ll par = blift[i][j-1];
  40.  
  41.                 blift[i][j] = blift[par][j-1];
  42.             }
  43.         }
  44.     }
  45. }
  46.  
  47. ll lca(ll a,ll b)
  48. {
  49.     ll k=depth[a]-depth[b];
  50.  
  51.     for(ll j=lg-1; j>=0; j--)
  52.     {
  53.         if(k &(1<<j))
  54.         {
  55.             a=blift[a][j];
  56.         }
  57.     }
  58.  
  59.     if(a==b)
  60.     {
  61.         return a;
  62.     }
  63.  
  64.     for(ll j=lg-1; j>=0; j--)
  65.     {
  66.         if(blift[b][j]!=-1 &&  blift[a][j]!=blift[b][j])
  67.         {
  68.             a=blift[a][j];
  69.             b=blift[b][j];
  70.         }
  71.     }
  72.  
  73.     return blift[a][0];
  74. }
  75.  
  76. void clr()
  77. {
  78.      for(ll i=0;i<n+2;i++)
  79.      {
  80.           g[i].clear();
  81.           v[i]=0;
  82.           depth[i]=0;
  83.           for(ll j=0;j<20;j++)
  84.           {
  85.                blift[i][j]=-1;
  86.           }
  87.      }
  88. }
  89.  
  90. ll kth_lca(ll a,ll k)
  91. {
  92.      for(ll i=lg;i>=0;i--)
  93.      {
  94.           if(k &(1<<i))
  95.           {
  96.                a=blift[a][i];
  97.           }
  98.      }
  99.      return a;
  100. }
  101.  
  102. int main()
  103. {
  104.    ios_base::sync_with_stdio(0);
  105.    cin.tie(0);cout.tie(0);
  106.  
  107.    while(cin>>n && n)
  108.    {
  109.         clr();
  110.  
  111.         lg=log2(n)+1;
  112.  
  113.         ll q,i,j,k,l,m;
  114.  
  115.         for(i=1;i<n;i++)
  116.         {
  117.              cin>>j>>k;
  118.              g[j].push_back(k);
  119.              g[k].push_back(j);
  120.         }
  121.         dfs(1);
  122.         binarylifting();
  123.  
  124.         cin>>q;
  125.  
  126.         while(q--)
  127.         {
  128.              cin>>j>>k;
  129.  
  130.              if(depth[j]<depth[k]) swap(j,k);
  131.  
  132.              l=lca(j,k);
  133.              m=depth[j]+depth[k]-2*depth[l]+1;
  134.  
  135.              if(m%2)
  136.              {
  137.                   l=kth_lca(j,m/2);
  138.                   cout<<"The fleas meet at "<<l<<"."<<nl;
  139.              }
  140.              else
  141.              {
  142.                   m=(m/2)-1;
  143.                   l=kth_lca(j,m);
  144.  
  145.                   j=blift[l][0];
  146.  
  147.                   /// cout<<l<<" "<<j<<nl;
  148.  
  149.                   if(l>j) swap(l,j);
  150.  
  151.                   cout<<"The fleas jump forever between "<<l<<" and "<<j<<"."<<nl;
  152.              }
  153.         }
  154.    }
  155.    get_lost_idiot;
  156. }
  157.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement