Advertisement
farsid

Untitled

Mar 9th, 2013
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. #define filer() freopen("in.txt","r",stdin)
  2. #define filew() freopen("out.txt","w",stdout)
  3. #include<iostream>
  4. #include<stdio.h>
  5. #include<string.h>
  6. #include<math.h>
  7. #include<algorithm>
  8. #include<queue>
  9. #include<stack>
  10. #include<string>
  11. #include<vector>
  12. #include <map>
  13. #define INF 1<<29
  14. #define PI pair<int,int>
  15. #define f first
  16. #define s second
  17. #define SET(a, x) memset((a), (x), sizeof(a))
  18. #define pb push_back
  19. #define i64 long long
  20. #define EPS (1e-9)
  21. #define MAXN 100000
  22. #define LOGMAXN 18
  23. using namespace std;
  24. typedef vector<int> VI;
  25. typedef vector<PI> vii;
  26. //i64 INF=(i64)((i64)1<<(i64)59);
  27.  
  28.  
  29. int N,Q;
  30. int T[100009];
  31. int P[MAXN][LOGMAXN],L[MAXN+10];
  32. vector<int>adj[MAXN+10];
  33. void DFS(int u)
  34. {
  35. int i,j,sz=adj[u].size(),v;
  36. for(i=0;i<sz;i++)
  37. {
  38. v=adj[u][i];
  39. if(v==P[u][0])continue;
  40. L[v]=L[u]+1;
  41. //T[v]=u;
  42. P[v][0]=u;
  43. DFS(v);
  44. }
  45. }
  46.  
  47. void process3( )
  48. {
  49. int i, j;
  50.  
  51. //we initialize every element in P with -1
  52. /*for (i = 0; i < N; i++)
  53. for (j = 0; 1 << j < N; j++)
  54. P[i][j] = -1;*/
  55.  
  56. //the first ancestor of every node i is T[i]
  57. /* for (i = 0; i < N; i++)
  58. P[i][0] = T[i];*/
  59.  
  60. //bottom up dynamic programing
  61. for (j = 1; 1 << j < N; j++)
  62. for (i = 0; i < N; i++)
  63. if (P[i][j - 1] != -1)
  64. P[i][j] = P[P[i][j - 1]][j - 1];
  65. }
  66.  
  67. int query( int p, int q)
  68. {
  69. int tmp, log, i;
  70.  
  71. //if p is situated on a higher level than q then we swap them
  72. if (L[p] < L[q])
  73. tmp = p, p = q, q = tmp;
  74.  
  75. //we compute the value of [log(L[p)]
  76. for (log = 1; 1 << log <= L[p]; log++);
  77. log--;
  78.  
  79. //we find the ancestor of node p situated on the same level
  80. //with q using the values in P
  81. for (i = log; i >= 0; i--)
  82. if (L[p] - (1 << i) >= L[q])
  83. p = P[p][i];
  84. return p;
  85.  
  86. if (p == q)
  87. return p;
  88.  
  89. //we compute LCA(p, q) using the values in P
  90. for (i = log; i >= 0; i--)
  91. if (P[p][i] != -1 && P[p][i] != P[q][i])
  92. p = P[p][i], q = P[q][i];
  93.  
  94. return P[p][0];
  95. }
  96.  
  97. int main()
  98. {
  99. //filer();
  100. int cas=0;
  101. int i,j,x,y,root;
  102. //SET(P,-1);
  103. scanf("%d%d",&N,&Q);
  104. scanf("%d",&root);
  105.  
  106. for (i = 0; i < N; i++)
  107. for (j = 0; 1 << j < N; j++)
  108. P[i][j] = -1;
  109.  
  110. L[root]=1;
  111.  
  112. for(i=1;i<N;i++)
  113. {
  114. scanf("%d%d",&x,&y);
  115. adj[x].pb(y);
  116. adj[y].pb(x);
  117. //T[y]=x;
  118. }
  119. DFS(ad);
  120. process3();
  121. while(Q--)
  122. {
  123. scanf("%d%d",&x,&y);
  124. if(L[x]==L[y])
  125. {
  126. printf("0\n");
  127. continue;
  128. }
  129. int lca=query(x,y);
  130. if(lca==y)printf("1\n");//cout<<"1"<<endl;
  131. else if(lca==x)printf("-1\n");//cout<<"-1"<<endl;
  132. else printf("0\n");//cout<<"0"<<endl;
  133. }
  134. return 0;
  135. }
  136. /*
  137. Test Case:
  138.  
  139. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement