{"id":1133,"date":"2024-08-25T09:00:00","date_gmt":"2024-08-25T01:00:00","guid":{"rendered":"https:\/\/seanxd.com\/?p=1133"},"modified":"2024-07-26T15:33:22","modified_gmt":"2024-07-26T07:33:22","slug":"zerojudge-e999","status":"publish","type":"post","link":"https:\/\/seanxd.com\/zh\/zerojudge-e999\/","title":{"rendered":"ZeroJudge E999: \u5de8\u984d\u734e\u91d1 (Bonus)"},"content":{"rendered":"\n\n\n<p>\u67d0\u6200\u611b\u904a\u6232\u88fd\u4f5c\u516c\u53f8\u70ba\u4e86\u6176\u795d\u5341\u5468\u5e74\uff0c\u7279\u5225\u5728\u7db2\u8def\u4e0a\u767c\u5e03\u4e86\u4e00\u500b\u904a\u6232\uff0c\u8ab0\u80fd\u901a\u95dc\u62ff\u5230\u6700\u4f73\u7d50\u5c40\uff0c\u5c31\u80fd\u7372\u5f97\u65b0\u53f0\u5e63\u4e00\u5343\u842c\u7684\u5de8\u984d\u734e\u91d1\u3002\u904a\u6232\u7684\u5167\u5bb9\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>\u4e00\u500b\u7537\u5b69\u9047\u898b\u4e86\u5fc3\u5100\u7684\u5c0d\u8c61\uff0c\u53c3\u8cfd\u8005\u8981\u5e6b\u52a9\u9019\u500b\u7537\u5b69\u5728\u6bcf\u500b\u968e\u6bb5\u9032\u884c\u4e8b\u4ef6\u9078\u64c7\uff0c\u9078\u64c7\u5f8c\u6703\u9032\u5165\u4e0b\u4e00\u500b\u968e\u6bb5\uff0c\u7136\u5f8c\u518d\u9032\u884c\u9078\u64c7 \u2026\u2026 \u76f4\u81f3\u7d50\u5c40\u968e\u6bb5\u3002\u7d50\u5c40\u70ba\u4f55\uff0c\u53d6\u6c7a\u65bc\u73a9\u5bb6\u6240\u7d93\u6b77\u7684\u968e\u6bb5\u548c\u4e8b\u4ef6\u3002\u6240\u6709\u7684\u7d50\u5c40\u4e2d\uff0c\u53ea\u6709\u4e00\u500b\u8def\u7dda\u80fd\u9054\u6210\u6700\u4f73\u7d50\u5c40\u3002<\/p>\n\n\n\n<p>\u7919\u65bc\u6210\u672c\u8003\u91cf\uff0c\u516c\u53f8\u6c7a\u5b9a\u5c0d\u65bc\u6bcf\u500b\u5831\u540d\u7684\u4eba\u6536\u8cbb\u3002\u4ed6\u5011\u5c0d\u6536\u8cbb\u7684\u91d1\u984d\u9032\u884c\u8a0e\u8ad6\uff0c\u6700\u5f8c\u8a0e\u8ad6\u51fa\u4e86\u4e00\u9023\u4e32\u8907\u96dc\u7684\u6578\u5b78\u5f0f\u5b50\u4f5c\u70ba\u8cbb\u7528\u7684\u8a08\u7b97\u516c\u5f0f\uff0c\u5176\u4e2d\u904a\u6232\u9032\u884c\u7684\u7e3d\u8def\u7dda\u6578\u662f\u6700\u95dc\u9375\u7684\u6578\u503c\u3002\u4f60\u662f\u516c\u53f8\u7684\u4e00\u540d\u54e1\u5de5\uff0c\u4f60\u5fc5\u9808\u8ca0\u8cac\u7b97\u51fa\u8def\u7dda\u7e3d\u6578 R\u3002<\/p>\n\n\n\n<p>\u904a\u6232\u7684\u8a2d\u5b9a\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u904a\u6232\u5171\u6709 N \u500b\u968e\u6bb5\uff0cS<sub>0<\/sub> \u70ba\u521d\u59cb\u968e\u6bb5\uff0cS<sub>N\uff0d1<\/sub> \u70ba\u7d50\u5c40\u968e\u6bb5\u3002<\/li>\n\n\n\n<li>\u5c0d\u6bcf\u500b\u968e\u6bb5 S<sub>i<\/sub>\uff0c\u4e8b\u4ef6 E<sub>ij<\/sub> \u6703\u8b93\u73a9\u5bb6\u9032\u5165\u5230\u968e\u6bb5 S<sub>j<\/sub>\u3002<\/li>\n\n\n\n<li>\u5c0d\u65bc\u4efb\u610f\u968e\u6bb5\uff0c\u73a9\u5bb6\u6700\u591a\u50c5\u80fd\u7d93\u6b77\u4e00\u6b21\u3002\u5373\uff1a\u4e0d\u6703\u6709\u4efb\u4f55\u4e8b\u4ef6\u53ef\u4ee5\u8b93\u73a9\u5bb6\u56de\u5230\u66fe\u7d93\u7d93\u6b77\u904e\u7684\u968e\u6bb5\u3002<\/li>\n\n\n\n<li>\u4e0d\u7ba1\u4e2d\u9593\u7684\u4e8b\u4ef6\u9078\u64c7\u662f\u4ec0\u9ebc\uff0c\u7e3d\u662f S<sub>0<\/sub> \u958b\u59cb\uff0cS<sub>N\uff0d1<\/sub> \u7d50\u675f\u3002<\/li>\n\n\n\n<li>\u8def\u7dda\u7e3d\u6578 R \u4e0d\u70ba 0\u3002<\/li>\n\n\n\n<li>\u4e0d\u6703\u6709\u91cd\u8907\u7684\u91cd\u4ef6\uff08\u5169\u500b\u968e\u6bb5\u81f3\u591a\u53ea\u7531\u4e00\u500b\u4e8b\u4ef6\u9023\u63a5\uff09\u3002<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"906\" height=\"472\" loading=\"lazy\" src=\"https:\/\/seanxd.com\/wp-content\/uploads\/2024\/07\/image-3.png\" alt=\"ZeroJudge E999 \u984c\u76ee\u89e3\u8aaa\" class=\"wp-image-1134\" srcset=\"https:\/\/seanxd.com\/wp-content\/uploads\/2024\/07\/image-3.png 906w, https:\/\/seanxd.com\/wp-content\/uploads\/2024\/07\/image-3-300x156.png 300w, https:\/\/seanxd.com\/wp-content\/uploads\/2024\/07\/image-3-768x400.png 768w, https:\/\/seanxd.com\/wp-content\/uploads\/2024\/07\/image-3-18x9.png 18w, https:\/\/seanxd.com\/wp-content\/uploads\/2024\/07\/image-3-50x26.png 50w, https:\/\/seanxd.com\/wp-content\/uploads\/2024\/07\/image-3-100x52.png 100w\" sizes=\"auto, (max-width: 906px) 100vw, 906px\" \/><\/figure>\n\n\n\n<p>\u4ee5\u4e0a\u5716\u70ba\u4f8b\uff0c\u9019\u500b\u904a\u6232\u5171\u6709 7 \u500b\u4e0d\u540c\u8def\u7dda\u53ef\u4ee5\u5230\u9054 S<sub>N\uff0d1<\/sub>\u3002<\/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>\u6bcf\u7d44\u6e2c\u8cc7\u7b2c\u4e00\u5217\u6709\u5169\u500b\u6b63\u6574\u6578 N \u53ca E\uff0cN \u8868\u793a\u968e\u6bb5\u7684\u6578\u91cf\uff0c\u968e\u6bb5\u5206\u5225\u7de8\u865f<br>0 \u81f3 N\uff0d1\uff0cE\uff08S\uff0d1 &lt;= E &lt;= 2\u00d7S\uff09\u70ba\u4e8b\u4ef6\u7684\u6578\u91cf\u3002\u63a5\u8457\u6709 E \u5217\uff0c\u6bcf\u5217\u6709\u5169\u500b\u6574\u6578<br>i\u3001j\uff0c\u8868\u793a\u968e\u6bb5 S<sub>i<\/sub> \u5b58\u5728\u4e8b\u4ef6 E<sub>ij<\/sub> \u80fd\u8b93\u73a9\u5bb6\u9032\u5165\u968e\u6bb5 S<sub>j<\/sub>\u3002<\/td><td>\u6bcf\u7d44\u6e2c\u8cc7\u8f38\u51fa\u4e00\u500b\u6b63\u6574\u6578 R\uff0c\u70ba\u8def\u7dda\u7e3d\u6578\u3002<\/td><\/tr><tr><td>9 12<br>0 1<br>0 2<br>0 3<br>0 5<br>0 7<br>3 4<br>1 4<br>4 6<br>7 6<br>6 8<br>2 8<br>5 8<\/td><td>5<\/td><\/tr><tr><td>10 14<br>0 1<br>0 2<br>1 3<br>1 4<br>1 5<br>1 6<br>2 6<br>3 9<br>4 9<br>5 9<br>6 7<br>6 8<br>7 9<br>8 9<\/td><td>7<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">ZeroJudge E999 \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 BFS\uff0c\u4e26\u4e14\u4f7f\u7528 Map \u4f86\u7d00\u9304\u6bcf\u4e00\u500b\u9ede\u53ef\u4ee5\u524d\u5f80\u7684\u9ede\u3002\u5ba3\u544a\u4e00\u500b ans \u9663\u5217\u9810\u8a2d\u70ba 0\uff0c\u4e26\u4e14\u5c07 ans[0] \u8a2d\u70ba 1\u3002<\/p>\n\n\n\n<p>\u5728 BFS \u4e2d\uff0c\u5c07\u4e0b\u4e00\u500b\u9ede\u7684 ans += \u9019\u500b\u9ede\u7684 ans\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8981\u7d00\u9304\u6bcf\u4e00\u500b\u9ede\u524d\u9762\u6709\u5e7e\u500b\u9ede\u9700\u8981\u5148\u8d70\u904e\uff0c\u6bcf\u8d70\u5230\u4e00\u500b\u9ede\u7684\u6642\u5019\u5c31\u5c07\u5176\u524d\u9762\u7684\u9ede\u7684\u6578\u91cf&#8211;\uff0c\u5982\u679c\u9019\u500b\u9ede\u524d\u9762\u7684\u9ede\u90fd\u5df2\u7d93\u8d70\u5b8c\u4e4b\u5f8c\u624d\u5c07\u5176\u4f5c\u70ba\u4e0b\u4e00\u6b21 BFS \u7684\u8d77\u9ede\u3002<\/p>\n\n\n\n<p>\u7b54\u6848\u5c31\u662f ans[N-1]\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u7bc4\u4f8b\u7a0b\u5f0f\u78bc\uff0d<a href=\"https:\/\/zerojudge.tw\/ShowProblem?problemid=e999\" target=\"_blank\" rel=\"noreferrer noopener\">ZeroJudge E999: \u5de8\u984d\u734e\u91d1 (Bonus)<\/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;vector&gt;\n#include &lt;map&gt;\nusing namespace std;\n\nmap&lt;int, vector&lt;int&gt;&gt;MAP;\nmap&lt;int, int&gt;ans, previous;\n\npair&lt;int, int&gt; p (const int a, const int b) {\n    pair&lt;int, int&gt;tmp;\n    tmp.first = a;\n    tmp.second = b;\n    return tmp;\n}\n\nvoid BFS(const vector&lt;int&gt;start) {\n    if (start.size() == 0) return;\n    vector&lt;int&gt;newStart;\n    for (int i = 0; i&lt;start.size(); i++) {\n        const int current = start[i];\n        for (int j = 0 ; j&lt;MAP[current].size(); j++) {\n            const int next = MAP[current][j];\n            previous[next]--;\n            ans[next] += ans[current];\n            if (previous[next] == 0) newStart.push_back(next);\n        }\n    }\n    BFS(newStart);\n}\n\nint main() {\n    cin.sync_with_stdio(0);\n    cin.tie(0);\n    int N, E;\n    cin &gt;&gt; N &gt;&gt; E;\n    for (int i = 0; i&lt;E; i++) {\n        int a, b;\n        cin &gt;&gt; a &gt;&gt; b;\n        MAP[a].push_back(b);\n        previous[b]++;\n    }\n    ans[0] = 1;\n    vector&lt;int&gt;start;\n    start.push_back(0);\n    BFS(start);\n    cout &lt;&lt; ans[N-1] &lt;&lt; &quot;\\n&quot;;\n}\n\n\/\/ZeroJudge E999\n\/\/Dr. SeanXD<\/code><\/pre><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u67d0\u6200\u611b\u904a\u6232\u88fd\u4f5c\u516c\u53f8\u70ba\u4e86\u6176\u795d\u5341\u5468\u5e74\uff0c\u7279\u5225\u5728\u7db2\u8def\u4e0a\u767c\u5e03\u4e86\u4e00\u500b\u904a\u6232\uff0c\u8ab0\u80fd\u901a\u95dc\u62ff\u5230\u6700\u4f73\u7d50\u5c40\uff0c\u5c31\u80fd\u7372\u5f97\u65b0\u53f0\u5e63\u4e00\u5343\u842c\u7684\u5de8\u984d [&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":[25],"tags":[30,14,23,20,8,9],"class_list":["post-1133","post","type-post","status-publish","format-standard","hentry","category-ioi-apcs","tag-bfs","tag-map","tag-pair","tag-20","tag-8","tag-9"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/seanxd.com\/zh\/wp-json\/wp\/v2\/posts\/1133","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/seanxd.com\/zh\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/seanxd.com\/zh\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/seanxd.com\/zh\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/seanxd.com\/zh\/wp-json\/wp\/v2\/comments?post=1133"}],"version-history":[{"count":5,"href":"https:\/\/seanxd.com\/zh\/wp-json\/wp\/v2\/posts\/1133\/revisions"}],"predecessor-version":[{"id":1139,"href":"https:\/\/seanxd.com\/zh\/wp-json\/wp\/v2\/posts\/1133\/revisions\/1139"}],"wp:attachment":[{"href":"https:\/\/seanxd.com\/zh\/wp-json\/wp\/v2\/media?parent=1133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/seanxd.com\/zh\/wp-json\/wp\/v2\/categories?post=1133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/seanxd.com\/zh\/wp-json\/wp\/v2\/tags?post=1133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}