Maruf_Hasan

LCA_SPOJ

Feb 15th, 2020
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. #define pb push_back
  6. #define ll long long
  7. #define pii pair<int,int>
  8. #define pll pair<ll,ll>
  9. #define M 100007
  10. #define INF 1e9
  11. #define INFL 1e18
  12. #define PI acos(-1)
  13. #define mp make_pair
  14. #define fast_in_out ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  15. //const int fx[]= {+1,-1,+0,+0};
  16. //const int fy[]= {+0,+0,+1,-1};
  17. //const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1}; // Kings Move
  18. //const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1}; // Kings Move
  19. //const int fx[]={-2, -2, -1, -1, 1, 1, 2, 2}; // Knights Move
  20. //const int fy[]={-1, 1, -2, 2, -2, 2, -1, 1}; // Knights Move
  21. #define MX 1010
  22. vector<int>adj[MX];
  23. int L[MX];
  24. int SparseTable[MX][22];
  25. int T[MX];
  26.  
  27. void dfs(int from,int u,int depth)
  28. {
  29. T[u]=from;
  30. L[u]=depth;
  31. for(int i=0;i<adj[u].size();i++)
  32. {
  33. int v=adj[u][i];
  34. if(v==from)
  35. continue;
  36. dfs(u,v,depth+1);
  37. }
  38.  
  39. }
  40.  
  41.  
  42. void LCA_initialize(int N)
  43. {
  44. memset(SparseTable,-1,sizeof SparseTable);
  45. int i,j;
  46. for(i=0;i<N;i++)
  47. {
  48. SparseTable[i][0]=T[i];
  49. }
  50. for(j=1;(1<<j)<N;j++)
  51. {
  52. for(i=0;i<N;i++)
  53. {
  54. if(SparseTable[i][j-1]!=-1)
  55. {
  56. SparseTable[i][j]=SparseTable[SparseTable[i][j-1]][j-1];
  57. }
  58. }
  59. }
  60. }
  61.  
  62.  
  63. int LCA_query(int N,int p,int q)
  64. {
  65. int tmp,log,i;
  66. if(L[p]<L[q])
  67. {
  68. tmp=p;
  69. p=q;
  70. q=tmp;
  71. }
  72. log=1;
  73. while(true)
  74. {
  75. int next=log+1;
  76. if((1<<next)>L[p])
  77. break;
  78. log++;
  79. }
  80.  
  81. for(i=log;i>=0;i--)
  82. {
  83. if(L[p]-(1<<i)>=L[q])
  84. {
  85. p=SparseTable[p][i];
  86. }
  87. }
  88. if(p==q)
  89. return p;
  90. for(i=log;i>=0;i--)
  91. {
  92. if(SparseTable[p][i]!=-1 && SparseTable[p][i]!=SparseTable[q][i])
  93. {
  94. p=SparseTable[p][i];
  95. q=SparseTable[q][i];
  96. }
  97. }
  98. return T[p];
  99.  
  100.  
  101. }
  102.  
  103. int main()
  104. {
  105. fast_in_out;
  106. int kase=0;
  107. int q;
  108. cin>>q;
  109. while(q--)
  110. {
  111.  
  112. int n;
  113. cin>>n;
  114. for(int i=0;i<n;i++)
  115. {
  116. int m;
  117. cin>>m;
  118. for(int j=0;j<m;j++)
  119. {
  120. int x;
  121. cin>>x;
  122. x--;
  123. adj[i].pb(x);
  124.  
  125. }
  126. }
  127.  
  128.  
  129. dfs(-1,0,0);
  130. LCA_initialize(n);
  131.  
  132. int q;
  133. cin>>q;
  134. cout<<"Case "<<kase+1<<":"<<endl;
  135. kase++;
  136. while(q--)
  137. {
  138. int x,y;
  139. cin>>x>>y;
  140. x--;
  141. y--;
  142. int ans=LCA_query(n,x,y);
  143. cout<<ans+1<<endl;
  144. }
  145. for(int i=0;i<MX;i++)
  146. {
  147. T[i]=0;
  148. L[i]=0;
  149. adj[i].clear();
  150. }
  151.  
  152.  
  153. }
  154. return 0;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment