{"id":86,"date":"2023-03-19T15:54:17","date_gmt":"2023-03-19T07:54:17","guid":{"rendered":"https:\/\/solost.cn\/?p=86"},"modified":"2023-03-19T15:54:17","modified_gmt":"2023-03-19T07:54:17","slug":"179","status":"publish","type":"post","link":"https:\/\/solost.cn\/?p=86","title":{"rendered":"\u62d3\u6251\u6392\u5e8f"},"content":{"rendered":"<h2>\u62d3\u6251\u6392\u5e8f\u7684\u5b9a\u4e49<\/h2>\n<p>\u5bf9\u4e00\u4e2a\u6709\u5411\u65e0\u73af\u56feG\u8fdb\u884c\u62d3\u6251\u6392\u5e8f\uff0c\u662f\u5c06G\u4e2d\u6240\u6709\u9876\u70b9\u6392\u6210\u4e00\u4e2a\u7ebf\u6027\u5e8f\u5217\uff0c\u4f7f\u5f97\u56fe\u4e2d\u4efb\u610f\u4e00\u5bf9\u9876\u70b9<code>u<\/code>\u548c<code>v<\/code>\uff0c\u82e5\u8fb9<code>(u,v)\u2208E(G)<\/code>\uff0c\u5219<code>u<\/code>\u5728\u7ebf\u6027\u5e8f\u5217\u4e2d\u51fa\u73b0\u5728<code>v<\/code>\u4e4b\u524d\u3002\u901a\u5e38\uff0c\u8fd9\u6837\u7684\u7ebf\u6027\u5e8f\u5217\u79f0\u4e3a\u6ee1\u8db3\u62d3\u6251\u6b21\u5e8f(Topological Order)\u7684\u5e8f\u5217\uff0c\u7b80\u79f0\u62d3\u6251\u5e8f\u5217\u3002\u7b80\u5355\u7684\u8bf4\uff0c\u7531\u67d0\u4e2a\u96c6\u5408\u4e0a\u7684\u4e00\u4e2a\u504f\u5e8f\u5f97\u5230\u8be5\u96c6\u5408\u4e0a\u7684\u4e00\u4e2a\u5168\u5e8f\uff0c\u8fd9\u4e2a\u64cd\u4f5c\u79f0\u4e4b\u4e3a\u62d3\u6251\u6392\u5e8f\u3002<\/p>\n<h2>\u62d3\u6251\u6392\u5e8f\u7684\u5e94\u7528<\/h2>\n<p>\u53c2\u8003\u4e00\u9053ACM\u9898\u76ee\u786e\u5b9a\u6bd4\u8d5b\u540d\u6b21<br \/>\u95ee\u9898\u63cf\u8ff0\uff1a<\/p>\n<p>\u6709N\u4e2a\u6bd4\u8d5b\u961f\uff081&lt;=N&lt;=500\uff09\uff0c\u7f16\u53f7\u4f9d\u6b21\u4e3a1\uff0c2\uff0c3\uff0c\u3002\u3002\u3002\u3002\uff0cN\u8fdb\u884c\u6bd4\u8d5b\uff0c\u6bd4\u8d5b\u7ed3\u675f\u540e\uff0c\u88c1\u5224\u59d4\u5458\u4f1a\u8981\u5c06\u6240\u6709\u53c2\u8d5b\u961f\u4f0d\u4ece\u524d\u5f80\u540e\u4f9d\u6b21\u6392\u540d\uff0c\u4f46\u73b0\u5728\u88c1\u5224\u59d4\u5458\u4f1a\u4e0d\u80fd\u76f4\u63a5\u83b7\u5f97\u6bcf\u4e2a\u961f\u7684\u6bd4\u8d5b\u6210\u7ee9\uff0c\u53ea\u77e5\u9053\u6bcf\u573a\u6bd4\u8d5b\u7684\u7ed3\u679c\uff0c\u5373P1\u8d62P2\uff0c\u7528P1\uff0cP2\u8868\u793a\uff0c\u6392\u540d\u65f6P1\u5728P2\u4e4b\u524d\u3002\u73b0\u5728\u8bf7\u4f60\u7f16\u7a0b\u5e8f\u786e\u5b9a\u6392\u540d\u3002<\/p>\n<p>\u73b0\u5df2\u77e5\u67094\u4e2a\u961f\u4f0d\u53c2\u52a0\u6bd4\u8d5b\uff0c\u800c\u6bcf\u4e24\u961f\u7684\u6bd4\u8d5b\u6210\u7ee9\u5982\u4e0b:<br \/>1 2<br \/>2 3<br \/>4 3<\/p>\n<p>1\uff0c2\u8868\u793a\u5373P1\u961f\u8d62\u4e86P2\u961f\u3002\u5148\u8ba9\u4f60\u6392\u5e8f\u51fa\u6574\u4e2a\u6bd4\u8d5b\u7684\u6392\u540d<br \/>\u8be5\u95ee\u9898\u7684\u6c42\u89e3\u8fc7\u7a0b\u5982\u4e0b\uff1a<br \/>\u5b8c\u5168\u53ef\u5c06P1\uff5eP4\u7684\u6bd4\u8d5b\u6392\u540d\u6309\u9898\u76ee\u8981\u6c42\u6784\u5efa\u51fa\u4e00\u4e2a\u6709\u5411\u56fe\uff1a<\/p>\n<p>P1 \u2014&gt; P2 \u2014&gt; P3 &lt;\u2014 P4<\/p>\n<p>\u7531\u56fe\u81ea\u7136\u975e\u5e38\u6e05\u695a\u7684\u770b\u51fa\u6392\u540d\u4e3aP1\uff0cP2\uff0cP4\uff0cP3\u6216P1\uff0cP4\uff0cP2\uff0cP3<\/p>\n<p>\u5982\u679c\u6211\u4eec\u8003\u8651\u56fe\u7684\u76f8\u5173\u6027\u8d28\u5728\u91cc\u9762\uff0c\u4f1a\u53d1\u73b0\uff1a<br \/>\u5165\u5ea6\u4e3a 0 \u7684\u9876\u70b9 P1 \u662f\u6392\u5e8f\u4e2d\u7684\u7b2c\u4e00\u540d(\u56e0\u4e3a\u6ca1\u6709\u4efb\u4f55\u4eba\u6218\u80dc\u5b83)\uff0c\u800c\u786e\u5b9a\u597d\u7b2c\u4e00\u540d\u4e4b\u540e\uff0c\u5c06\u7b2c\u4e00\u540d\u4ece\u6392\u5e8f\u4e2d\u8e22\u51fa\uff0c\u5269\u4e0b\u7684\u56fe\uff1a P2\u2013&gt; P3 &lt;\u2013 P4 \u4e2d\uff0c\u4f9d\u7136\u5b58\u5728\u5165\u5ea6\u4e3a0\u7684\u8282\u70b9\uff0c\u4f9d\u6b21\u7c7b\u63a8\uff0c\u4fbf\u5f97\u5230\u4e86\u4e00\u4e2a\u6c42\u62d3\u6251\u6392\u5e8f\u7684\u7b97\u6cd5\u3002<\/p>\n<h2>\u62d3\u6251\u6392\u5e8f\u5b58\u5728\u7684\u6761\u4ef6<\/h2>\n<p>\u5e76\u4e0d\u662f\u6240\u6709\u56fe\u90fd\u5b58\u5728\u62d3\u6251\u6392\u5e8f\uff0c\u62d3\u6251\u6392\u5e8f\u5728\u56fe\u4e2d\u4e5f\u4e0d\u662f\u552f\u4e00\u7684(\u4f8b\u5982\u4e0a\u4f8b\u4e2d\u5c06\u5b58\u5728\u4e24\u4e2a\u62d3\u6251\u6392\u5e8f)\u3002<\/p>\n<p>\u4e0d\u542b\u73af\u8def\u7684\u6709\u5411\u56fe\u5fc5\u5305\u542b\u5165\u5ea6\u4e3a\u96f6\u7684\u9876\u70b9\u2014\u56e0\u6b64\u4e00\u5b9a\u5b58\u5728\u62d3\u6251\u6392\u5e8f<\/p>\n<h2>\u5b9e\u73b0\u4ee3\u7801<\/h2>\n<pre><code class=\"lang-cpp\">int d[N];\nvector&lt;int&gt; res;\nbool topsort()\n{\n    \/\/ d[i] \u5b58\u50a8\u70b9i\u7684\u5165\u5ea6\n    queue&lt;int&gt; q;\n    for (int i = 1; i &lt;= n; i++) \n        if (!d[i]) q.push(i);\n\n    while (q.size()) {\n        auto t = q.front();\n        res.push_back(t); \/\/\u5b58\u50a8\u62d3\u6251\u5e8f\n        q.pop();\n        for (auto x : a[t]) {\n            if (--d[x] == 0) {\/\/\u5982\u679c\u4e00\u4e2a\u70b9\u7684\u5165\u5ea6\u4e3a0\uff0c\u5c06\u4e0d\u4f1a\u6709\u70b9\u518d\u904d\u5386\u5b83\uff0c\u6240\u4ee5\u53ef\u4ee5\u5f80\u4e0b\u641c\u7d22\u5b83\u4e86\n                q.push(x);\n            }\n        }\n    }\n    \/\/ \u5982\u679c\u6240\u6709\u70b9\u90fd\u5165\u961f\u4e86\uff0c\u8bf4\u660e\u5b58\u5728\u62d3\u6251\u5e8f\u5217\uff1b\u5426\u5219\u4e0d\u5b58\u5728\u62d3\u6251\u5e8f\u5217\u3002\n    return res.size() == n;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u62d3\u6251\u6392\u5e8f\u7684\u5b9a\u4e49 \u5bf9\u4e00\u4e2a\u6709\u5411\u65e0\u73af\u56feG\u8fdb\u884c\u62d3\u6251\u6392\u5e8f\uff0c\u662f\u5c06G\u4e2d\u6240\u6709\u9876\u70b9\u6392\u6210\u4e00\u4e2a\u7ebf\u6027\u5e8f\u5217\uff0c\u4f7f\u5f97\u56fe\u4e2d\u4efb\u610f\u4e00\u5bf9\u9876\u70b9u\u548cv\uff0c\u82e5\u8fb9(u,v)\u2208E(G)\uff0c\u5219u\u5728\u7ebf\u6027\u5e8f\u5217\u4e2d\u51fa\u73b0\u5728v\u4e4b\u524d\u3002\u901a\u5e38\uff0c\u8fd9\u6837\u7684\u7ebf\u6027\u5e8f\u5217\u79f0\u4e3a\u6ee1\u8db3\u62d3\u6251\u6b21\u5e8f(Topological Order)\u7684\u5e8f\u5217\uff0c\u7b80\u79f0\u62d3\u6251\u5e8f\u5217\u3002\u7b80\u5355\u7684\u8bf4\uff0c\u7531\u67d0\u4e2a\u96c6\u5408\u4e0a\u7684\u4e00\u4e2a\u504f\u5e8f\u5f97\u5230\u8be5\u96c6\u5408\u4e0a\u7684\u4e00\u4e2a\u5168\u5e8f\uff0c\u8fd9\u4e2a\u64cd\u4f5c\u79f0\u4e4b\u4e3a\u62d3\u6251\u6392\u5e8f\u3002 \u62d3\u6251\u6392\u5e8f\u7684\u5e94\u7528 \u53c2\u8003\u4e00\u9053ACM\u9898\u76ee\u786e\u5b9a\u6bd4\u8d5b\u540d\u6b21\u95ee\u9898\u63cf\u8ff0\uff1a \u6709N\u4e2a\u6bd4\u8d5b\u961f\uff081&lt;=N&lt;=500\uff09\uff0c\u7f16\u53f7\u4f9d\u6b21\u4e3a1\uff0c2\uff0c3\uff0c\u3002\u3002\u3002\u3002\uff0cN\u8fdb\u884c\u6bd4\u8d5b\uff0c\u6bd4\u8d5b\u7ed3\u675f\u540e\uff0c\u88c1\u5224\u59d4\u5458\u4f1a\u8981\u5c06\u6240\u6709\u53c2\u8d5b\u961f\u4f0d\u4ece\u524d\u5f80\u540e\u4f9d\u6b21\u6392\u540d\uff0c\u4f46\u73b0\u5728\u88c1\u5224\u59d4\u5458\u4f1a\u4e0d\u80fd\u76f4\u63a5\u83b7\u5f97\u6bcf\u4e2a\u961f\u7684\u6bd4\u8d5b\u6210\u7ee9\uff0c\u53ea\u77e5\u9053\u6bcf\u573a\u6bd4\u8d5b\u7684\u7ed3\u679c\uff0c\u5373P1\u8d62P2\uff0c\u7528P1\uff0cP2\u8868\u793a\uff0c\u6392\u540d\u65f6P1\u5728P2\u4e4b\u524d\u3002\u73b0\u5728\u8bf7\u4f60\u7f16\u7a0b\u5e8f\u786e\u5b9a\u6392\u540d\u3002 \u73b0\u5df2\u77e5\u67094\u4e2a\u961f\u4f0d\u53c2\u52a0\u6bd4\u8d5b\uff0c\u800c\u6bcf\u4e24\u961f\u7684\u6bd4\u8d5b\u6210\u7ee9\u5982\u4e0b:1 22 34 3 1\uff0c2\u8868\u793a\u5373P1\u961f\u8d62\u4e86P2\u961f\u3002\u5148\u8ba9\u4f60\u6392\u5e8f\u51fa\u6574\u4e2a\u6bd4\u8d5b\u7684\u6392\u540d\u8be5\u95ee\u9898\u7684\u6c42\u89e3\u8fc7\u7a0b\u5982\u4e0b\uff1a\u5b8c\u5168\u53ef\u5c06P1\uff5eP4\u7684\u6bd4\u8d5b\u6392\u540d\u6309\u9898\u76ee\u8981\u6c42\u6784\u5efa\u51fa\u4e00\u4e2a\u6709\u5411\u56fe\uff1a P1 \u2014&gt; P2 \u2014&gt; P3 &lt;\u2014 P4 \u7531\u56fe\u81ea\u7136\u975e\u5e38\u6e05\u695a\u7684\u770b\u51fa\u6392\u540d\u4e3aP1\uff0cP2\uff0cP4\uff0cP3\u6216P1\uff0cP4\uff0cP2\uff0cP3 \u5982\u679c\u6211\u4eec\u8003\u8651\u56fe\u7684\u76f8\u5173\u6027\u8d28\u5728\u91cc\u9762\uff0c\u4f1a\u53d1\u73b0\uff1a\u5165\u5ea6\u4e3a 0 \u7684\u9876\u70b9 P1 \u662f\u6392\u5e8f\u4e2d\u7684\u7b2c\u4e00\u540d(\u56e0\u4e3a\u6ca1\u6709\u4efb\u4f55\u4eba\u6218\u80dc\u5b83)\uff0c\u800c\u786e\u5b9a\u597d\u7b2c\u4e00\u540d\u4e4b\u540e\uff0c\u5c06\u7b2c\u4e00\u540d\u4ece\u6392\u5e8f\u4e2d\u8e22\u51fa\uff0c\u5269\u4e0b\u7684\u56fe\uff1a P2\u2013&gt; P3 &lt;\u2013 P4 \u4e2d\uff0c\u4f9d\u7136\u5b58\u5728\u5165\u5ea6\u4e3a0\u7684\u8282\u70b9\uff0c\u4f9d\u6b21\u7c7b\u63a8\uff0c\u4fbf\u5f97\u5230\u4e86\u4e00\u4e2a\u6c42\u62d3\u6251\u6392\u5e8f\u7684\u7b97\u6cd5\u3002 \u62d3\u6251\u6392\u5e8f\u5b58\u5728\u7684\u6761\u4ef6 \u5e76\u4e0d\u662f\u6240\u6709\u56fe\u90fd\u5b58\u5728\u62d3\u6251\u6392\u5e8f\uff0c\u62d3\u6251\u6392\u5e8f\u5728\u56fe\u4e2d\u4e5f\u4e0d\u662f\u552f\u4e00\u7684(\u4f8b\u5982\u4e0a\u4f8b\u4e2d\u5c06\u5b58\u5728\u4e24\u4e2a\u62d3\u6251\u6392\u5e8f)\u3002 \u4e0d\u542b\u73af\u8def\u7684\u6709\u5411\u56fe\u5fc5\u5305\u542b\u5165\u5ea6\u4e3a\u96f6\u7684\u9876\u70b9\u2014\u56e0\u6b64\u4e00\u5b9a\u5b58\u5728\u62d3\u6251\u6392\u5e8f \u5b9e\u73b0\u4ee3\u7801 int d[N]; vector&lt;int&gt; res; bool topsort() { \/\/ d[i] \u5b58\u50a8\u70b9i\u7684\u5165\u5ea6 queue&lt;int&gt; q; for (int i = 1; i &lt;= n; i++) if [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[72],"tags":[25,38],"class_list":["post-86","post","type-post","status-publish","format-standard","hentry","category-acm","tag-bfs","tag-38"],"_links":{"self":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts\/86","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=86"}],"version-history":[{"count":0,"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts\/86\/revisions"}],"wp:attachment":[{"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=86"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=86"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=86"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}