{"id":2316,"date":"2023-02-10T17:24:11","date_gmt":"2023-02-10T09:24:11","guid":{"rendered":"https:\/\/www.shuizilong.com\/house\/?p=2316"},"modified":"2023-02-10T17:24:11","modified_gmt":"2023-02-10T09:24:11","slug":"typedb-forces-2023","status":"publish","type":"post","link":"https:\/\/www.shuizilong.com\/house\/archives\/typedb-forces-2023\/","title":{"rendered":"TypeDB Forces 2023"},"content":{"rendered":"<ul>\n<li><a href=\"https:\/\/codeforces.com\/contest\/1787\">https:\/\/codeforces.com\/contest\/1787<\/a><\/li>\n<li><a href=\"https:\/\/zhuanlan.zhihu.com\/p\/601836449\">TypeDB Forces 2023 (Div. 1 + Div. 2) C (dp) D(dfs), \u4e25\u683c\u9e3d<\/a><\/li>\n<li><a href=\"https:\/\/zhuanlan.zhihu.com\/p\/602013323\">TypeDB Forces 2023 (Div. 1 + Div. 2, Rated, Prizes!), Immortality<\/a><\/li>\n<\/ul>\n<h2>Problem G. Colorful Tree Again<\/h2>\n<p>\u9898\u610f\uff1a\u7ed9\u5b9a\u4e00\u4e2a\u8fb9\u6743\u56fe\uff0c\u521d\u59cb\u56fa\u5b9a\u6bcf\u4e2a\u8fb9\u6709\u989c\u8272\u548c\u8fb9\u6743\u3002\u6bcf\u4e2a\u8282\u70b9\u4e5f\u6709\u989c\u8272\uff0c\u521d\u59cb\u90fd\u662f\u9ed1\u8272\u3002<br \/>\n\u6bcf\u6b21\u64cd\u4f5c\u53ef\u4ee5\u6539\u53d8\u4e00\u4e2a\u8282\u70b9\u7684\u989c\u8272\uff0c\u5e76\u8be2\u95ee\u5f53\u524d\u6700\u5927\u7684\u597d\u7684\u8def\u5f84\u7684\u6743\u503c\u548c\u662f\u591a\u5c11\u3002<br \/>\n\u4e00\u4e2a\u8def\u5f84\u662f\u597d\u7684\uff0c\u5f53\u4e14\u4ec5\u5f53\u8def\u5f84\u4e0a\u7684\u6240\u6709\u70b9\u989c\u8272\u76f8\u540c\uff0c\u5e76\u5305\u542b\u4e86\u6240\u6709\u8be5\u989c\u8272\u7684\u8fb9\uff0c\u4e14\u8def\u5f84\u4e0a\u7ecf\u8fc7\u7684\u6240\u6709\u8282\u70b9\u7684\u989c\u8272\u4e3a\u767d\u3002<\/p>\n<p>\u89e3\u6cd5\uff1a\u8fd9\u4e2a\u9898\u9996\u5148\u7b2c\u4e00\u53cd\u5e94\u662f QTREE 4 \u548c QTREE 6 \u7684\u67d0\u79cd\u7ed3\u5408\u3002\u3002\u3002 \u4f46\u662f\u4ed4\u7ec6\u8bfb\u9898\u53d1\u73b0\u5224\u5b9a\u7684\u6761\u4ef6\u662f path \u800c\u4e0d\u662f component \uff01\uff01\uff01<br \/>\n\u56e0\u800c\u9898\u76ee\u96be\u5ea6\u5927\u5e45\u7b80\u5316\u3002\u3002\u3002\u4f46\u662f\u60f3\u5199\u5f97\u7b80\u6d01\u4f9d\u7136\u4f1a\u5f88\u56f0\u96be\u3002\u3002\u3002<\/p>\n<p>\u9996\u5148\u7b2c\u4e00\u6b65\u5f53\u7136\u662f\u5254\u9664\u4e0d\u5408\u6cd5\u7684\u989c\u8272\u3002\u3002\u3002\uff08\u6700\u597d\u7528\u5ea6\u6570\u505a\u7b80\u5355\u7684\u5224\u5b9a\uff0c\u4e0d\u8981\u5728 dfs() \u7684\u65f6\u5019\u5224\u5b9a\u3002\u3002\uff09<br \/>\n\u7136\u540e\u518d dfs() \u7684\u65f6\u5019\uff0c\u5269\u4e0b\u7684\u8def\u5f84\u90fd\u6709\u4e14\u4ec5\u6709\u4e00\u4e2a\u6700\u9ad8\u70b9\u3002\u3002\u6211\u4eec\u5bf9\u6240\u6709\u6700\u9ad8\u70b9\u4f4d u \u7684\u8def\u5f84\u90fd\u653e\u5728\u4e00\u4e2a\u5806\u91cc\u7ef4\u62a4\uff0c\u8bb0\u505a Q[u]&#8230;<br \/>\n\u90a3\u4e48\u5728\u7528\u6240\u6709\u7684 Q[u] \u7ec4\u7ec7\u4e00\u4e2a\u5806\u6765\u7ef4\u62a4\u6700\u4f18\u89e3\u3002\u3002\uff08\u7c7b\u4f3c\u52a8\u6001\u76f4\u5f84\u95ee\u9898\u7684\u70b9\u5206\u6811\u505a\u6cd5\u91cc\u6700\u6709\u7684\u5806\u3002\u3002\u3002\uff09<\/p>\n<p>\u90a3\u4e48\u6bcf\u6b21 toggle \u4e00\u4e2a\u70b9\u65f6\uff0c\u9664\u4e86\u4f1a\u5f71\u54cd Q[u].. \u8fd8\u4f1a\u5f71\u54cd u \u5411\u4e0a\u7684\u4e00\u6761\u8def\u5f84\u3002\u3002<br \/>\n\u8bb0\u5f55\u90a3\u6761\u8def\u5f84\u6240\u5728\u7684\u5806\u4e3a Q[y]\uff0c\u90a3\u4e48\u5982\u679c\u73b0\u5728 Q[y] \u5305\u542b\u5728 ans \u91cc\u65f6\uff0c\u66f4\u65b0 Q[y] \u524d\u540e\u8fd8\u8981\u987a\u624b\u66f4\u65b0\u4e00\u4e0b ans\u3002<br \/>\n\u65e0\u8bba\u90a3\u79cd\u60c5\u51b5\uff0c\u6211\u4eec\u73b0\u5728\u90fd\u53ef\u4ee5 O(1) \u7684 push()\u3001pop() \u64cd\u4f5c\u8fdb\u884c\u5904\u7406\u3002\u3002<\/p>\n<p>\u90a3\u4e48\u6211\u4eec\u53ea\u9700\u8981\u5b9e\u73b0\u4e00\u4e2a\u53ef\u5220\u9664\u5806\u5373\u53ef\u3002\u3002\u3002stl \u91cc\u7684\u5806\u81ea\u7136\u662f\u4e0d\u53ef\u5b9a\u5411\u5220\u9664\u7684\u3002\u3002\u3002<br \/>\n\u6240\u4ee5\u73b0\u5728\u4e3b\u6d41\u7684 Dijkstra \u6a21\u677f\u91cc\u8981\u6c42\u53ef\u5220\u9664\u5806\u7684\u65f6\u5019\u3002\u3002\u5176\u5b9e\u6211\u4eec\u90fd\u662f\u505a\u5ef6\u8fdf\u5904\u7406\u3002\u3002\uff08\u5c3d\u7ba1 clrs \u91cc\u5176\u5b9e\u6709\u53ef\u5220\u9664\u5806\u7684\u76f8\u5173\u4ee3\u7801\u3002\uff09<\/p>\n<p>\u8fd9\u91cc\u6211\u4eec\u4e5f\u53ef\u4ee5\u7528\u7c7b\u4f3c\u7684\u65b9\u6cd5\u3002\u3002\u3002<br \/>\n\u5bf9\u4e8e\u6bcf\u4e2a\u5806\uff0c\u518d\u5f00\u4e00\u4e2a\u8f85\u52a9\u5806\uff0c\u8bb0\u5f55\u5220\u9664\u8fc7\u7684\u503c\u5373\u53ef\u3002\u3002<\/p>\n<pre class=\"brush: cpp; light: false; title: ; toolbar: true; notranslate\" title=\"\">\r\n#include &lt;lastweapon\/io&gt;\r\nusing namespace lastweapon;\r\n\r\nconst int N = int(2e5) + 9;\r\n\r\nstruct heap {\r\n\tpriority_queue&lt;LL&gt;Q, q;\r\n\tvoid push(LL x){if(x)Q.push(x);}\r\n\tvoid pop(LL x){if(x)q.push(x);}\r\n\tLL top() {\r\n\t\twhile(!q.empty() &amp;&amp; Q.top() == q.top()) Q.pop(), q.pop();\r\n\t\tif (Q.empty()) return 0;\r\n\t\treturn Q.top();\r\n\t}\r\n};\r\n\r\nvector&lt;pair&lt;int, PII&gt;&gt; adj&#x5B;N]; map&lt;int, int&gt; sub&#x5B;N]; heap Q&#x5B;N], ans;\r\nLL W&#x5B;N]; bool path&#x5B;N], bad&#x5B;N]; int top&#x5B;N], upc&#x5B;N], del&#x5B;N];\r\n \/\/ \u989c\u8272\u7684\u6743\u503c\u548c\uff0c\u8be5\u989c\u8272\u662f\u5426\u7ec4\u6210\u8def\u5f84\uff0c\u8282\u70b9\u662f\u5426\u5df2\u635f\u574f\uff0c\u989c\u8272\u7684\u6700\u9ad8\u4ee3\u8868\u70b9\uff0c\u70b9\u5230\u7236\u4eb2\u7684\u8fb9\u7684\u989c\u8272\uff0c\u9664\u6700\u9ad8\u70b9\u5916\u8def\u5f84\u88ab\u963b\u585e\u7684\u6b21\u6570\r\nint n;\r\n\r\n\r\n\r\nvoid dfs(int u = 1, int p = 0) {\r\n    for (auto e: adj&#x5B;u]) {\r\n        int v = e.fi; if (v == p) continue;\r\n        int w = e.se.fi, c = e.se.se;\r\n        if (!top&#x5B;c]) top&#x5B;c] = u;\r\n        upc&#x5B;v] = c; dfs(v, u);\r\n    }\r\n}\r\n\r\nvoid fix(int x) {\r\n    if (bad&#x5B;x]) {\r\n        ans.pop(Q&#x5B;x].top());\r\n        if (x != 1) {\r\n            int c = upc&#x5B;x]; if (!path&#x5B;c]) return;\r\n            if (del&#x5B;c]++) return;\r\n            int y = top&#x5B;c];\r\n            if (!bad&#x5B;y]) ans.pop(Q&#x5B;y].top());\r\n            Q&#x5B;y].pop(W&#x5B;c]);\r\n            if (!bad&#x5B;y]) ans.push(Q&#x5B;y].top());\r\n        }\r\n\r\n    } else {\r\n        ans.push(Q&#x5B;x].top());\r\n        if (x != 1) {\r\n            int c = upc&#x5B;x]; if (!path&#x5B;c]) return;\r\n            if (--del&#x5B;c]) return;\r\n            int y = top&#x5B;c];\r\n            if (!bad&#x5B;y]) ans.pop(Q&#x5B;y].top());\r\n            Q&#x5B;y].push(W&#x5B;c]);\r\n            if (!bad&#x5B;y]) ans.push(Q&#x5B;y].top());\r\n        }\r\n    }\r\n}\r\n\r\nint main() {\r\n#ifndef ONLINE_JUDGE\r\n     freopen(&quot;in.txt&quot;, &quot;r&quot;, stdin);\r\n#endif\r\n    int q; RD(n, q); DO(n-1) {\r\n\t\tint x, y, w, c; RD(x, y, w, c); ++sub&#x5B;c]&#x5B;x]; ++sub&#x5B;c]&#x5B;y]; W&#x5B;c] += w;\r\n\t\tadj&#x5B;x].PB({y, {w, c}}); adj&#x5B;y].PB({x, {w, c}});\r\n\t}\r\n\r\n\tdfs();\r\n\r\n    REP_1(c, n) if (W&#x5B;c]) {\r\n        int c1 = 0; path&#x5B;c] = 1;\r\n        for (auto e: sub&#x5B;c]) {\r\n            if (e.se == 1) ++c1;\r\n            else if (e.se != 2) {\r\n                path&#x5B;c] = 0;\r\n                break;\r\n            }\r\n        }\r\n        path&#x5B;c] &amp;= c1 == 2;\r\n        if (path&#x5B;c]) Q&#x5B;top&#x5B;c]].push(W&#x5B;c]);\r\n    }\r\n\r\n    REP_1(i, n) ans.push(Q&#x5B;i].top());\r\n\r\n    DO(q) {\r\n\t    int p, x; RD(p, x); bad&#x5B;x] = !p; fix(x);\r\n        printf(&quot;%lld\\n&quot;, ans.top());\r\n    }\r\n}\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/codeforces.com\/contest\/1787 TypeDB Forces 2023 (Div. 1 + Div. 2) C (dp) D(dfs), \u4e25\u683c\u9e3d TypeDB Forces 2023 (Div. 1 + Div. 2, Rated, Prizes!), Immortality Problem G. Colorful Tree Again \u9898\u610f\uff1a\u7ed9\u5b9a\u4e00\u4e2a\u8fb9\u6743\u56fe\uff0c\u521d\u59cb\u56fa\u5b9a\u6bcf\u4e2a\u8fb9\u6709\u989c\u8272\u548c\u8fb9\u6743\u3002\u6bcf\u4e2a\u8282\u70b9\u4e5f\u6709\u989c\u8272\uff0c\u521d\u59cb\u90fd\u662f\u9ed1\u8272\u3002 \u6bcf\u6b21\u64cd\u4f5c\u53ef\u4ee5\u6539\u53d8\u4e00\u4e2a\u8282\u70b9\u7684\u989c\u8272\uff0c\u5e76\u8be2\u95ee\u5f53\u524d\u6700\u5927\u7684\u597d\u7684\u8def\u5f84\u7684\u6743\u503c\u548c\u662f\u591a\u5c11\u3002 \u4e00\u4e2a\u8def\u5f84\u662f\u597d\u7684\uff0c\u5f53\u4e14\u4ec5\u5f53\u8def\u5f84\u4e0a\u7684\u6240\u6709\u70b9\u989c\u8272\u76f8\u540c\uff0c\u5e76\u5305\u542b\u4e86\u6240\u6709\u8be5\u989c\u8272\u7684\u8fb9\uff0c\u4e14\u8def\u5f84\u4e0a\u7ecf\u8fc7\u7684\u6240\u6709\u8282\u70b9\u7684\u989c\u8272\u4e3a\u767d\u3002 \u89e3\u6cd5\uff1a\u8fd9\u4e2a\u9898\u9996\u5148\u7b2c\u4e00\u53cd\u5e94\u662f QTREE 4 \u548c QTREE 6 \u7684\u67d0\u79cd\u7ed3\u5408\u3002\u3002\u3002 \u4f46\u662f\u4ed4\u7ec6\u8bfb\u9898\u53d1\u73b0\u5224\u5b9a\u7684\u6761\u4ef6\u662f path \u800c\u4e0d\u662f component \uff01\uff01\uff01 \u56e0\u800c\u9898\u76ee\u96be\u5ea6\u5927\u5e45\u7b80\u5316\u3002\u3002\u3002\u4f46\u662f\u60f3\u5199\u5f97\u7b80\u6d01\u4f9d\u7136\u4f1a\u5f88\u56f0\u96be\u3002\u3002\u3002 \u9996\u5148\u7b2c\u4e00\u6b65\u5f53\u7136\u662f\u5254\u9664\u4e0d\u5408\u6cd5\u7684\u989c\u8272\u3002\u3002\u3002\uff08\u6700\u597d\u7528\u5ea6\u6570\u505a\u7b80\u5355\u7684\u5224\u5b9a\uff0c\u4e0d\u8981\u5728 dfs() \u7684\u65f6\u5019\u5224\u5b9a\u3002\u3002\uff09 \u7136\u540e\u518d dfs() \u7684\u65f6\u5019\uff0c\u5269\u4e0b\u7684\u8def\u5f84\u90fd\u6709\u4e14\u4ec5\u6709\u4e00\u4e2a\u6700\u9ad8\u70b9\u3002\u3002\u6211\u4eec\u5bf9\u6240\u6709\u6700\u9ad8\u70b9\u4f4d u \u7684\u8def\u5f84\u90fd\u653e\u5728\u4e00\u4e2a\u5806\u91cc\u7ef4\u62a4\uff0c\u8bb0\u505a Q[u]&#8230; \u90a3\u4e48\u5728\u7528\u6240\u6709\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[1],"tags":[],"class_list":["post-2316","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2tdP7-Bm","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/2316","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/comments?post=2316"}],"version-history":[{"count":1,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/2316\/revisions"}],"predecessor-version":[{"id":2317,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/2316\/revisions\/2317"}],"wp:attachment":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/media?parent=2316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/categories?post=2316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/tags?post=2316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}