{"id":1112,"date":"2024-08-19T09:00:00","date_gmt":"2024-08-19T01:00:00","guid":{"rendered":"https:\/\/seanxd.com\/?p=1112"},"modified":"2024-07-16T23:19:05","modified_gmt":"2024-07-16T15:19:05","slug":"zerojudge-a552","status":"publish","type":"post","link":"https:\/\/seanxd.com\/en\/zerojudge-a552\/","title":{"rendered":"ZeroJudge A552: Model"},"content":{"rendered":"\n\n\n<p>\u4f60\u8cb7\u4e86\u4e00\u7d44\u6a21\u578b\uff0c\u5176\u4e2d\u6709 N \u500b\u96f6\u4ef6\uff0c\u800c\u96f6\u4ef6\u4e4b\u9593\u6709\u5148\u5f8c\u95dc\u4fc2\uff0c\u4e5f\u5c31\u662f\u5728\u88dd\u4e0a\u96f6\u4ef6 y \u4e4b\u524d\uff0c\u5fc5\u9808\u5148\u5c07\u96f6\u4ef6 x \u88dd\u4e0a\u624d\u53ef\u4ee5\uff0c\u8aaa\u660e\u66f8\u4e0a\u5305\u542b\u4e86 m \u7a2e (x, y) \u7684\u95dc\u4fc2\uff0c\u8acb\u554f\u4f60\u8a72\u5982\u4f55\u4f9d\u5e8f\u4f7f\u7528\u96f6\u4ef6\u624d\u80fd\u5c07\u6a21\u578b\u5b8c\u6210\uff1f<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7bc4\u4f8b\u6e2c\u8cc7<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7bc4\u4f8b\u8f38\u5165<\/th><th>\u7bc4\u4f8b\u8f38\u51fa<\/th><\/tr><\/thead><tbody><tr><td>\u672c\u984c\u63a1\u53d6\u5faa\u74b0\u8f38\u5165\uff0c\u8b80\u81f3 EOF \u6642\u7d50\u675f\u7a0b\u5f0f\u3002<br>\u6bcf\u7b46\u6e2c\u8cc7\u6703\u5148\u6709\u5169\u500b\u6578\u5b57 N \u548c M \u4ee3\u8868 N \u500b\u96f6\u4ef6\u548c M \u7a2e\u95dc\u4fc2\uff0c\u6bcf\u7a2e\u95dc\u4fc2\u53ea\u6703\u51fa\u73fe\u4e00\u6b21\uff0cN &lt;= 100\u3002 \u63a5\u4e0b\u4f86\u6709 M \u884c\uff0c\u6bcf\u884c\u6709\u5169\u500b\u6578\u5b57 i \u548c j\uff0c\u4ee3\u8868\u5fc5\u9808\u5148\u5c07 i \u88dd\u4e0a\u5f8c\uff0c\u624d\u80fd\u5c07 j \u88dd\u4e0a\u53bb (i -> j)\u3002<br>\u672c\u984c\u5fc5\u5b9a\u6709\u89e3\uff0c\u5373\u4e0d\u6703\u6709\u5faa\u74b0\u51fa\u73fe (\u4f8b\u5982 1 -> 2, 2 -> 3, 3 -> 1)\u3002<\/td><td>\u8f38\u51fa\u4e00\u884c\u6578\u5217\u5171\u6709 N \u500b\u6578\u5b57\uff0c\u8868\u793a\u8a72\u4f9d\u5e8f\u4f7f\u7528\u54ea\u4e9b\u96f6\u4ef6\u3002<br>\u5982\u679c\u540c\u6642\u6709\u591a\u7a2e\u89e3\uff0c\u8acb\u56de\u7b54\u5b57\u5178\u5e8f\u6700\u5c0f\u7684\u89e3\u3002(\u6709\u591a\u7a2e\u96f6\u4ef6\u80fd\u540c\u6642\u9078\u7684\u8a71\uff0c\u8981\u5148\u9078\u7de8\u865f\u6700\u5c0f\u7684)<\/td><\/tr><tr><td>5 4<br>0 4<br>4 3<br>2 1<br>3 2<br><br>3 2<br>2 0<br>1 0<\/td><td>0 4 3 2 1<br>1 2 0<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">ZeroJudge A552 \u7bc4\u4f8b\u6e2c\u8cc7<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\u984c\u601d\u8def<\/h2>\n\n\n\n<p>\u4f7f\u7528 Map&lt;int, Map&lt;int, int>> \u4f86\u7d00\u9304\u6bcf\u4e00\u500b\u96f6\u4ef6\u7684\u524d\u5f8c\u6709\u54ea\u4e9b\u96f6\u4ef6\u3002\u6536\u5b8c\u8cc7\u6599\u4e4b\u5f8c\u8dd1\u8ff4\u5708\u5224\u65b7\u54ea\u4e00\u500b\u96f6\u4ef6\u6c92\u6709\u524d\u9762\u6240\u9700\u7684\u96f6\u4ef6\uff0c\u5c31\u5ba3\u544a\u4e00\u500b Map&lt;int, int> \u4f86\u7d00\u9304\u7b49\u4e00\u4e0b BFS \u7684\u8d77\u9ede\uff0c\u5c07\u5176\u7684 Map \u503c ++\u3002\u8dd1\u5b8c\u8ff4\u5708\u4e4b\u5f8c\u5c31\u8dd1 BFS\u3002<\/p>\n\n\n\n<p>\u5728 BFS \u4e2d\uff0c\u6703\u9700\u8981\u4e00\u500b Map&lt;int, int> start \u4f86\u7d00\u9304\u8d77\u9ede\uff0c\u4e26\u4e14\u5ba3\u544a\u4e00\u500b Map&lt;int, int> push \u4f86\u7d00\u9304\u5df2\u7d93\u4f7f\u7528\u904e\u54ea\u4e9b\u9ede\u4e86\u3002\u5ba3\u544a\u4e00\u500b While \u8ff4\u5708\uff0c\u7576 start.size() == 0 \u6642\u5c31\u7d50\u675f\u8ff4\u5708\uff0c\u88e1\u9762\u8981\u5224\u65b7 start \u7684\u7b2c\u4e00\u500b\u8cc7\u6599\u662f\u4ec0\u9ebc\uff0c\u4e26\u4e14\u5c07\u5176\u522a\u9664\uff0c\u9084\u9700\u5224\u65b7\u5176\u7684\u524d\u9762\u9084\u6709\u6c92\u6709\u9700\u8981\u7528\u7684\u96f6\u4ef6\u9084\u6c92\u7528\u3002\u5982\u679c\u90fd\u901a\u904e\u7684\u8a71\u5c31\u5c07\u5176\u8f38\u51fa\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u7bc4\u4f8b\u7a0b\u5f0f\u78bc\uff0d<a href=\"https:\/\/zerojudge.tw\/ShowProblem?problemid=a552\" target=\"_blank\" rel=\"noreferrer noopener\">ZeroJudge A552: \u6a21\u578b<\/a><\/h3>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-cpp\" data-lang=\"C++\"><code>#include &lt;iostream&gt;\n#include &lt;map&gt;\nusing namespace std;\n\nmap&lt;int, map&lt;int, int&gt;&gt;MAP, before;\nmap&lt;int, int&gt;walk;\n\nvoid BFS(map&lt;int, int&gt;start) {\n    map&lt;int, int&gt;push;\n    while(start.size() &gt; 0) {\n        const auto first = *start.begin();\n        start.erase(start.begin());\n        const int num = first.first;\n        bool ok = true;\n        for (auto it:before[num]) {\n            if (walk[it.first] == 0) {\n                ok = false;\n                break;\n            }\n        }\n        if (!ok) {\n            push[num] = 0;\n            continue;\n        }\n        cout &lt;&lt; num &lt;&lt; &quot; &quot;;\n        walk[num]++;\n        const map&lt;int, int&gt; mm = MAP[num];\n        for (auto it:mm) {\n            if (walk[it.first] == 0 && push[it.first] == 0) {\n                push[it.first]++;\n                start[it.first]++;\n            }\n        }\n    }\n}\n\nint main() {\n    cin.sync_with_stdio(0);\n    cin.tie(0);\n    int N, M;\n    while (cin &gt;&gt; N &gt;&gt; M) {\n        walk.clear();\n        MAP.clear();\n        before.clear();\n        map&lt;int, int&gt;dad;\n        for (int i = 0; i&lt;M; i++) {\n            int a, b;\n            cin &gt;&gt; a &gt;&gt; b;\n            dad[b]++;\n            before[b][a]++;\n            MAP[a][b]++;\n        }\n        map&lt;int, int&gt;start;\n        for (int i = 0; i&lt;N; i++) {\n            if (dad[i] == 0) {\n                start[i]++;\n            }\n        }\n        BFS(start);\n        cout &lt;&lt; &quot;\\n&quot;;\n    }\n}\n\n\/\/ZeroJudge A552\n\/\/Dr. SeanXD<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u4f60\u8cb7\u4e86\u4e00\u7d44\u6a21\u578b\uff0c\u5176\u4e2d\u6709 N \u500b\u96f6\u4ef6\uff0c\u800c\u96f6\u4ef6\u4e4b\u9593\u6709\u5148\u5f8c\u95dc\u4fc2\uff0c\u4e5f\u5c31\u662f\u5728\u88dd\u4e0a\u96f6\u4ef6 y \u4e4b\u524d\uff0c\u5fc5\u9808\u5148\u5c07\u96f6\u4ef6 x \u88dd\u4e0a\u624d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","footnotes":""},"categories":[29],"tags":[14,8,9],"class_list":["post-1112","post","type-post","status-publish","format-standard","hentry","category-zerojudge-","tag-map","tag-8","tag-9"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/1112","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/comments?post=1112"}],"version-history":[{"count":2,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/1112\/revisions"}],"predecessor-version":[{"id":1114,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/1112\/revisions\/1114"}],"wp:attachment":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/media?parent=1112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/categories?post=1112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/tags?post=1112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}