{"id":240,"date":"2023-03-19T16:22:00","date_gmt":"2023-03-19T08:22:00","guid":{"rendered":"https:\/\/solost.cn\/?p=240"},"modified":"2023-05-22T19:23:56","modified_gmt":"2023-05-22T11:23:56","slug":"364","status":"publish","type":"post","link":"https:\/\/solost.cn\/?p=240","title":{"rendered":"AtCoder Beginner Contest 291 &#8211; E &#8211; Find Permutation"},"content":{"rendered":"<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2026\/11\/image-10.png\" alt=\"image.png\" title=\"image.png\"><\/p>\n<pre><code class=\"lang-cpp\">#include &lt;algorithm&gt;\n#include &lt;bitset&gt;\n#include &lt;cmath&gt;\n#include &lt;cstdio&gt;\n#include &lt;cstdlib&gt;\n#include &lt;cstring&gt;\n#include &lt;fstream&gt;\n#include &lt;iostream&gt;\n#include &lt;map&gt;\n#include &lt;queue&gt;\n#include &lt;set&gt;\n#include &lt;stack&gt;\n#include &lt;unordered_map&gt;\n#include &lt;vector&gt;\n\nusing namespace std;\n#define x first\n#define y second\n#define endl &#039;\\n&#039;\n#define IOS                       \\\n    ios_base::sync_with_stdio(0); \\\n    cin.tie(0);                   \\\n    cout.tie(0);\ntypedef long long ll;\ntypedef pair&lt;int, int&gt; PII;\ntypedef unsigned long long ull;\nconst int N = 2e5 + 10;\n\nvector&lt;int&gt; a[N];\nint n, m;\nint ans[N];\nint 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  \n    for (int i = 1; i &lt;= n; i++) {\n        if (!d[i]) {\n            q.push(i);\n        }\n    }\n  \n    while (q.size()) {\n        if(q.size() &gt; 1) return 0; \/\/\u5982\u679c\u6b64\u65f6\u7684\u5934\u6709\u4e24\u4e2a\uff0c\u5c31\u6709\u4e24\u79cd\u8d70\u6cd5\uff0c\u6240\u4ee5\u62d3\u6251\u56fe\u4e00\u5b9a\u4e0d\u552f\u4e00\n        auto t = q.front();\n        res.push_back(t);\n        \/\/ ans[t] = f--;\n        q.pop();\n        for (auto x : a[t]) {\n            if (--d[x] == 0) {\/\/\u8fd9\u91cc\u4fdd\u8bc1\u641c\u5230\u7684\u70b9\u662f\u4e00\u5b9a\u628a\u5165\u5ea6\u90fd\u53d8\u62100\uff0c\u5373\u4e0d\u53ef\u80fd\u6709\u70b9\u518d\u641c\u5230\u5b83\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    \/*\u4e3a\u4e86\u7406\u89e3\uff0c\u7ed9\u51fa\u4e0b\u9762\u6837\u4f8b\n    1 3\uff0c 3 2\uff0c2 4\uff0c2 5\uff0c5 4\u4e0d\u4f1a\u51fa\u73b0\u57282\u7684\u5730\u65b9\u4f1a\u641c\u5230\u4e24\u6761\u8def\uff0c\u56e0\u4e3a\u8fd8\u67095-4\u8fd9\u6761\u8fb9\u7684\u5b58\u5728\u3002\n    *\/\n}\n\nvoid solve()\n{\n    cin &gt;&gt; n &gt;&gt; m;\n    for (int i = 1; i &lt;= m; i++) {\n        int x, y;\n        cin &gt;&gt; x &gt;&gt; y;\n        a[y].push_back(x);\n        d[x]++;\n    }\n    \/\/ cout &lt;&lt; topsort() &lt;&lt; endl;\n    if(topsort()){\n        cout &lt;&lt; &quot;Yes\\n&quot;;\n        int t = n;\n        for(auto x : res)\n            ans[x] = t--;\n            \/\/ cout &lt;&lt; x &lt;&lt; &quot; &quot;;\n        for(int i = 1; i &lt;= n; i++)\n            cout &lt;&lt; ans[i] &lt;&lt; &quot; &quot;;\n        cout &lt;&lt; endl;\n    }\n    else cout &lt;&lt; &quot;No\\n&quot;;\n}\nint main()\n{\n    \/\/ IOS;\n    int t = 1;\n    \/\/ cin &gt;&gt; t;\n    while (t--) {\n        solve();\n    }\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>#include &lt;algorithm&gt; #include &lt;bitset&gt; #include &lt;cmath&gt; #include &lt;cstdio&gt; #include &lt;cstdlib&gt; #include &lt;cstring&gt; #include &lt;fstream&gt; #include &lt;iostream&gt; #include &lt;map&gt; #include &lt;queue&gt; #include &lt;set&gt; #include &lt;stack&gt; #include &lt;unordered_map&gt; #include &lt;vector&gt; using namespace std; #define x first #define y second #define endl &#039;\\n&#039; #define IOS \\ ios_base::sync_with_stdio(0); \\ cin.tie(0); \\ cout.tie(0); typedef long long ll; typedef pair&lt;int, int&gt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[45,38],"class_list":["post-240","post","type-post","status-publish","format-standard","hentry","category-solution","tag-45","tag-38"],"_links":{"self":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts\/240","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=240"}],"version-history":[{"count":0,"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts\/240\/revisions"}],"wp:attachment":[{"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}