记录编号 489075 评测结果 AAAAAAAAAA
题目名称 [ZJOI 2007]捉迷藏 最终得分 100
用户昵称 GravatarCSU_Turkey 是否通过 通过
代码语言 C++ 运行时间 6.868 s
提交时间 2018-02-27 10:51:42 内存使用 11.38 MiB
显示代码纯文本
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int inf=1e5+10;
  4. int n,now_cnt,q,tot,w[inf],fi[inf],nxt[inf<<1],to[inf<<1];
  5. void link(int x,int y){
  6. to[++tot]=y;nxt[tot]=fi[x];fi[x]=tot;
  7. }
  8. int top[inf],Fa[inf],siz[inf],son[inf],dep[inf];
  9. void dfs1(int x){
  10. siz[x]=1;
  11. for(int i=fi[x];i;i=nxt[i]){
  12. if(to[i]==Fa[x])continue;
  13. Fa[to[i]]=x;
  14. dep[to[i]]=dep[x]+1;
  15. dfs1(to[i]);
  16. siz[x]+=siz[to[i]];
  17. if(siz[to[i]]>siz[son[x]])son[x]=to[i];
  18. }
  19. }
  20. void dfs2(int x,int y){
  21. top[x]=y;
  22. if(son[x])dfs2(son[x],y);
  23. for(int i=fi[x];i;i=nxt[i]){
  24. if(top[to[i]])continue;
  25. dfs2(to[i],to[i]);
  26. }
  27. }
  28. int get_dis(int x,int y){
  29. int tmp=dep[x]+dep[y];
  30. while(top[x]!=top[y]){
  31. if(dep[top[x]]<dep[top[y]])swap(x,y);
  32. x=Fa[top[x]];
  33. }
  34. return tmp-min(dep[x],dep[y])*2;
  35. }
  36. struct ghb_heap{
  37. priority_queue<int>INS,DEL;
  38. void pop(){
  39. while(!DEL.empty() && INS.top()==DEL.top())
  40. INS.pop(),DEL.pop();
  41. INS.pop();
  42. }
  43. int top(){
  44. while(!DEL.empty() && INS.top()==DEL.top())
  45. INS.pop(),DEL.pop();
  46. return INS.top();
  47. }
  48. void insert(int x){
  49. INS.push(x);
  50. }
  51. void del(int x){
  52. DEL.push(x);
  53. }
  54. int size(){
  55. return INS.size()-DEL.size();
  56. }
  57. int longest(){
  58. while(!DEL.empty() && INS.top()==DEL.top())
  59. INS.pop(),DEL.pop();
  60. int u=INS.top();
  61. INS.pop();
  62. while(!DEL.empty() && INS.top()==DEL.top())
  63. INS.pop(),DEL.pop();
  64. int v=INS.top();
  65. INS.push(u);
  66. return u+v;
  67. }
  68. }A[inf],B[inf],C;
  69. int vis[inf],fa[inf];
  70. int id,Min_size;
  71. void getsize(int x,int f){
  72. siz[x]=1;
  73. for(int i=fi[x];i;i=nxt[i]){
  74. if(vis[to[i]] || to[i]==f)continue;
  75. getsize(to[i],x);
  76. siz[x]+=siz[to[i]];
  77. }
  78. }
  79. void getrt(int x,int f,int y){
  80. int tmp=-0x3fffffff;
  81. for(int i=fi[x];i;i=nxt[i]){
  82. if(vis[to[i]] || to[i]==f)continue;
  83. getrt(to[i],x,y);
  84. tmp=max(tmp,siz[to[i]]);
  85. }
  86. tmp=max(tmp,y-siz[x]);
  87. if(tmp<Min_size){
  88. Min_size=tmp;
  89. id=x;
  90. }
  91. }
  92. void dfs(int x,int f,int y){
  93. A[y].insert(get_dis(x,fa[y]));
  94. for(int i=fi[x];i;i=nxt[i]){
  95. if(vis[to[i]] || to[i]==f)continue;
  96. dfs(to[i],x,y);
  97. }
  98. }
  99. void work(int x){
  100. B[x].insert(0);
  101. if(!fa[x])return ;
  102. dfs(x,0,x);
  103. B[fa[x]].insert(A[x].top());
  104. }
  105. void Div(int x,int f){
  106. getsize(x,0);
  107. Min_size=0x3fffffff;
  108. getrt(x,0,siz[x]);
  109. int rt=id;
  110. vis[rt]=1;
  111. fa[rt]=f;
  112. work(rt);
  113. for(int i=fi[rt];i;i=nxt[i]){
  114. if(vis[to[i]])continue;
  115. Div(to[i],rt);
  116. }
  117. if(B[rt].size()>=2)C.insert(B[rt].longest());
  118. }
  119. void init(){
  120. dfs1(1);
  121. dfs2(1,1);
  122. Div(1,0);
  123. }
  124. void turnon(int x){
  125. if(B[x].size()>=2)C.del(B[x].longest());
  126. B[x].del(0);
  127. if(B[x].size()>=2)C.insert(B[x].longest());
  128. int now=x;
  129. while(fa[now]){
  130. if(B[fa[now]].size()>=2)C.del(B[fa[now]].longest());
  131. B[fa[now]].del(A[now].top());
  132. A[now].del(get_dis(x,fa[now]));
  133. if(A[now].size()>=1)B[fa[now]].insert(A[now].top());
  134. if(B[fa[now]].size()>=2)C.insert(B[fa[now]].longest());
  135. now=fa[now];
  136. }
  137. }
  138. void turnoff(int x){
  139. if(B[x].size()>=2)C.del(B[x].longest());
  140. B[x].insert(0);
  141. if(B[x].size()>=2)C.insert(B[x].longest());
  142. int now=x;
  143. while(fa[now]){
  144. if(B[fa[now]].size()>=2)C.del(B[fa[now]].longest());
  145. if(A[now].size()>=1)B[fa[now]].del(A[now].top());
  146. A[now].insert(get_dis(x,fa[now]));
  147. B[fa[now]].insert(A[now].top());
  148. if(B[fa[now]].size()>=2)C.insert(B[fa[now]].longest());
  149. now=fa[now];
  150. }
  151. }
  152. int main()
  153. {
  154. freopen("hide.in","r",stdin);
  155. freopen("hide.out","w",stdout);
  156. scanf("%d",&n);
  157. now_cnt=n;
  158. for(int i=1;i<n;i++){
  159. int x,y;
  160. scanf("%d%d",&x,&y);
  161. link(x,y);link(y,x);
  162. }
  163. init();
  164. scanf("%d",&q);
  165. for(int i=1;i<=q;i++){
  166. char s[10];
  167. int x;
  168. scanf("%s",s);
  169. if(s[0]=='C'){
  170. scanf("%d",&x);
  171. if(w[x]){
  172. turnoff(x);
  173. now_cnt++;
  174. }
  175. else {
  176. turnon(x);
  177. now_cnt--;
  178. }
  179. w[x]^=1;
  180. }
  181. else {
  182. if(now_cnt==0)printf("-1\n");
  183. else if(now_cnt==1)printf("0\n");
  184. else printf("%d\n",C.top());
  185. }
  186. }
  187. return 0;
  188. }