通过这题我知道了一个鬼故事,trunc(ln(128)/ln(2))=6……以后不敢轻易这么写了

好了言归正传,这题明显的构建虚树,但构建虚树后怎么树形dp呢?

由于虚树上的点不仅是议事会还有可能是议事会的LCA,所以

我们要先求出虚树上每个点是被那个议事会管理的,这我们可以通过两遍dfs求出(儿子更新父亲,父亲更新儿子)

然后我们考虑虚树上每条边所代表原数的结点归属就可以了,这个地方细节比较多,建议自己想,具体见代码注释

  1. type node=record
  2. po,next:longint;
  3. end;
  4. point=record
  5. fr,ds:longint;
  6. end;
  7.  
  8. var e:array[..] of node;
  9. f,b,c,d,a,st,ans,p,s:array[..] of longint;
  10. anc:array[..,..] of longint;
  11. w:array[..] of point;
  12. n,q,m,i,len,j,x,y,t,z:longint;
  13. v:array[..] of boolean;
  14.  
  15. procedure swap(var a,b:longint);
  16. var c:longint;
  17. begin
  18. c:=a;
  19. a:=b;
  20. b:=c;
  21. end;
  22.  
  23. procedure add(x,y:longint);
  24. begin
  25. inc(len);
  26. e[len].po:=y;
  27. e[len].next:=p[x];
  28. p[x]:=len;
  29. end;
  30.  
  31. procedure dfs(x:longint);
  32. var i,y:longint;
  33. begin
  34. inc(t);
  35. a[x]:=t;
  36. s[x]:=;
  37. i:=p[x];
  38. while i<> do
  39. begin
  40. y:=e[i].po;
  41. if s[y]= then
  42. begin
  43. anc[y,]:=x;
  44. d[y]:=d[x]+;
  45. dfs(y);
  46. s[x]:=s[x]+s[y];
  47. end;
  48. i:=e[i].next;
  49. end;
  50. end;
  51.  
  52. procedure sort(l,r:longint);
  53. var i,j,x:longint;
  54. begin
  55. i:=l;
  56. j:=r;
  57. x:=c[(l+r) shr ];
  58. repeat
  59. while a[c[i]]<a[x] do inc(i);
  60. while a[x]<a[c[j]] do dec(j);
  61. if not(i>j) then
  62. begin
  63. swap(c[i],c[j]);
  64. inc(i);
  65. dec(j);
  66. end;
  67. until i>j;
  68. if l<j then sort(l,j);
  69. if i<r then sort(i,r);
  70. end;
  71.  
  72. function lca(x,y:longint):longint;
  73. var i:longint;
  74. begin
  75. if x=y then exit(x);
  76. if d[x]<d[y] then swap(x,y);
  77. if d[x]>d[y] then
  78. begin
  79. for i:= downto do
  80. if d[x]- shl i>=d[y] then x:=anc[x,i];
  81. end;
  82. if x=y then exit(x);
  83. for i:= downto do
  84. if anc[x,i]<>anc[y,i] then
  85. begin
  86. x:=anc[x,i];
  87. y:=anc[y,i];
  88. end;
  89. exit(anc[x,]);
  90. end;
  91.  
  92. procedure get(var a:point;x,y:longint);
  93. begin
  94. if (a.ds>x) or (a.ds=x) and (a.fr>y) then
  95. begin
  96. a.ds:=x;
  97. a.fr:=y;
  98. end;
  99. end;
  100.  
  101. function find(x,h:longint):longint;
  102. var i:longint;
  103. begin
  104. if h= then exit(x);
  105. for i:= downto do
  106. if h- shl i>= then
  107. begin
  108. x:=anc[x,i];
  109. h:=h- shl i;
  110. if h= then break;
  111. end;
  112. exit(x);
  113. end;
  114.  
  115. procedure work1(x:longint);
  116. var i,y:longint;
  117. begin
  118. if v[x] then
  119. begin
  120. w[x].fr:=x;
  121. w[x].ds:=;
  122. end
  123. else begin
  124. w[x].fr:=n+;
  125. w[x].ds:=;
  126. end;
  127. f[x]:=s[x];
  128. i:=p[x];
  129. while i<> do
  130. begin
  131. y:=e[i].po;
  132. f[x]:=f[x]-s[find(y,d[y]-d[x]-)];
  133. work1(y);
  134. get(w[x],w[y].ds+d[y]-d[x],w[y].fr);
  135. i:=e[i].next;
  136. end;
  137. end;
  138.  
  139. procedure work2(x:longint);
  140. var i,y:longint;
  141. begin
  142. i:=p[x];
  143. while i<> do
  144. begin
  145. y:=e[i].po;
  146. get(w[y],w[x].ds+d[y]-d[x],w[x].fr);
  147. work2(y);
  148. i:=e[i].next;
  149. end;
  150. end;
  151.  
  152. procedure calc(x:longint);
  153. var i,y,l,h:longint;
  154. begin
  155. inc(ans[b[w[x].fr]],f[x]); //我们先单独考虑边的端点
  156. i:=p[x];
  157. while i<> do
  158. begin
  159. y:=e[i].po;
  160. if w[x].fr=w[y].fr then
  161. inc(ans[b[w[x].fr]],s[find(y,d[y]-d[x]-)]-s[y])
  162. else begin
  163. l:=w[x].ds+w[y].ds+d[y]-d[x];
  164. h:=l div -w[y].ds; //均分
  165. if (l mod =) and (w[x].fr<w[y].fr) then dec(h); //注意临界情况
  166. h:=find(y,h); //寻找向上d个单位的点
  167. inc(ans[b[w[x].fr]],s[find(y,d[y]-d[x]-)]-s[h]); //注意这里的结点归属
  168. inc(ans[b[w[y].fr]],s[h]-s[y]);
  169. end;
  170. calc(y);
  171. i:=e[i].next;
  172. end;
  173. p[x]:=;
  174. end;
  175.  
  176. begin
  177. readln(n);
  178. for i:= to n- do
  179. begin
  180. readln(x,y);
  181. add(x,y);
  182. add(y,x);
  183. end;
  184. dfs();
  185. for j:= to trunc(ln(n)/ln()) do
  186. for i:= to n do
  187. begin
  188. x:=anc[i,j-];
  189. anc[i,j]:=anc[x,j-];
  190. end;
  191.  
  192. len:=;
  193. fillchar(p,sizeof(p),);
  194. readln(m);
  195. while m> do
  196. begin
  197. dec(m);
  198. len:=;
  199. readln(q);
  200. for i:= to q do
  201. begin
  202. read(c[i]);
  203. b[c[i]]:=i;
  204. v[c[i]]:=true;
  205. end;
  206. sort(,q);
  207. st[]:=;
  208. t:=;
  209. for i:= to q do
  210. begin
  211. x:=c[i];
  212. z:=lca(x,st[t]);
  213. while d[z]<d[st[t]] do
  214. begin
  215. if d[z]>=d[st[t-]] then
  216. begin
  217. add(z,st[t]);
  218. dec(t);
  219. if st[t]<>z then
  220. begin
  221. inc(t);
  222. st[t]:=z;
  223. end;
  224. break;
  225. end;
  226. add(st[t-],st[t]);
  227. dec(t);
  228. end;
  229. if st[t]<>x then
  230. begin
  231. inc(t);
  232. st[t]:=x;
  233. end;
  234. end;
  235. while t> do
  236. begin
  237. add(st[t-],st[t]);
  238. dec(t);
  239. end;
  240. work1();
  241. work2();
  242. calc();
  243. for i:= to q do
  244. begin
  245. write(ans[i],' ');
  246. ans[i]:=;
  247. b[c[i]]:=;
  248. v[c[i]]:=false;
  249. end;
  250. writeln;
  251. end;
  252. end.

bzoj3572的更多相关文章

  1. 【bzoj3572】 世界树

    http://www.lydsy.com/JudgeOnline/problem.php?id=3572 (题目链接) 题意 给出一棵n个节点的树,q次询问,每次给出k个关键点.规定对于树上每个节点归 ...

  2. [BZOJ3572][Hnoi2014]世界树

    [BZOJ3572][Hnoi2014]世界树 试题描述 世界树是一棵无比巨大的树,它伸出的枝干构成了整个世界.在这里,生存着各种各样的种族和生灵,他们共同信奉着绝对公正公平的女神艾莉森,在他们的信条 ...

  3. 【BZOJ3572】[Hnoi2014]世界树 虚树

    [BZOJ3572][Hnoi2014]世界树 Description 世界树是一棵无比巨大的树,它伸出的枝干构成了整个世界.在这里,生存着各种各样的种族和生灵,他们共同信奉着绝对公正公平的女神艾莉森 ...

  4. bzoj3572又TM是网络流

    = =我承认我写网络流写疯了 = =我承认前面几篇博文都是扯淡,我写的是垃圾dinic(根本不叫dinic) = =我承认这道题我调了半天 = =我承认我这道题一开始是T的,后来换上真正的dinic才 ...

  5. 【BZOJ-3572】世界树 虚树 + 树形DP

    3572: [Hnoi2014]世界树 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 1084  Solved: 611[Submit][Status ...

  6. bzoj千题计划255:bzoj3572: [Hnoi2014]世界树

    http://www.lydsy.com/JudgeOnline/problem.php?id=3572 明显需要构造虚树 点属于谁管理分三种情况: 1.属于虚树的点 2.在虚树上的边上的点 3.既不 ...

  7. 2018.09.25 bzoj3572: [Hnoi2014]世界树(虚树+树形dp)

    传送门 虚树入门题? 好难啊. 在学习别人的写法之后终于过了. 这道题dp方程很好想. 主要是不好写. 简要说说思路吧. 显然最优值只能够从子树和父亲转移过来. 于是我们先dfs一遍用儿子更新父亲,然 ...

  8. BZOJ3572:[HNOI2014]世界树——题解

    +++++++++++++++++++++++++++++++++++++++++++ +本文作者:luyouqi233. + +欢迎访问我的博客:http://www.cnblogs.com/luy ...

  9. [HNOI2014][bzoj3572] 世界树 [虚树+dp]

    题面: 传送门 思路: 一道虚树的好题,是很多虚树博客的入门题目 但是我认为这道题目出的难点和亮点不在于虚树,而在于建出虚树以后dp的思路与实现 下文中为方便描述,用势力范围来表示一个“议事处”管辖的 ...

随机推荐

  1. php微信支付(仅Jsapi支付)详细步骤.----仅适合第一次做微信开发的程序员

    本人最近做了微信支付开发,是第一次接触.其中走了很多弯路,遇到的问题也很多.为了让和我一样的新人不再遇到类似的问题,我把我的开发步骤和问题写出来,以供参考. 开发时间是2016/8/2,所以微信支付的 ...

  2. 直接下载完整chrome浏览器的方法

    目前通过下吗的链接可以获得独立的安装包. http://www.google.com/chrome/eula.html?standalone=1&hl=zh-CN

  3. CentOS7安装nagios并配置出图详解

    目录 开始之前 系统环境 监控内容 所需软件包 台机器,全都按照CentOS7最小化模式安装系统 系统版本号 [root@localhost ~]# cat  /etc/redhat-release ...

  4. Oracle中的CR块详解

    1.概述 Cr块consistent read块也就是用来维护oracle的读一致性的数据块.当查询某些数据的时候,发现数据块的版本比我们要查询的新,例如session1执行了dml操作并没有提交,s ...

  5. jQuery 点击按钮刷新页面

    //页面加载时绑定按钮点击事件 $(function () { $("#按钮id").click(function () { refresh(); }); }); //点击按钮调用 ...

  6. 使用自定义任务审批字段创建 SharePoint 顺序工作流

    http://msdn.microsoft.com/zh-cn/library/hh824675(v=office.14).aspx#odc_sp14_ta_CreatingSPSeqWorkflow ...

  7. Notepad++ 书签

      Notepad++,有一个书签功能,指定书签是Ctrl+F2,在书签之间移动是按F2来切换,这个可以在几个想查看的数据之间进行快速切换,所以看起来就很方便.

  8. 【贪心】 BZOJ 3252:攻略

    3252: 攻略 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 261  Solved: 90[Submit][Status][Discuss] De ...

  9. OD之常用命令

    一:od断点注释保存的问题,由于od只有在正常退出的情况下才会保存分析代码时留下的注释,而很多时候为了在退出od时不让目标程序退出使用了剥离进程,这样就会导致这次操作所有的注释都没有保存,第二次重新载 ...

  10. ASP + ACCESS 上传图片到数据库与将图片读出数据库显示之实现

    1.uppic.asp:上传图片程序 <% dim rs dim formsize,formdata,bncrlf,divider,datastart,dataend,mydata formsi ...