{"id":130,"date":"2023-04-16T15:21:02","date_gmt":"2023-04-16T07:21:02","guid":{"rendered":"https:\/\/solost.cn\/?p=130"},"modified":"2023-04-16T15:21:02","modified_gmt":"2023-04-16T07:21:02","slug":"237","status":"publish","type":"post","link":"https:\/\/solost.cn\/?p=130","title":{"rendered":"Manthan, Codefest 18 (rated, Div. 1 + Div. 2) &#8211; D. Valid BFS? time limit per test2 seconds"},"content":{"rendered":"<h2>\u9898\u76ee<\/h2>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2026\/11\/image-21.png\" alt=\"image.png\" title=\"image.png\"><\/p>\n<h2>\u9898\u76ee\u5927\u610f<\/h2>\n<p>\u7ed9\u4f60\u4e00\u5806\u53cc\u5411\u8fb9\uff0c\u4fdd\u8bc1\u80fd\u591f\u5f62\u6210\u4e00\u4e2a\u6811\uff0c\u518d\u7ed9\u4f60\u4e00\u4e2a\u4e00\u4e2a\u5e8f\u5217\uff0c\u95ee\u4f60\u8fd9\u4e2a\u5e8f\u5217\u548c\u7ed9\u51fa\u7684\u8fb9\u5f62\u6210\u7684\u6811\u4e00\u6837\u4e0d\uff1f<\/p>\n<h2>\u9898\u76ee\u94fe\u63a5<\/h2>\n<p><a href=\"https:\/\/codeforces.com\/problemset\/problem\/1037\/D\">\u94fe\u63a5<\/a><\/p>\n<h2>\u9898\u89e3<\/h2>\n<p>\u521a\u5f00\u59cb\u60f3\u7684\u662f\u5148bfs\uff0c\u7136\u540e\u8ba1\u7b97\u51fa\u4e0b\u4e00\u5c42\u7684\u513f\u5b50\u7684\u4f4d\u7f6e\u5728\u54ea\uff0c\u7136\u540e\u8fd9\u4e2a\u8fd9\u4e48\u592a\u96be\u5199\u4e86\uff0c\u6240\u4ee5\u7785\u4e86\u773c\u9898\u89e3\u91cd\u65b0\u5199\u4e00\u4e0b\u3002\u601d\u8def\u662f\u628abfs\u7684\u987a\u5e8f\u91cd\u65b0\u6392\u5217\u4e0b\uff0c\u6309\u7167\u7ed9\u51fa\u7684\u5e8f\u5217\u7684\u4e0b\u6807\u91cd\u65b0\u6392\u5217\u3002\u7136\u540e\u518dbfs\u4e00\u4e0b\u5c31\u884c\uff0c\u7136\u540e\u5224\u65adbfs\u51fa\u6765\u7684\u548c\u7ed9\u51fa\u6765\u7684\u6709\u4ec0\u4e48\u4e0d\u4e00\u6837\u5417\uff0c\u4e00\u6837\u7684yes,\u4e0d\u4e00\u6837no<\/p>\n<pre><code class=\"lang-cpp\">#include &lt;map&gt;\n#include &lt;set&gt;\n#include &lt;cmath&gt;\n#include &lt;queue&gt;\n#include &lt;stack&gt;\n#include &lt;vector&gt;\n#include &lt;bitset&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;algorithm&gt;\n#include &lt;unordered_map&gt;\n#include &lt;unordered_set&gt;\n\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);\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair&lt;ll, ll&gt; pll;\ntypedef pair&lt;int, int&gt; pii;\ntypedef unsigned long long ull;\n\nconst int INF = 0x3f3f3f3f, mod = 1000000007;\nconst int N = 2e5 + 10;\n\nint n;\nint b[N];\nvector&lt;int&gt; a[N], ans;\nmap&lt;int, int&gt; pos, vis;\n\nbool cmp(int a, int b) {\n    return pos[a] &lt; pos[b];\n}\n\nvoid solve() {\n    cin &gt;&gt; n;\n    for(int i = 1; i &lt; n; i++) {\n        int x, y;\n        cin &gt;&gt; x &gt;&gt; y;\n        a[x].push_back(y);\n        a[y].push_back(x);\n    }\n    for (int i = 1; i &lt;= n; i++) {\n        cin &gt;&gt; b[i];\n        pos[b[i]] = i;\n    }\n    for(int i = 1; i &lt;= n; i++) \n        sort(a[i].begin(), a[i].end(), cmp);\n\n    queue&lt;int&gt; q;\n    q.push(1);\n    ans.push_back(1);\n    vis[1] = 1;\n    while(q.size()) {\n        auto t = q.front();\n        q.pop();\n        for(auto x : a[t]) {\n            if(!vis[x]) {\n                vis[x] = 1;\n                q.push(x);\n                ans.push_back(x);\n            }\n        }\n    }\n    for(int i = 0; i &lt; n; i++) {\n        if(b[i + 1] != ans[i]) {\n            cout &lt;&lt; &quot;No\\n&quot;;\n            return;\n        }\n    }\n    cout &lt;&lt; &quot;Yes\\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>\u9898\u76ee \u9898\u76ee\u5927\u610f \u7ed9\u4f60\u4e00\u5806\u53cc\u5411\u8fb9\uff0c\u4fdd\u8bc1\u80fd\u591f\u5f62\u6210\u4e00\u4e2a\u6811\uff0c\u518d\u7ed9\u4f60\u4e00\u4e2a\u4e00\u4e2a\u5e8f\u5217\uff0c\u95ee\u4f60\u8fd9\u4e2a\u5e8f\u5217\u548c\u7ed9\u51fa\u7684\u8fb9\u5f62\u6210\u7684\u6811\u4e00\u6837\u4e0d\uff1f \u9898\u76ee\u94fe\u63a5 \u94fe\u63a5 \u9898\u89e3 \u521a\u5f00\u59cb\u60f3\u7684\u662f\u5148bfs\uff0c\u7136\u540e\u8ba1\u7b97\u51fa\u4e0b\u4e00\u5c42\u7684\u513f\u5b50\u7684\u4f4d\u7f6e\u5728\u54ea\uff0c\u7136\u540e\u8fd9\u4e2a\u8fd9\u4e48\u592a\u96be\u5199\u4e86\uff0c\u6240\u4ee5\u7785\u4e86\u773c\u9898\u89e3\u91cd\u65b0\u5199\u4e00\u4e0b\u3002\u601d\u8def\u662f\u628abfs\u7684\u987a\u5e8f\u91cd\u65b0\u6392\u5217\u4e0b\uff0c\u6309\u7167\u7ed9\u51fa\u7684\u5e8f\u5217\u7684\u4e0b\u6807\u91cd\u65b0\u6392\u5217\u3002\u7136\u540e\u518dbfs\u4e00\u4e0b\u5c31\u884c\uff0c\u7136\u540e\u5224\u65adbfs\u51fa\u6765\u7684\u548c\u7ed9\u51fa\u6765\u7684\u6709\u4ec0\u4e48\u4e0d\u4e00\u6837\u5417\uff0c\u4e00\u6837\u7684yes,\u4e0d\u4e00\u6837no #include &lt;map&gt; #include &lt;set&gt; #include &lt;cmath&gt; #include &lt;queue&gt; #include &lt;stack&gt; #include &lt;vector&gt; #include &lt;bitset&gt; #include &lt;cstdio&gt; #include &lt;cstdlib&gt; #include &lt;cstring&gt; #include &lt;fstream&gt; #include &lt;iostream&gt; #include &lt;algorithm&gt; #include &lt;unordered_map&gt; #include &lt;unordered_set&gt; #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); using [&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":[25,5,9],"class_list":["post-130","post","type-post","status-publish","format-standard","hentry","category-solution","tag-bfs","tag-5","tag-9"],"_links":{"self":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts\/130","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=130"}],"version-history":[{"count":0,"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts\/130\/revisions"}],"wp:attachment":[{"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}