{"id":535,"date":"2018-04-04T13:45:19","date_gmt":"2018-04-04T05:45:19","guid":{"rendered":"http:\/\/kylelv.com\/?p=535"},"modified":"2018-05-23T13:47:41","modified_gmt":"2018-05-23T05:47:41","slug":"bzoj-3991-sdoi2015%e5%af%bb%e5%ae%9d%e6%b8%b8%e6%88%8f-dfs%e5%ba%8f%ef%bc%8cset","status":"publish","type":"post","link":"https:\/\/blog.kylelv.com\/?p=535","title":{"rendered":"bzoj 3991: [SDOI2015]\u5bfb\u5b9d\u6e38\u620f  &#8212; dfs\u5e8f\uff0cset"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_73 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-69fdef605c965\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69fdef605c965\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blog.kylelv.com\/?p=535\/#3991_SDOI2015%E5%AF%BB%E5%AE%9D%E6%B8%B8%E6%88%8F\" title=\"3991: [SDOI2015]\u5bfb\u5b9d\u6e38\u620f\">3991: [SDOI2015]\u5bfb\u5b9d\u6e38\u620f<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blog.kylelv.com\/?p=535\/#Description\" title=\"Description\">Description<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/blog.kylelv.com\/?p=535\/#Input\" title=\"Input\">Input<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/blog.kylelv.com\/?p=535\/#Output\" title=\"Output\">Output<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/blog.kylelv.com\/?p=535\/#Sample_Input\" title=\"Sample Input\">Sample Input<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/blog.kylelv.com\/?p=535\/#Sample_Output\" title=\"Sample Output\">Sample Output<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/blog.kylelv.com\/?p=535\/#HINT\" title=\"HINT\">HINT<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/blog.kylelv.com\/?p=535\/#Source\" title=\"Source\">Source<\/a><\/li><\/ul><\/nav><\/div>\n<h2 style=\"text-align: center;\"><span class=\"ez-toc-section\" id=\"3991_SDOI2015%E5%AF%BB%E5%AE%9D%E6%B8%B8%E6%88%8F\"><\/span>3991: [SDOI2015]\u5bfb\u5b9d\u6e38\u620f<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: center;\"><span class=\"green\">Time Limit:\u00a0<\/span>40 Sec\u00a0\u00a0<span class=\"green\">Memory Limit:\u00a0<\/span>128 MB<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Description\"><\/span>Description<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"content\">\n<p>\u00a0\u5c0f<span lang=\"EN-US\">B<\/span>\u6700\u8fd1\u6b63\u5728\u73a9\u4e00\u4e2a\u5bfb\u5b9d\u6e38\u620f\uff0c\u8fd9\u4e2a\u6e38\u620f\u7684\u5730\u56fe\u4e2d\u6709<span lang=\"EN-US\">N<\/span>\u4e2a\u6751\u5e84\u548c<span lang=\"EN-US\">N-1<\/span>\u6761\u9053\u8def\uff0c\u5e76\u4e14\u4efb\u4f55\u4e24\u4e2a\u6751\u5e84\u4e4b\u95f4\u6709\u4e14\u4ec5\u6709\u4e00\u6761\u8def\u5f84\u53ef\u8fbe\u3002\u6e38\u620f\u5f00\u59cb\u65f6\uff0c\u73a9\u5bb6\u53ef\u4ee5\u4efb\u610f\u9009\u62e9\u4e00\u4e2a\u6751\u5e84\uff0c\u77ac\u95f4\u8f6c\u79fb\u5230\u8fd9\u4e2a\u6751\u5e84\uff0c\u7136\u540e\u53ef\u4ee5\u4efb\u610f\u5728\u5730\u56fe\u7684\u9053\u8def\u4e0a\u884c\u8d70\uff0c\u82e5\u8d70\u5230\u67d0\u4e2a\u6751\u5e84\u4e2d\u6709\u5b9d\u7269\uff0c\u5219\u89c6\u4e3a\u627e\u5230\u8be5\u6751\u5e84\u5185\u7684\u5b9d\u7269\uff0c\u76f4\u5230\u627e\u5230\u6240\u6709\u5b9d\u7269\u5e76\u8fd4\u56de\u5230\u6700\u521d\u8f6c\u79fb\u5230\u7684\u6751\u5e84\u4e3a\u6b62\u3002\u5c0f<span lang=\"EN-US\">B<\/span>\u5e0c\u671b\u8bc4\u6d4b\u4e00\u4e0b\u8fd9\u4e2a\u6e38\u620f\u7684\u96be\u5ea6\uff0c\u56e0\u6b64\u4ed6\u9700\u8981\u77e5\u9053\u73a9\u5bb6\u627e\u5230\u6240\u6709\u5b9d\u7269\u9700\u8981\u884c\u8d70\u7684\u6700\u77ed\u8def\u7a0b\u3002\u4f46\u662f\u8fd9\u4e2a\u6e38\u620f\u4e2d\u5b9d\u7269\u7ecf\u5e38\u53d8\u5316\uff0c\u6709\u65f6\u67d0\u4e2a\u6751\u5e84\u4e2d\u4f1a\u7a81\u7136\u51fa\u73b0\u5b9d\u7269\uff0c\u6709\u65f6\u67d0\u4e2a\u6751\u5e84\u5185\u7684\u5b9d\u7269\u4f1a\u7a81\u7136\u6d88\u5931\uff0c\u56e0\u6b64\u5c0f<span lang=\"EN-US\">B<\/span>\u9700\u8981\u4e0d\u65ad\u5730\u66f4\u65b0\u6570\u636e\uff0c\u4f46\u662f\u5c0f<span lang=\"EN-US\">B<\/span>\u592a\u61d2\u4e86\uff0c\u4e0d\u613f\u610f\u81ea\u5df1\u8ba1\u7b97\uff0c\u56e0\u6b64\u4ed6\u5411\u4f60\u6c42\u52a9\u3002\u4e3a\u4e86\u7b80\u5316\u95ee\u9898\uff0c\u6211\u4eec\u8ba4\u4e3a\u6700\u5f00\u59cb\u65f6\u6240\u6709\u6751\u5e84\u5185\u5747\u6ca1\u6709\u5b9d\u7269<\/p>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Input\"><\/span>Input<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"content\">\n<p>\u00a0\u7b2c\u4e00\u884c\uff0c\u4e24\u4e2a\u6574\u6570N\u3001M\uff0c\u5176\u4e2dM\u4e3a\u5b9d\u7269\u7684\u53d8\u52a8\u6b21\u6570\u3002<\/p>\n<div>\u63a5\u4e0b\u6765\u7684N-1\u884c\uff0c\u6bcf\u884c\u4e09\u4e2a\u6574\u6570x\u3001y\u3001z\uff0c\u8868\u793a\u6751\u5e84x\u3001y\u4e4b\u95f4\u6709\u4e00\u6761\u957f\u5ea6\u4e3az\u7684\u9053\u8def\u3002<\/div>\n<div>\u63a5\u4e0b\u6765\u7684M\u884c\uff0c\u6bcf\u884c\u4e00\u4e2a\u6574\u6570t\uff0c\u8868\u793a\u4e00\u4e2a\u5b9d\u7269\u53d8\u52a8\u7684\u64cd\u4f5c\u3002\u82e5\u8be5\u64cd\u4f5c\u524d\u6751\u5e84t\u5185\u6ca1\u6709\u5b9d\u7269\uff0c\u5219\u64cd\u4f5c\u540e\u6751\u5e84\u5185\u6709\u5b9d\u7269\uff1b\u82e5\u8be5\u64cd\u4f5c\u524d\u6751\u5e84t\u5185\u6709\u5b9d\u7269\uff0c\u5219\u64cd\u4f5c\u540e\u6751\u5e84\u5185\u6ca1\u6709\u5b9d\u7269\u3002<\/div>\n<div><\/div>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Output\"><\/span>Output<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"content\">\n<p>\u00a0M\u884c\uff0c\u6bcf\u884c\u4e00\u4e2a\u6574\u6570\uff0c\u5176\u4e2d\u7b2ci\u884c\u7684\u6574\u6570\u8868\u793a\u7b2ci\u6b21\u64cd\u4f5c\u4e4b\u540e\u73a9\u5bb6\u627e\u5230\u6240\u6709\u5b9d\u7269\u9700\u8981\u884c\u8d70\u7684\u6700\u77ed\u8def\u7a0b\u3002\u82e5\u53ea\u6709\u4e00\u4e2a\u6751\u5e84\u5185\u6709\u5b9d\u7269\uff0c\u6216\u8005\u6240\u6709\u6751\u5e84\u5185\u90fd\u6ca1\u6709\u5b9d\u7269\uff0c\u5219\u8f93\u51fa0\u3002<\/p>\n<div><\/div>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Sample_Input\"><\/span>Sample Input<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"content\"><span class=\"sampledata\">4 5<br \/>\n1 2 30<br \/>\n2 3 50<br \/>\n2 4 60<br \/>\n2<br \/>\n3<br \/>\n4<br \/>\n2<br \/>\n1<\/span><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Sample_Output\"><\/span>Sample Output<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"content\"><span class=\"sampledata\">0<br \/>\n100<br \/>\n220<br \/>\n220<br \/>\n280<\/span><\/div>\n<h2><span class=\"ez-toc-section\" id=\"HINT\"><\/span>HINT<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"content\">\n<p>\u00a01&lt;=N&lt;=100000<\/p>\n<div>1&lt;=M&lt;=100000<\/div>\n<div>\u5bf9\u4e8e\u5168\u90e8\u7684\u6570\u636e\uff0c1&lt;=z&lt;=10^9<\/div>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Source\"><\/span>Source<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u53ef\u4ee5\u8bc1\u660e\u4e00\u4e2a\u7ed3\u8bba\uff0c\u6700\u4f18\u65b9\u6848\u5c31\u662f\u628a\u6240\u6709\u5173\u952e\u70b9\u6392\u5e8f\uff0c\u76f8\u90bb\u70b9\u7684\u8def\u5f84\u548c\u52a0\u4e0a\u4e24\u7aef\u70b9\u7684\u8def\u5f84<\/p>\n<p>\u90a3\u4e48\u5c31\u662f\u53ef\u4ee5\u7ef4\u62a4\u4e00\u4e2aset\uff0c\u7ef4\u62a4dfs\u5e8f\uff0c\u6bcf\u6b21\u64cd\u4f5c\u5c31\u662f\u63d2\u5165\u548c\u5220\u9664\uff0c\u8ba1\u7b97\u76f8\u90bb\u70b9\u7684\u53d8\u5316\u5c31\u597d<\/p>\n<pre class=\"lang:c++ decode:true \">#include&lt;set&gt;\r\n#include&lt;cmath&gt;\r\n#include&lt;queue&gt;\r\n#include&lt;cstdio&gt;\r\n#include&lt;string&gt;\r\n#include&lt;cstring&gt;\r\n#include&lt;iostream&gt;\r\n#include&lt;algorithm&gt;\r\nusing namespace std;\r\n#define inf 1000000007\r\n#define ll long long\r\n#define N 200010\r\ninline int rd()\r\n{\r\n\tint x=0,f=1;char ch=getchar();\r\n\twhile(ch&lt;'0'||ch&gt;'9'){if(ch=='-')f=-1;ch=getchar();}\r\n\twhile(ch&gt;='0'&amp;&amp;ch&lt;='9'){x=x*10+ch-'0';ch=getchar();}\r\n\treturn x*f;\r\n}\r\nint lj[N],fro[N],to[N],cnt;\r\nll v[N];\r\nvoid add(int a,int b,int c){fro[++cnt]=lj[a];to[cnt]=b;lj[a]=cnt;v[cnt]=c;}\r\nint n,m;\r\nint dep[N],dfn[N],tim;\r\nint fa[N][21];\r\nll dis[N];\r\nvoid dfs(int x,int f)\r\n{\r\n\tdep[x]=dep[f]+1;\r\n\tdfn[x]=++tim;\r\n\tfa[x][0]=f;\r\n\tfor(int i=1;i&lt;21;i++) fa[x][i]=fa[fa[x][i-1]][i-1];\r\n\tfor(int i=lj[x];i;i=fro[i])\r\n\t{\r\n\t\tif(to[i]==f) continue;\r\n\t\tdis[to[i]]=dis[x]+v[i];\r\n\t\tdfs(to[i],x);\r\n\t}\r\n}\r\nset&lt;pair&lt;int,int&gt; &gt;s;\r\nset&lt;pair&lt;int,int&gt; &gt;::iterator it,bg,ed,b2,i2,i1;\r\n#define mp make_pair\r\nbool vs[N];\r\nll ans;\r\nint LCA(int a,int b)\r\n{\r\n\tif(dep[a]&lt;dep[b]) swap(a,b);\r\n\tfor(int i=20;i&gt;=0;i--) if(dep[fa[a][i]]&gt;=dep[b]) a=fa[a][i];\r\n\tif(a==b) return a;\r\n\tfor(int i=20;i&gt;=0;i--) if(fa[a][i]^fa[b][i]) a=fa[a][i],b=fa[b][i];\r\n\treturn fa[a][0];\r\n}\r\nll fd(int a,int b)\r\n{\r\n\tint l=LCA(a,b);\r\n\treturn dis[a]+dis[b]-dis[l]*2;\r\n}\r\nint main()\r\n{\r\n\tn=rd();m=rd();\r\n\tfor(int i=1,x,y,z;i&lt;n;i++)\r\n\t{\r\n\t\tx=rd();y=rd();z=rd();\r\n\t\tadd(x,y,z);add(y,x,z);\r\n\t}\r\n\tdfs(1,0);\r\n\tint x,y,z;\r\n\tpair&lt;int,int&gt;p;\r\n\twhile(m--)\r\n\t{\r\n\t\tx=rd();\r\n\t\tp=mp(dfn[x],x);\r\n\t\tb2=bg=s.begin();ed=s.end();\r\n\t\tif(s.size()) b2++,ed--;\r\n\t\tif(!vs[x])\r\n\t\t{\r\n\t\t\tvs[x]=1;\r\n\t\t\tif(!s.size()); \r\n\t\t\telse if(s.size()==1) ans=fd((*bg).second,x)*2;\r\n\t\t\telse \r\n\t\t\t{\r\n\t\t\t\ti1=it=s.lower_bound(p);\r\n\t\t\t\tif(it==s.end()) i1--,y=(*bg).second,z=(*i1).second;\r\n\t\t\t\telse if(it==s.begin()) y=(*bg).second,z=(*ed).second;\r\n\t\t\t\telse i1--,y=(*i1).second,z=(*it).second;\r\n\t\t\t\tans-=fd(y,z);\r\n\t\t\t\tans+=fd(x,y)+fd(x,z);\r\n\t\t\t}\r\n\t\t\ts.insert(p);\r\n\t\t}\r\n\t\telse \r\n\t\t{\r\n\t\t\tvs[x]=0;\r\n\t\t\tif(s.size()&lt;=2) ans=0;\r\n\t\t\telse \r\n\t\t\t{\r\n\t\t\t\ti1=i2=it=s.lower_bound(p);i2++;\r\n\t\t\t\tif(i2==s.end()) i1--,y=(*bg).second,z=(*i1).second;\r\n\t\t\t\telse if(it==s.begin()) y=(*b2).second,z=(*ed).second;\r\n\t\t\t\telse i1--,y=(*i1).second,z=(*i2).second;\r\n\t\t\t\tans+=fd(y,z);\r\n\t\t\t\tans-=fd(x,y)+fd(x,z);\r\n\t\t\t}\r\n\t\t\ts.erase(p);\r\n\t\t}\r\n\t\tprintf(\"%lld\\n\",ans);\r\n\t}\r\n\treturn 0;\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>3991: [SDOI2015]\u5bfb\u5b9d\u6e38\u620f Time Limit:\u00a040 Sec\u00a0\u00a0Memory Limit:\u00a0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[59,60],"class_list":["post-535","post","type-post","status-publish","format-standard","hentry","category-bzoj","tag-dfs","tag-stl"],"_links":{"self":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/535","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=535"}],"version-history":[{"count":1,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/535\/revisions"}],"predecessor-version":[{"id":536,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/535\/revisions\/536"}],"wp:attachment":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}