{"id":109,"date":"2023-03-29T16:05:17","date_gmt":"2023-03-29T08:05:17","guid":{"rendered":"https:\/\/solost.cn\/?p=109"},"modified":"2023-03-29T16:05:17","modified_gmt":"2023-03-29T08:05:17","slug":"209","status":"publish","type":"post","link":"https:\/\/solost.cn\/?p=109","title":{"rendered":"AcWing &#8211; 1562. \u5fae\u535a\u8f6c\u53d1"},"content":{"rendered":"<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2026\/11\/image-14.png\" alt=\"image.png\" title=\"image.png\"><\/p>\n<h2>\u9898\u76ee\u94fe\u63a5<\/h2>\n<p><a href=\"https:\/\/www.acwing.com\/problem\/content\/1564\/\">\u9898\u76ee\u94fe\u63a5<\/a><\/p>\n<h2>\u9898\u89e3<\/h2>\n<h3>DFS\u7248\u672c<\/h3>\n<p>\u56e0\u4e3a\u641c\u7d22\u7684\u65f6\u5019\u4f1a\u6709\u4e00\u4e2a\u95ee\u9898\uff0c\u5982\u679c\u641c\u7d22\u7684\u65f6\u5019\u53d1\u73b0\u5230\u8fbe\u4e00\u4e2a\u70b9\u7684\u65f6\u5019\u7684\u6b65\u6570\uff0c\u6bd4\u524d\u4e00\u6b21\u8bbf\u95ee\u8fc7\u8fd9\u4e2a\u70b9\u7684\u6b65\u6570\u8fd8\u591a\uff0c\u90a3\u4e48\u5c31\u4e0d\u7528\u5f80\u4e0b\u641c\u7d22\u4e86\uff0c\u518d\u641c\u53ea\u4f1a\u589e\u52a0\u65f6\u95f4\u590d\u6742\u5ea6\u3002\u5728\u5199\u9898\u8fc7\u7a0b\u4e2d\u8fd8\u6709\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\u548c\u5c40\u90e8\u53d8\u91cf\u95ee\u9898\u3002<\/p>\n<blockquote><p>\u51fd\u6570\u4e2d\u5c40\u90e8\u53d8\u91cf\u53ea\u4f1a\u5728\u8fd9\u4e00\u5c42\u8d77\u4f5c\u7528\uff0c\u518d\u5f80\u4e0b\u9012\u5f52\u7684\u65f6\u5019\uff0c\u6bcf\u4e2a\u540c\u540d\u7684\u53d8\u91cf\u4e0d\u662f\u540c\u4e00\u4e2a\u53d8\u91cf<\/p><\/blockquote>\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 INF = 0x3f3f3f3f, mod = 1000000007;\nconst int N = 2e5 + 10;\nvector&lt;int&gt; a[1010];\nint n, d;\nint vis[1010];\nint dfs(int u, int s)\n{\n    int ans = 0;\/\/\u6b64\u65f6ans\u53ea\u5bf9\u5f53\u524d\u5c42\u8d77\u4f5c\u7528\n    if (s &gt; d || s &gt;= vis[u]) return ans;\n    \/*\n    \u5982\u679c\u6b65\u6570\u8d85\u8fc7d\u6216\u8005\u5f53\u524d\u904d\u5386\u7684\u70b9\u4e4b\u524d\u5df2\u7ecf\u904d\u5386\u8fc7\uff0c\n    \u4e14\u5f53\u524d\u904d\u5386\u7684\u6b65\u6570\u5927\u4e8e\u7b49\u4e8e\u4e4b\u524d\u904d\u5386\u7684\u6b65\u6570\uff0c\n    \u90a3\u4e48\u5c31\u6ca1\u6709\u7ee7\u7eed\u904d\u5386\u7684\u610f\u4e49\n    *\/\n    if(vis[u] == INF) ans++;\/\/\u5982\u679c\u8fd9\u4e2a\u70b9\u7b49\u4e8eINF\uff0c\u90a3\u4e48\u8bf4\u660e\u8fd9\u4e2a\u70b9\u8fd8\u6ca1\u6709\u904d\u5386\u8fc7\n    vis[u] = s;\/\/\u6b64\u65f6\u628a\u5f53\u524d\u70b9\u7684\u6b65\u6570\u66f4\u6539\u4e3a\u5230\u8fbe\u5f53\u524d\u70b9\u6240\u9700\u7684\u6b65\u6570\n    for (auto x : a[u]) {\n        ans += dfs(x, s + 1);\n    }\n    return ans;\n}\n\nvoid solve()\n{\n    cin &gt;&gt; n &gt;&gt; d;\n    for (int i = 1; i &lt;= n; i++) {\n        int t;\n        cin &gt;&gt; t;\n        for (int j = 1; j &lt;= t; j++) {\n            int x;\n            cin &gt;&gt; x;\n            a[x].push_back(i);\n        }\n    }\n    int q;\n    cin &gt;&gt; q;\n    while (q--) {\n        int x;\n        cin &gt;&gt; x;\n        memset(vis, 0x3f, sizeof(vis));\n        cout &lt;&lt; dfs(x, 0) - 1 &lt;&lt; endl;\n    }\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<h3>BFS<\/h3>\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 INF = 0x3f3f3f3f, mod = 1000000007;\nconst int N = 2e5 + 10;\nvector&lt;int&gt; a[1010];\nint n, d;\nint bfs(int u, int s)\n{\n    int ans = 0;\n    queue&lt;PII&gt; q;\n    q.push({ u, s });\n    unordered_map&lt;int, int&gt; vis;\n    vis[u] = 1;\n    while (q.size()) {\n        auto t = q.front();\n        q.pop();\n        for (auto x : a[t.x]) {\n            auto ne = t;\n            if (!vis[x]) {\n                vis[x] = 1;\n                ans++;\n                ne.x = x;\n                ne.y++;\n                if (ne.y &gt;= d)\n                    continue;\n                q.push(ne);\n            }\n        }\n    }\n    return ans;\n}\n\nvoid solve()\n{\n    cin &gt;&gt; n &gt;&gt; d;\n    for (int i = 1; i &lt;= n; i++) {\n        int t;\n        cin &gt;&gt; t;\n        for (int j = 1; j &lt;= t; j++) {\n            int x;\n            cin &gt;&gt; x;\n            a[x].push_back(i);\n        }\n    }\n    int q;\n    cin &gt;&gt; q;\n    while (q--) {\n        int x;\n        cin &gt;&gt; x;\n        cout &lt;&lt; bfs(x, 0) &lt;&lt; endl;\n    }\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\u94fe\u63a5 \u9898\u76ee\u94fe\u63a5 \u9898\u89e3 DFS\u7248\u672c \u56e0\u4e3a\u641c\u7d22\u7684\u65f6\u5019\u4f1a\u6709\u4e00\u4e2a\u95ee\u9898\uff0c\u5982\u679c\u641c\u7d22\u7684\u65f6\u5019\u53d1\u73b0\u5230\u8fbe\u4e00\u4e2a\u70b9\u7684\u65f6\u5019\u7684\u6b65\u6570\uff0c\u6bd4\u524d\u4e00\u6b21\u8bbf\u95ee\u8fc7\u8fd9\u4e2a\u70b9\u7684\u6b65\u6570\u8fd8\u591a\uff0c\u90a3\u4e48\u5c31\u4e0d\u7528\u5f80\u4e0b\u641c\u7d22\u4e86\uff0c\u518d\u641c\u53ea\u4f1a\u589e\u52a0\u65f6\u95f4\u590d\u6742\u5ea6\u3002\u5728\u5199\u9898\u8fc7\u7a0b\u4e2d\u8fd8\u6709\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\u548c\u5c40\u90e8\u53d8\u91cf\u95ee\u9898\u3002 \u51fd\u6570\u4e2d\u5c40\u90e8\u53d8\u91cf\u53ea\u4f1a\u5728\u8fd9\u4e00\u5c42\u8d77\u4f5c\u7528\uff0c\u518d\u5f80\u4e0b\u9012\u5f52\u7684\u65f6\u5019\uff0c\u6bcf\u4e2a\u540c\u540d\u7684\u53d8\u91cf\u4e0d\u662f\u540c\u4e00\u4e2a\u53d8\u91cf #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 [&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,24,8],"class_list":["post-109","post","type-post","status-publish","format-standard","hentry","category-solution","tag-bfs","tag-dfs","tag-8"],"_links":{"self":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts\/109","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=109"}],"version-history":[{"count":0,"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts\/109\/revisions"}],"wp:attachment":[{"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}