fork download
  1. /*
  2.   Author: Nguyen Nhut Truong
  3.   From Chuyen Tien Giang High School For The Gifted
  4. */
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7.  
  8. //#define int long long
  9. #define start signed main
  10. #define fi first
  11. #define se second
  12. #define pb push_back
  13. #define eb emplace_back
  14. #define il pair<int,ll>
  15. #define ii pair<int,int>
  16. #define len(s) (int)s.size()
  17. #define all(s) (s).begin(),(s).end()
  18. #define OpenFile(Name) if (fopen(Name".inp","r")) freopen(Name".inp","r",stdin),freopen(Name".out","w",stdout);
  19.  
  20. #define MASK(x) ((1LL)<<(x))
  21. #define Bit(x,i) (((x)>>(i))&1)
  22. #define Countbit(x) __builtin_popcountll(x)
  23.  
  24. typedef long long ll;
  25. typedef long double ld;
  26.  
  27. int dx[]={1,-1,0,0,-1,1,1,-1};
  28. int dy[]={0,0,1,-1,-1,-1,1,1};
  29.  
  30. template <class C> bool Minimize(C &a, C b) { if (a>b) { a=b; return true; } return false;}
  31. template <class C> bool Maximize(C &a, C b) { if (a<b) { a=b; return true; } return false;}
  32.  
  33. inline ll add(ll a,ll b,ll c) { return (a+b)%c; };
  34. inline ll suf(ll a,ll b,ll c) { return (a-b+c)%c; };
  35. inline ll mul(ll a,ll b,ll c) { return ((a%c)*(b%c))%c; };
  36.  
  37. mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count());
  38.  
  39. ll rand(ll l,ll r){
  40. assert(l<=r);
  41. return l+rd()%(r-l+1);
  42. }
  43.  
  44. void MakeInp() {
  45. ofstream cout("Task.inp");
  46.  
  47. cout.close();
  48. }
  49.  
  50. /// Constant Limit
  51.  
  52. const int N=1e6+5,M=1e3+5,INF=1e9,lim=1e6;
  53. const int block=448,base=31;
  54.  
  55. ll Mod=1e9+7,Mod_base=1777777777,LNF=1e18;
  56.  
  57. ///________________________________________________________________________________________________________
  58.  
  59. int h[N],par[N][22];
  60. vector<int> a[N];
  61.  
  62. void DFS(int u=1,int p=-1) {
  63. par[u][0]=p;
  64. for (int i=1;i<=18;++i) par[u][i]=par[par[u][i-1]][i-1];
  65.  
  66. for (int v:a[u])
  67. if (v!=p) {
  68. h[v]=h[u]+1;
  69. DFS(v,u);
  70. }
  71. }
  72.  
  73. int lca(int u,int v) {
  74. if (h[u]<h[v]) swap(u,v);
  75. int x=h[u]-h[v];
  76.  
  77. for (int i=18;i>=0;--i)
  78. if (Bit(x,i)) u=par[u][i];
  79.  
  80. if (u==v) return u;
  81.  
  82. for (int i=18;i>=0;--i)
  83. if (par[u][i]!=par[v][i]) {
  84. u=par[u][i];
  85. v=par[v][i];
  86. }
  87.  
  88. return par[u][0];
  89. }
  90.  
  91. start() {
  92. ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
  93.  
  94. int n,q; cin>>n>>q;
  95. for (int i=1;i<n;++i) {
  96. int u,v; cin>>u>>v;
  97. a[u].eb(v);
  98. a[v].eb(u);
  99. }
  100.  
  101. DFS();
  102.  
  103. while (q--) {
  104. int u,v; cin>>u>>v;
  105. cout<<lca(u,v)<<' ';
  106. }
  107.  
  108.  
  109.  
  110.  
  111.  
  112. //cerr<<"\nBien dich thanh cong\nTime: "<<(1.0*clock()/CLOCKS_PER_SEC)<<" s\n";
  113. return 0;
  114. }
Success #stdin #stdout 0.01s 30864KB
stdin
Standard input is empty
stdout
Standard output is empty