{"id":876,"date":"2019-06-14T16:26:47","date_gmt":"2019-06-14T08:26:47","guid":{"rendered":"http:\/\/SmokeyDays.top\/wordpress\/?p=876"},"modified":"2019-06-14T16:26:47","modified_gmt":"2019-06-14T08:26:47","slug":"lp5290-%e5%8d%81%e4%ba%8c%e7%9c%81%e8%81%94%e8%80%832019-%e6%98%a5%e8%8a%82%e5%8d%81%e4%ba%8c%e5%93%8d","status":"publish","type":"post","link":"http:\/\/SmokeyDays.top\/wordpress\/2019\/06\/14\/lp5290-%e5%8d%81%e4%ba%8c%e7%9c%81%e8%81%94%e8%80%832019-%e6%98%a5%e8%8a%82%e5%8d%81%e4%ba%8c%e5%93%8d\/","title":{"rendered":"lp5290 \u5341\u4e8c\u7701\u8054\u80032019 \u6625\u8282\u5341\u4e8c\u54cd"},"content":{"rendered":"\n<p>\u9996\u5148\u6211\u4eec\u8003\u8651\u4e00\u79cdn^2logn\u7684\u505a\u6cd5\uff1a\u5c06\u6240\u6709\u7684\u5b50\u7a0b\u5e8f\u6839\u636e\u5185\u5b58\u6d88\u8017\u6765\u6392\u5e8f\uff0c\u7136\u540e\u6bcf\u4e00\u6b21\u9009\u4e2d\u5185\u5b58\u6d88\u8017\u6700\u5927\u4e14\u5c1a\u672a\u88ab\u9009\u4e2d\u7684\uff0c\u5c06\u6240\u6709\u5185\u5b58\u6bd4\u5b83\u5c0f\u5e76\u4e14\u53ef\u4ee5\u88ab\u540c\u65f6\u9009\u4e2d\u7684\u9009\u4e2d\uff0c\u7136\u540e\u7edf\u8ba1\u8d21\u732e\u3002<br>\n\u4e8e\u662f\u95ee\u9898\u8f6c\u5316\u4e3a\u5982\u4f55\u7528logn\u7684\u590d\u6742\u5ea6\u67e5\u8be2\u7956\u5148-\u540e\u4ee3\u5173\u7cfb\u3002<br>\n\u6211\u4eec\u8003\u8651\u5c06\u6574\u68f5\u6811\u5c55\u5f00\u4e3aDFS\u5e8f\u5217\uff0c\u90a3\u4e48\u53ef\u4ee5\u7528\u6811\u72b6\u6570\u7ec4\u7ef4\u62a4\u7956\u5148-\u540e\u4ee3\u5173\u7cfb\u3002<\/p>\n\n\n\n<p>\u5982\u4f55\u4f18\u5316\u8fd9\u4e2a\u505a\u6cd5\u5462\uff1f<br>\n\u6211\u4eec\u6ce8\u610f\u5230\u5b58\u5728\u4e00\u79cd\u94fe\u7684\u90e8\u5206\u5206\u3002\u8fd9\u79cd\u60c5\u51b5\u662f\u5bb9\u6613\u89e3\u51b3\u7684\uff1a\u641e\u4e24\u4e2a\u5806\uff0c\u5148\u628a\u4e24\u6761\u5b50\u94fe\u5404\u81ea\u52a0\u5230\u5806\u91cc\uff0c\u7136\u540e\u6bcf\u6b21\u5c06\u5b83\u4eec\u7684\u5806\u9876\u5404\u81ea\u63d0\u51fa\u6765\uff0c\u7136\u540e\u53d6\u8f83\u5927\u503c\u52a0\u5165\u6700\u7ec8\u7b54\u6848\u3002<br>\n\u8fd9\u542f\u53d1\u6211\u4eec\u7528\u7c7b\u4f3c\u7684\u65b9\u6cd5\u5904\u7406\u4e00\u822c\u7684\u6811\u72b6\u60c5\u51b5\u3002<br>\n\u5bf9\u4e8e\u4efb\u610f\u4e00\u4e2a\u8282\u70b9\uff0c\u6211\u4eec\u5efa\u4e00\u4e2a\u5806\u4ee3\u8868\u8fd9\u4e2a\u8282\u70b9\u7684\u5b50\u6811\u7684\u6570\u503c\u60c5\u51b5\uff0c\u6bcf\u4e00\u6b21\u4f7f\u7528\u7c7b\u4f3c\u94fe\u7684\u65b9\u6cd5\u5408\u5e76\u3002<br>\n\u7136\u800c\uff0c\u8fd9\u6837\u5e76\u4e0d\u80fd\u4ece\u672c\u8d28\u4e0a\u63d0\u5347\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u5b83\u4ecd\u7136\u9700\u8981n^2logn\u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u53ea\u4e0d\u8fc7\u4e0d\u6ee1\u3002<br>\n\u8003\u8651\u5230\u8fd9\u662f\u6811\u4e0a\u7684\u5408\u5e76\u95ee\u9898\uff0c\u56e0\u6b64\u5c1d\u8bd5\u4f7f\u7528\u542f\u53d1\u5f0f\u5408\u5e76\uff0c\u6bcf\u6b21\u53ea\u5408\u5e76\u4e24\u4e2a\u8282\u70b9\uff0c\u5e76\u5c06\u5927\u5c0f\u8f83\u5c0f\u7684\u90a3\u4e2a\u8282\u70b9\u7684\u5806\u91cc\u7684\u4e1c\u897f\u585e\u5230\u5927\u5c0f\u8f83\u5927\u7684\u90a3\u4e2a\u8282\u70b9\u7684\u5806\u91cc\u3002<br>\n\u5747\u644a\u800c\u8a00\uff0c\u6bcf\u4e2a\u70b9\u7684\u4f4d\u7f6e\u53ea\u4f1a\u88ab\u8f6c\u79fb\u4e00\u6b21\u2014\u2014\u8fd9\u662f\u56e0\u4e3a\u6bcf\u4e00\u6b21\u6709\u4e14\u4ec5\u6709\u8f83\u5c0f\u7684\u90a3\u4e2a\u5806\u4f1a\u88ab\u5408\u5e76\uff0c\u800c\u8f83\u5927\u7684\u90a3\u4e2a\u5806\u4e0d\u4f1a\u52a8\u3002\u4e8e\u662f\uff0c\u5982\u679c\u4e00\u4e2a\u70b9\u88ab\u8f6c\u79fb\u4e86\u4e24\u6b21\uff0c\u90a3\u4e48\u4e00\u5b9a\u610f\u5473\u7740\u4e00\u4e2a\u6216\u8005\u66f4\u591a\u7684\u8282\u70b9\u5728\u8fd9\u4e00\u5c42\u7684\u5408\u5e76\u4e2d\u6ca1\u6709\u88ab\u8f6c\u79fb\u3002\u6240\u4ee5\u5747\u644a\u5171\u8f6c\u79fb\u4e0d\u5230n\u6b21\u3002<br>\n\u5bf9\u4e8e\u6bcf\u4e00\u6b21\u8282\u70b9\u7684\u4f4d\u7f6e\u8f6c\u79fb\uff0c\u590d\u6742\u5ea6\u662flogn\uff0c\u56e0\u6b64\u603b\u590d\u6742\u5ea6\u662f\\(O(n*logn)\\)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include&lt;iostream>\n#include&lt;cstdio>\n#include&lt;queue>\n\ntypedef long long ll;\nconst int N=200005;\ninline void Swap(int &amp;A,int &amp;B){\n\tA^=B^=A^=B;\n}\ninline int Max(int A,int B){\n\treturn A>B?A:B;\n}\nstruct ee{\n\tint v;\n\tint nxt;\n}e[N&lt;&lt;1];\nint h[N],et=0;\ninline void add(int U,int V){\n\te[++et]=(ee){V,h[U]};\n\th[U]=et;\n}\nint fa[N],n;\nll ans=0;\nint val[N],id[N],st[N],tp=0;\nstd::priority_queue&lt;int> q[N];\ninline void uni(int X,int Y){\n\tif(q[id[X]].size()&lt;q[id[Y]].size()){\n\t\tSwap(id[X],id[Y]);\n\t}\n\twhile(!q[id[Y]].empty()){\n\t\tst[++tp]=Max(q[id[X]].top(),q[id[Y]].top());\n\t\tq[id[X]].pop(),q[id[Y]].pop();\n\t}\n\twhile(tp){\n\t\tq[id[X]].push(st[tp--]);\n\t} \n}\ninline void dfs(int X){\n\tfor(int i=h[X];i;i=e[i].nxt){\n\t\tdfs(e[i].v);\n\t\tuni(X,e[i].v);\n\t}\n\tq[id[X]].push(val[X]);\n}\nvoid init(){\n\tscanf(\"%d\",&amp;n);\n\tfor(int i=1;i&lt;=n;++i){\n\t\tscanf(\"%d\",&amp;val[i]);\n\t\tid[i]=i;\n\t}\n\tfor(int i=2;i&lt;=n;++i){\n\t\tscanf(\"%d\",&amp;fa[i]);\n\t\tadd(fa[i],i);\n\t}\n\tdfs(1);\n\twhile(!q[id[1]].empty()){\n\t\tans+=q[id[1]].top();\n\t\tq[id[1]].pop();\n\t}\n\tprintf(\"%lld\\n\",ans);\n}\n\nint main(){\n\tinit();\n\treturn 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9996\u5148\u6211\u4eec\u8003\u8651\u4e00\u79cdn^2logn\u7684\u505a\u6cd5\uff1a\u5c06\u6240\u6709\u7684\u5b50\u7a0b\u5e8f\u6839\u636e\u5185\u5b58\u6d88\u8017\u6765\u6392\u5e8f\uff0c\u7136\u540e\u6bcf\u4e00\u6b21\u9009\u4e2d\u5185\u5b58\u6d88\u8017\u6700\u5927\u4e14\u5c1a\u672a\u88ab\u9009\u4e2d &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/SmokeyDays.top\/wordpress\/2019\/06\/14\/lp5290-%e5%8d%81%e4%ba%8c%e7%9c%81%e8%81%94%e8%80%832019-%e6%98%a5%e8%8a%82%e5%8d%81%e4%ba%8c%e5%93%8d\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201clp5290 \u5341\u4e8c\u7701\u8054\u80032019 \u6625\u8282\u5341\u4e8c\u54cd\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[30,110,43,72,56,70,9,6,5],"tags":[],"_links":{"self":[{"href":"http:\/\/SmokeyDays.top\/wordpress\/wp-json\/wp\/v2\/posts\/876"}],"collection":[{"href":"http:\/\/SmokeyDays.top\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/SmokeyDays.top\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/SmokeyDays.top\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/SmokeyDays.top\/wordpress\/wp-json\/wp\/v2\/comments?post=876"}],"version-history":[{"count":1,"href":"http:\/\/SmokeyDays.top\/wordpress\/wp-json\/wp\/v2\/posts\/876\/revisions"}],"predecessor-version":[{"id":877,"href":"http:\/\/SmokeyDays.top\/wordpress\/wp-json\/wp\/v2\/posts\/876\/revisions\/877"}],"wp:attachment":[{"href":"http:\/\/SmokeyDays.top\/wordpress\/wp-json\/wp\/v2\/media?parent=876"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/SmokeyDays.top\/wordpress\/wp-json\/wp\/v2\/categories?post=876"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/SmokeyDays.top\/wordpress\/wp-json\/wp\/v2\/tags?post=876"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}