{"id":1891,"date":"2021-05-30T10:50:38","date_gmt":"2021-05-30T02:50:38","guid":{"rendered":"http:\/\/47.101.202.111\/?p=1891"},"modified":"2023-04-07T16:17:58","modified_gmt":"2023-04-07T08:17:58","slug":"pat-advanced-tree","status":"publish","type":"post","link":"http:\/\/139.196.114.170\/?p=1891","title":{"rendered":"PAT &#8211; ADVANCED &#8211; \u6811\u5f62\u7ed3\u6784\u7cbe\u8bb2"},"content":{"rendered":"<p>\u672c\u6587\u8be6\u7ec6\u89e3\u6790\u4e8c\u53c9\u6811\u3001\u4e8c\u53c9\u641c\u7d22\u6811\u3001\u54c8\u592b\u66fc\u6811\u3001\u5e76\u67e5\u96c6\u3001\u5806\u7b49\u6811\u5f62\u7ed3\u6784\u548c\u7b97\u6cd5\u5728PAT\u771f\u9898\u4e2d\u7684\u9009\u578b\u539f\u56e0\u3001\u5e94\u7528\u65b9\u6cd5\u53ca\u5b9e\u73b0\u7ec6\u8282<\/p>\n<p><!--more--><\/p>\n<h2>\u6811<\/h2>\n<table>\n<thead>\n<tr>\n<th>\u4e3b\u8981\u8003\u70b9<\/th>\n<th>\u6982\u8981\u5185\u5bb9<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u6811\u7684\u8868\u793a\u6cd5<\/td>\n<td>\u7ed3\u6784\u4f53\u8868\u793a\u3001\u4fdd\u5b58\u5b50\u8282\u70b9\u7684vector\u6570\u7ec4<br \/>\u94fe\u63a5\u6839\u8282\u70b9\u7684\u6570\u7ec4\u3001\u6ee1\u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a<\/td>\n<\/tr>\n<tr>\n<td>\u6811\u7684\u904d\u5386\u65b9\u5f0f<\/td>\n<td>BFS\u3001DFS\u3001\u524d\u4e2d\u540e\u5e8f\u904d\u5386<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>A1004 \u53f6\u8282\u70b9\u8ba1\u6570<\/h3>\n<p>\u9898\u610f\u4e3a\u7edf\u8ba1\u6bcf\u5c42\u53f6\u8282\u70b9\u4e2a\u6570\uff0c\u53ef\u91c7\u7528BFS\u65b9\u5f0f\u904d\u5386\u6811\u8ba1\u6570\uff0c\u904d\u5386\u65f6\u8bb0\u5f55\u5f53\u524d\u5c42\u6570level\uff0c\u6bcf\u5230\u53f6\u8282\u70b9\u5904\u5c06\u5bf9\u5e94\u5c42\u7684\u8ba1\u6570\u53d8\u91cf\u81ea\u589e<\/p>\n<p>\u03b5=\u03b5=\u03b5=(~\uffe3\u25bd\uffe3)~ <strong>Q\uff1aDFS\u600e\u4e48\u505a\uff1f<\/strong><\/p>\n<p>(\u3003\uffe3\ufe36\uffe3) <strong>A\uff1a\u4e00\u5c42\u53f6\u8282\u70b9\u4e2a\u6570\u7531\u201c\u6570\u7ec4[\u5c42\u6570]\u201d\u8bb0\u5f55\uff0c\u9012\u5f52\u5f0fDFS(root, level + 1)\u53ef\u9012\u589e\u5c42\u6570\uff0c\u6bcf\u5230\u4e00\u4e2a\u8282\u70b9\u90fd\u5224\u65ad\u662f\u5426\u53f6\u8282\u70b9\u518d\u8ba1\u6570<\/strong><\/p>\n<pre><code class=\"language-cpp\">#include &lt;iostream&gt;\n#include &lt;vector&gt;\n#include &lt;queue&gt;\nusing namespace std;\n\nconst int maxn = 100;\nvector&lt;int&gt; tree[maxn];\nint num[maxn];\nint level = 0;\n\nint main() {\n    queue&lt;int&gt; q;\n    int N, M, ID, K, S, sz;\n    scanf(&quot;%d %d&quot;, &amp;N, &amp;M);\n\n    while (M--) {\n        scanf(&quot;%d %d&quot;, &amp;ID, &amp;K);\n        while (K--) {\n            scanf(&quot;%d&quot;, &amp;S);\n            tree[ID].push_back(S);\n        }\n    }\n\n    q.push(1);\n    while (!q.empty()) {\n        sz = q.size();\n        for (int i = 0; i &lt; sz; i++) {\n            S = q.front();\n            q.pop();\n            if (!tree[S].size())\n                num[level]++;\n            for (int &amp;x : tree[S])\n                q.push(x);\n        }\n        level++;\n    }\n\n    for (int i = 0; i &lt; level - 1; i++)\n        printf(&quot;%d &quot;, num[i]);\n    printf(&quot;%d\\n&quot;, num[level - 1]);\n\n    return 0;\n}<\/code><\/pre>\n<h3>A1020 \u4e2d\u540e\u5e8f\u5efa\u6811+\u5c42\u5e8f\u904d\u5386<\/h3>\n<p>\u6bcf\u4e2a\u5b50\u6811\u90fd\u80fd\u548c\u76f8\u5e94\u7684\u4e2d\u5e8f\u3001\u540e\u5e8f\u5b50\u5e8f\u5217\u4e00\u4e00\u5bf9\u5e94\uff0c\u5b50\u6811\u540e\u5e8f\u7684\u672b\u5c3e\u8282\u70b9\u603b\u662f\u6839\u8282\u70b9\uff0c\u5bf9\u5e94\u7684\u4e2d\u5e8f\u4e2d\u8be5\u6839\u8282\u70b9\u5de6\u53f3\u90e8\u5206\u4e3a\u5de6\u53f3\u5b50\u6811\uff0c\u4ece\u800c\u4e0d\u65ad\u62ff\u5230\u5b50\u6811\u7684\u6839\u8282\u70b9\u4f5c\u4e3a\u5f53\u524d\u8282\u70b9\u7684\u53f6\u8282\u70b9\u9012\u5f52\u5efa\u6811\uff0c\u5efa\u597d\u7684\u6811\u7528\u961f\u5217\u5c42\u5e8f\u904d\u5386<\/p>\n<p>(\u25cb\u00b4\uff65\u0434\uff65)\uff89<strong>Q:\u4e0d\u662f\u53ef\u4ee5\u7701\u7565\u6811\u7684\u7ed3\u6784\u4f53\uff0c\u76f4\u63a5\u5c06\u8282\u70b9\u6309\u5b8c\u5168\u4e8c\u53c9\u6811\u7684\u65b9\u5f0f\u5b58\u8fdb\u6570\u7ec4\u4e2d\u5417\uff0c\u8fd9\u6837\u5c42\u5e8f\u904d\u5386\u76f4\u63a5\u4ece\u5934\u5230\u5c3e\u626b\u63cf\u6570\u7ec4\u5c31OK\u554a\uff1f<\/strong><\/p>\n<p>( \u2022\u0300 \u03c9 \u2022\u0301 )\u2727<strong>A:\u95ee\u5f97\u597d\uff0c\u786e\u5b9e\u53ef\u4ee5\u53ea\u4f20\u4e00\u4e2a\u7d22\u5f15i\u8fdb\u6765\uff0c\u9012\u5f52\u65f6\u5de6\u5b50\u6811\u6839\u5b58\u57282<em>i\uff0c\u53f3\u5b50\u6811\u6839\u5b58\u57282<\/em>i+1\uff0c\u5c42\u5e8f\u904d\u5386\u5982\u6b64\u7b80\u6d01\u7684\u60f3\u6cd5\u4e5f\u5f88\u68d2\uff01\u7136\u800c\u8fd9\u4e2a\u9898\u6700\u591a\u670931\u4e2a\u8282\u70b9\uff0c\u770b\u4f3c\u8282\u7701\u4e86\u975e\u5e38\u591a\u7684\u5185\u5b58\uff0c\u5982\u679c\u662f\u53ea\u6709\u53f3\u5b50\u6811\u90a3\u79cd\u957f\u94fe\u578b\u7684\uff0c\u5185\u5b58\u5360\u75282\u768431\u6b21\u65b9\uff0cOMG\uff0c\u800c\u4e14\u8f93\u51fa\u5de8\u6162<\/strong><\/p>\n<p>(\u25cb\u00b4\uff65\u0434\uff65)\uff89<strong>Q:\u8fd9\u4e2a\u9012\u5f52\u7684\u4f20\u53c2\u7ed9\u6211\u6574\u50bb\u4e86\uff0c\u800c\u4e14\u600e\u4e48\u7ed3\u675f\u9012\u5f52\u554a<\/strong><\/p>\n<p>(\u256f\u2035\u25a1\u2032)\u256f\u70b8\u5f39\uff01\u2022\u2022\u2022\uff5e\u25cf <strong>A:\u6211TM\u5f53\u65f6\u4e5f\u50bb\u4e86\uff0c\u540e\u6765\u753b\u4e86\u4e2a\u56fe\uff0c\u55ef\u2026\u5177\u4f53\u5982\u4e0b\u56fe\uff0c\u627e\u5230\u4e2d\u5e8f\u6839\u8282\u70b9\u7684\u4f4d\u7f6e\u53ef\u4ee5\u5e2e\u6211\u4eec\u8ba1\u7b97\u51fa\u5de6\u53f3\u5b50\u6811\u7684\u5927\u5c0f\uff0c\u8fd9\u6837\u7684\u8bdd\u5c31\u80fd\u5728\u540e\u5e8f\u5e8f\u5217\u4e2d\u786e\u5b9a\u51fa\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\u7684\u8303\u56f4\u3002\u9012\u5f52\u4e0b\u53bb\uff0c\u8303\u56f4\u5212\u5230\u53ea\u6709\u4e00\u4e2a\u8282\u70b9\u7684\u65f6\u5019\u5fc5\u7136\u662f\u4e00\u4e2a\u72ec\u7acb\u7684\u53f6\u8282\u70b9\u4e86\uff0c\u628a\u5b83\u8fd4\u56de\u94fe\u63a5\u5230\u7236\u7ed3\u70b9\u4e0a\uff0c\u5982\u679c\u8303\u56f4\u4e0d\u5408\u6cd5\u8bf4\u660e\u6ca1\u8fd9\u4e2a\u53f6\u8282\u70b9\u8fd4\u56de\u7a7a\u6307\u9488\uff0c\u5f53\u524d\u5c42\u672c\u8eab\u662f\u4e00\u4e2a\u6839\u8282\u70b9\u4e5f\u7ed9\u5b83\u8fd4\u56de\u6302\u5230\u7236\u7ed3\u70b9\u4e0a<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">\u4e0b\u6807\u8303\u56f4<\/th>\n<th style=\"text-align: center;\">\u4e0b\u6807\u8303\u56f4<\/th>\n<th style=\"text-align: center;\">\u4e0b\u6807\u8303\u56f4<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\">\u540e\u5e8fp<\/td>\n<td style=\"text-align: center;\">\u5de6\u5b50\u6811[plo,plo+lLen-1]<\/td>\n<td style=\"text-align: center;\">\u53f3\u5b50\u6811[phi-rLen,phi-1]<\/td>\n<td style=\"text-align: center;\">\u6839[phi]<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">\u4e2d\u5e8fi<\/td>\n<td style=\"text-align: center;\">\u5de6\u5b50\u6811[ilo,ir-1]<\/td>\n<td style=\"text-align: center;\">\u6839[ir]<\/td>\n<td style=\"text-align: center;\">\u53f3\u5b50\u6811[ir+1,ihi]<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>(\u2299o\u2299)\uff1f<strong>Q:\u8f93\u51fa\u7684\u65f6\u5019\uff0c\u6211\u62ff\u961f\u5217\u53ea\u6709\u4e00\u4e2a\u8282\u70b9\u7684\u60c5\u51b5\u5224\u65ad\u672b\u5c3e\u7684\uff0c\u7136\u540e\u5c31GG\u4e86<\/strong><\/p>\n<p>\u03bf(=\u2022\u03c9\uff1c=)\u03c1\u2312\u2606<strong>A:\u6bd4\u5982\u53ea\u6709\u6839\u8282\u70b9\u7684\u65f6\u5019\u4e5f\u662f\u53ea\u6709\u4e00\u4e2a\u8282\u70b9\u554a\uff0c\u6362\u4e2a\u601d\u8def\uff0c\u90a3\u4e2a\u8282\u70b9\u6570\u76een\u5f88\u91cd\u8981\uff0c\u6bcf\u51fa\u961f\u5217\u4e00\u6b21\uff0c\u5c31\u7ed9\u5b83\u81ea\u51cf\uff0c\u52240\u5c31OK\u4e86<\/strong><\/p>\n<pre><code class=\"language-cpp\">#include &lt;iostream&gt;\n#include &lt;bits\/stdc++.h&gt;\n#include &lt;queue&gt;\nusing namespace std;\n\nconst int maxn = 31;\nint po[maxn];\nint in[maxn];\n\nstruct TreeNode {\n    int val;\n    TreeNode *lNode;\n    TreeNode *rNode;\n    TreeNode(int v, TreeNode *l, TreeNode *r) : val(v), lNode(l), rNode(r) {}\n};\n\nTreeNode *buildTree(int plo, int phi, int ilo, int ihi) {\n    if (phi &lt; plo) return nullptr;\n    if (phi == plo) return new TreeNode(po[plo], nullptr, nullptr);\n\n    int irId;\n    int root = po[phi];\n    for (irId = ilo; root != in[irId]; irId++);\n    int lLen = irId - ilo;\n    int rLen = ihi - irId;\n\n    return new TreeNode(root, buildTree(plo, plo + lLen - 1, ilo, irId - 1), buildTree(plo + lLen, phi - 1, irId + 1, ihi));\n}\n\nint main() {\n    memset(po, 0, sizeof(po));\n    memset(in, 0, sizeof(in));\n\n    int n;\n    scanf(&quot;%d&quot;, &amp;n);\n    for (int i = 1; i &lt;= n; i++) scanf(&quot;%d&quot;, &amp;po[i]);\n    for (int i = 1; i &lt;= n; i++) scanf(&quot;%d&quot;, &amp;in[i]);\n\n    queue&lt;TreeNode *&gt; q;\n    q.push(buildTree(1, n, 1, n));\n    while (!q.empty()) {\n        TreeNode *node = q.front();\n        q.pop(); n--;\n        if (n == 0) printf(&quot;%d\\n&quot;, node-&gt;val);\n        else printf(&quot;%d &quot;, node-&gt;val);\n        if (node-&gt;lNode) q.push(node-&gt;lNode);\n        if (node-&gt;rNode) q.push(node-&gt;rNode);\n    }\n\n    return 0;\n}<\/code><\/pre>\n<h3>A1053 \u8def\u5f84\u6743\u503c\u548c<\/h3>\n<p>\u5178\u578b\u7684DFS\u7c7b\u95ee\u9898\u3002\u53ef\u5206\u89e3\u4e3a\u4e24\u4e2a\u95ee\u9898\uff1a\u4e00\u662f\u627e\u5230\u7ed9\u5b9a\u8def\u5f84\u5e76\u8bb0\u5f55\uff0c\u4e8c\u662f\u975e\u5347\u5e8f\u8f93\u51fa<\/p>\n<p>(\u24ff_\u24ff) <strong>Q\uff1aDFS\u6211\u4f1a\uff0c\u4f46\u662f\u7ed9\u5b9a\u8def\u5f84\u600e\u4e48\u8bb0\u5f55\uff0c\u800c\u4e14\u8fd8\u8981\u8bb0\u5f55\u4e0d\u540c\u8def\u5f84\uff0c\u773c\u82b1\u5934\u79c3<\/strong><\/p>\n<p>(\u0e07 \u2022_\u2022)\u0e07 <strong>A\uff1a\u522b\u614c\u3002DFS\u7684\u904d\u5386\u8fc7\u7a0b\u53ef\u7531\u6808\u6a21\u62df\uff0c\u6bcf\u8fdb\u5165\u5b50\u5c42\u76f8\u5f53\u4e8e\u5b50\u8282\u70b9\u5165\u6808\uff0c\u6bcf\u56de\u5230\u7236\u5c42\u76f8\u5f53\u4e8e\u5b50\u7ed3\u70b9\u51fa\u6808\uff0c\u4e14\u6808\u4e2d\u59cb\u7ec8\u53ea\u6709\u4e00\u6761\u8def\u5f84\uff0c\u7531\u6b64\u4ece\u6808\u5e95\u5230\u6808\u9876\u53ef\u552f\u4e00\u6253\u5370\u8def\u5f84\u3002\u7136\u9e45\u4f7f\u7528stack\u548cqueue\u5747\u9700\u6240\u6709\u8282\u70b9\u5f39\u51fa\u6570\u636e\u7ed3\u6784\uff0c\u7834\u574f\u7ee7\u7eed\u8c03\u7528\u7684\u6761\u4ef6\uff0c\u56e0\u6b64\u6362\u7528vector\u6700\u4e3a\u59a5\u5f53<\/strong><\/p>\n<p>\u2026\u2026]((o<em> <\/em>)&#8217;\u5f61\u2606 <strong>Q\uff1a\u975e\u5347\u5e8f\u8f93\u51fa\u600e\u4e48\u641e\uff1f<\/strong><\/p>\n<p>( \u2022\u0300 \u03c9 \u2022\u0301 )y <strong>A\uff1a\u4e3e\u4e2a\u6817\u5b50~\u73b0\u5728\u5b9a\u4f4d\u5230DFS\u8bbf\u95ee\u5b50\u8282\u70b9\u7684\u4ee3\u7801\u884c\uff0c\u5982\u679c\u6211\u4eec\u9012\u5f52\u8bbf\u95ee\u5b50\u8282\u70b93\uff0c\u8fd4\u56de\u5230\u5f53\u524d\u5c42\u4e4b\u540e\u63a5\u7740\u8bbf\u95ee\u66f4\u5c0f\u5b50\u8282\u70b92\u7684\u8bdd\uff0c\u8f93\u51fa\u7684\u8bbf\u95ee\u7ed3\u679c\u5c06\u4f9d\u6b21\u662f3XXX\uff0c2XXX\u3002\u7531\u6b64\u8bfb\u5165\u5b50\u5e8f\u5217\u540e\u987a\u5e26\u6392\u4e2a\u964d\u5e8f\uff0cDFS\u6bcf\u5c42\u5c31\u90fd\u4f1a\u964d\u5e8f\u8bbf\u95ee\u5b50\u8282\u70b9\u5566\uff0c\u6392\u5e8f\u5c31\u7528\u5e93\u51fd\u6570sort\u7b80\u5355\u65b9\u4fbf\uff01<\/strong><\/p>\n<pre><code class=\"language-cpp\">#include &lt;iostream&gt;\n#include &lt;vector&gt;\n#include &lt;algorithm&gt;\nusing namespace std;\n\nint S;\nconst int maxn = 100;\nvector&lt;int&gt; q;\n\nstruct Node {\n    int w;\n    vector&lt;int&gt; v;\n} tree[maxn];\n\nbool cmp(int a, int b) {\n    return tree[a].w &gt; tree[b].w;\n}\n\nvoid DFS(int root, int sum) {\n    q.push_back(tree[root].w);\n    int add = sum + tree[root].w;\n\n    if (add &gt; S) return;\n\n    if (add == S &amp;&amp; !tree[root].v.size()) {\n        for (int i = 0; i &lt; q.size() - 1; i++)\n            printf(&quot;%d &quot;, q[i]);\n        printf(&quot;%d\\n&quot;, q[q.size() - 1]);\n    }\n\n    else for (int &amp;x : tree[root].v) {\n        DFS(x, add);\n        q.pop_back();\n    }\n}\n\nint main() {\n    int N, M, W, i, n, s;\n    scanf(&quot;%d %d %d&quot;, &amp;N, &amp;M, &amp;S);\n    for (int i = 0; i &lt; N; i++)\n        scanf(&quot;%d&quot;, &amp;tree[i].w);\n\n    while (M--) {\n        scanf(&quot;%d %d&quot;, &amp;i, &amp;n);\n        while (n--) {\n            scanf(&quot;%d&quot;, &amp;s);\n            tree[i].v.push_back(s);\n        }\n        sort(tree[i].v.begin(), tree[i].v.end(), cmp);\n    }\n\n    DFS(0, 0);\n\n    return 0;\n}<\/code><\/pre>\n<h3>A1079 \u53f6\u8282\u70b9\u53ca\u6df1\u5ea6<\/h3>\n<p>\u9898\u610f\u4e3a\u8d27\u7269\u4ece\u521d\u59cb\u4f9b\u8d27\u5546\u7ecf\u8fc7\u591a\u7ea7\u5206\u9500\u5546\u52a0\u4ef7\u5230\u8fbe\u96f6\u552e\u5546\u7684\u552e\u5356\u603b\u4ef7\uff0c\u5373 $\\scriptsize \u603b\u4ef7=\\sum\\limits_{\u96f6\u552e\u55461}^{\u96f6\u552e\u5546n} \u5355\u4ef7\u00d7\u6570\u91cf\u00d7(1+\u767e\u5206\u6bd4)^{\u96f6\u552e\u5546i\u5c42\u7ea7}$\uff0c\u4f9b\u8d27\u5546\u5373\u6839\u8282\u70b9\uff0c\u5206\u9500\u5546\u4e3a\u975e\u53f6\u8282\u70b9\uff0c\u96f6\u552e\u5546\u4e3a\u53f6\u8282\u70b9\uff0c\u96f6\u552e\u5546\u5c42\u7ea7\u5373\u53f6\u8282\u70b9\u6df1\u5ea6\uff0c\u6b64\u9898\u5173\u952e\u70b9\u5728\u4e8e\u5224\u65ad\u53f6\u8282\u70b9\u548c\u83b7\u53d6\u53f6\u8282\u70b9\u6df1\u5ea6<\/p>\n<p>( \u2022\u0300 .\u032b \u2022\u0301 )\u2727 <strong>Q\uff1a\u6211\u6709\u4e00\u4e2a\u60f3\u6cd5\uff0c\u7528\u6811\u7684\u6570\u7ec4\u8868\u793a\uff0c\u6bcf\u4e2a\u8282\u70b9\u5b58\u50a8\u5176\u7236\u7ed3\u70b9\u7d22\u5f15\uff1b\u8bfb\u5230\u53f6\u8282\u70b9\u65f6\u5165\u961f\u5217\uff0c\u6700\u540e\u53ea\u53d6\u961f\u5217\u4e2d\u7684\u53f6\u8282\u70b9\uff0c\u9010\u6b21\u6eaf\u5230\u6839\u5373\u4e3a\u6df1\u5ea6\uff0c\u8fd9\u6837\u5904\u7406\u7684\u8282\u70b9\u6570\u662f\u4e0d\u662f\u5927\u5927\u51cf\u5c0f\u5566<\/strong><\/p>\n<p>\u0e05\u0295\u2022\u0361\u032b\u2022\u0294\u0e05 <strong>A\uff1a\u60f3\u6cd5\u5f88\u68d2\u54df\uff0c\u8fd9\u79cd\u6811\u7684\u8868\u793a\u60f3\u5fc5\u662f\u6765\u81ea\u5e76\u67e5\u96c6\u5427\uff01\u6d47\u76c6\u51b7\u6c34\uff0c\u5f53\u5b58\u5728\u5927\u91cf\u53f6\u8282\u70b9\u7684\u65f6\u5019\uff0c\u5047\u8bbe\u4e3a\u6ee1m\u53c9\u6811\uff0c\u8ba1\u7b97\u53f6\u8282\u70b9\u6df1\u5ea6\u7684\u603b\u6b21\u6570\u4e3a $\\scriptsize O(m^{logn}logn)$\uff0c$\\scriptsize 10^5$\u95ee\u9898\u89c4\u6a21\u4f1a\u8d85\u65f6\u7684\uff01\u800c\u5982\u679c\u6211\u4eec\u91c7\u7528DFS\u6216BFS\uff0c$\\scriptsize O(n)$ \u5df2\u7ecf\u4fdd\u8bc1\u8ba1\u7b97\u6240\u6709\u8282\u70b9\u6df1\u5ea6\u5566\u3002\u90a3\u4e3a\u4f55\u7528BFS\u6ca1\u7528DFS\u5462\uff0c\u56e0\u4e3a\u9012\u5f52\u8c03\u7528\u7684\u5f00\u9500\u8f83\u5927\uff0c\u91c7\u7528\u8fed\u4ee3\u7684\u6027\u80fd\u5f53\u7136\u4f1a\u66f4\u597d\u54af\uff0c\u800c\u4e14\u5c42\u5e8f\u904d\u5386\u7684\u5c42\u6570\u5c31\u662f\u6df1\u5ea6\uff0c\u8ba1\u6570\u65b9\u4fbf\u5f88\u591a\u561b<\/strong><\/p>\n<p>(\u25cb\uff40 3\u2032\u25cb) <strong>Q\uff1a\u90a3\u6211\u5728\u5efa\u6811\u7684\u65f6\u5019\uff0c\u6bcf\u8fde\u4e00\u4e2a\u5b50\u8282\u70b9\u5c31\u8ba9\u5b83\u5728\u7236\u7ed3\u70b9\u6df1\u5ea6\u4e0a\u7d2f\u52a01\uff0c\u8fd9\u6837\u5c31\u80fd\u76f4\u63a5\u7528\uff01<\/strong><\/p>\n<p>(\uffe3\u25bd\uffe3)&quot; <strong>A\uff1a\u5443\u2026\u5982\u679c\u8f93\u5165\u6570\u636e\u662f\u5148\u5efa\u7acb\u5b50\u6811\uff0c\u540e\u7eed\u624d\u6302\u5230\u7236\u7ed3\u70b9\u4e0a\uff0c\u90a3\u600e\u4e48\u529e\uff0c\u5411\u4e0b\u904d\u5386\u6539\u53d8\u6240\u6709\u5b50\u8282\u70b9\u561b\uff1f\u5b58\u50a8\u5b50\u8282\u70b9\u518d\u53bb\u904d\u5386\uff0c\u8fd9\u6837\u597d\u50cf\u548cDFS\u904d\u5386\u6811\u7ed3\u6784\u6ca1\u4ec0\u4e48\u533a\u522b<\/strong><\/p>\n<p>((\uffe3_\uffe3|||)) <strong>Q\uff1a\u662f\u54e6\u2026\u2026\u7b49\u4e00\u4e0b\uff01\u8ba9\u5b50\u8282\u70b9\u6eaf\u6839\u7684\u65f6\u5019\u5728\u5df2\u8ba1\u7b97\u7684\u8282\u70b9\u4e0a\u6807\u8bb0\u4e00\u4e0b\u6df1\u5ea6\uff0c\u5176\u4ed6\u5b50\u8282\u70b9\u8bbf\u95ee\u5230\u516c\u5171\u8282\u70b9\u7684\u65f6\u5019\u5c31\u80fd\u5171\u7528\u2026<\/strong><\/p>\n<p>d=====(\uffe3\u25bd\uffe3*)b <strong>Q\uff1a\u54e6\uff0c\u662f\u561b\uff1f\u7075\u9b42\u4e09\u95ee\uff1a\u95ee\u9898\u4e00\uff1a\u516c\u5171\u8282\u70b9\u771f\u7684\u5f88\u591a\u561b\uff1f\u95ee\u9898\u4e8c\uff1a\u516c\u5171\u8282\u70b9\u771f\u7684\u8db3\u591f\u6df1\u4ee5\u51cf\u5c11\u91cd\u590d\u8ba1\u7b97\u561b\uff1f\u6700\u540e\u81f4\u547d\u4e00\u51fb\uff1a\u4ece\u6839\u5230\u53f6\u7d2f\u8ba1\u624d\u53ef\u4ee5\u5171\u7528\u6df1\u5ea6\uff0c\u4f60\u7684\u65b9\u5411\u662f\u4ece\u53f6\u5230\u6839\uff01\u4f60\u53ef\u80fd\u662f\u60f3\u5230\u5e76\u67e5\u96c6\u7684\u5408\u5e76\uff1a\u4e24\u6839\u8282\u70b9\u4fdd\u5b58\u4e86\u5404\u81ea\u6811\u7684\u5927\u5c0f\uff0c\u5408\u5e76\u65f6\u76f4\u63a5\u6eaf\u6839\u5408\u5e76\u5927\u5c0f\uff0c\u5f97\u7684\u662f\u6574\u4f53\u7684\u7ed3\u679c\uff1b\u800c\u8fd9\u91cc\u662f\u6bcf\u4e2a\u53f6\u8282\u70b9\u90fd\u8981\u8ba1\u7b97\u5230\u6839\u7684\u8ddd\u79bb\uff0c\u662f\u4e2a\u4f53\u7684\u7ed3\u679c\uff01\u522b\u6323\u624e\u5566\u2026\u2026\u542c\u8bdd\uff0c\u4e56\u4e56\u53bb\u904d\u5386<\/strong><\/p>\n<p>(\u256c\u2594\u76bf\u2594)\u51f8 <strong>Q\uff1aehhhhhh\uff0c\u751f\u6c14(\u810f\u8bdd\uff01)\uff0c\u8fd8\u6709\u6bb5\u9519\u8bef\u4ec0\u4e48\u9b3c\uff1f<\/strong><\/p>\n<p>( \u2022\u0300 \u03c9 \u2022\u0301 )y <strong>A\uff1a\u5feb\u53bb\u68c0\u67e5\u4f60\u6709\u6ca1\u6709\u6570\u7ec4\u8d8a\u754c\uff0c\u65e0\u9650\u9012\u5f52\uff0c\u7a7a\u6307\u9488\u8d4b\u503c\uff01\u5f31\u5f31\u7684\u63d0\u9192\u4e00\u4e0b\uff0c10\u7684\u4e94\u6b21\u65b9\u540e\u9762\u67095\u4e2a0<\/strong><\/p>\n<pre><code class=\"language-cpp\">#include &lt;iostream&gt;\n#include &lt;cmath&gt;\n#include &lt;queue&gt;\n#include &lt;bits\/stdc++.h&gt;\nusing namespace std;\n\nconst int maxn = 100001;\n\nstruct TreeNode {\n    int qty;\n    vector&lt;int&gt; sub;\n} tree[maxn];\n\nint main() {\n    int N, num, qty, sub, size, depth = 0;\n    double P, r, sum = 0;\n    TreeNode node;\n    scanf(&quot;%d %lf %lf\\n&quot;, &amp;N, &amp;P, &amp;r);\n    for (int i = 0; i &lt; N; i++) {\n        scanf(&quot;%d&quot;, &amp;num);\n        if (num == 0) {\n            scanf(&quot;%d\\n&quot;, &amp;qty);\n            tree[i].qty = qty;\n        }\n        else while (num--) {\n            scanf(&quot;%d&quot;, &amp;sub);\n            tree[i].sub.push_back(sub);\n        }\n    }\n\n    queue&lt;int&gt; q;\n    q.push(0);\n    while (!q.empty()) {\n        size = q.size();\n        while (size--) {\n            node = tree[q.front()];\n            q.pop();\n            if (node.sub.size() &lt;= 0)\n                sum += P * node.qty * pow(1 + r \/ 100, depth);\n            else for (int x : node.sub)\n                q.push(x);\n        }\n        depth++;\n    }\n\n    printf(&quot;%.1f\\n&quot;, sum);\n\n    return 0;\n}<\/code><\/pre>\n<h3>A1086 \u975e\u9012\u5f52\u5efa\u6811<\/h3>\n<p>\u975e\u9012\u5f52\u5efa\u6811\u7684\u7279\u70b9\u662f\uff1a<strong>\u8fde\u7eed\u7684push\u4e00\u5b9a\u662f\u6808\u9876\u8282\u70b9\u7684\u5de6\u5b69\u5b50\uff0cpop\u4e4b\u540e\u7684push\u662f\u6808\u9876\u8282\u70b9\u7684\u53f3\u5b69\u5b50<\/strong>\uff1b\u4f9d\u6b64\u4fdd\u5b58\u672c\u6b21top\u8282\u70b9\u4f5c\u4e3a\u4e0b\u4e2apush\u8282\u70b9\u7684\u7236\u7ed3\u70b9\uff0c\u6807\u8bb0push\u6216pop\u64cd\u4f5c\u51b3\u5b9a\u5de6\u8fd8\u662f\u53f3\u5b50\u8282\u70b9\u3002\u5efa\u6811\u540e\u540e\u5e8f\u904d\u5386\u3002<\/p>\n<p>(\u02c9\u25bd\uffe3\uff5e) \u5207\uff5e\uff5e <strong>Q:\u4eba\u5bb6\u6709\u66f4\u597d\u7684\u60f3\u6cd5\uff0cpush\u5e8f\u5217\u662f\u5148\u5e8f\u7684\uff0cpop\u5e8f\u5217\u662f\u4e2d\u5e8f\u7684\uff0c\u8fd9\u4e2a\u95ee\u9898\u4e0d\u5c31\u662f\u4e2d\u5e8f\u548c\u5148\u5e8f\u5e8f\u5217\u5efa\u6811\u561b<\/strong><\/p>\n<p>(\uff03\u00b0\u0414\u00b0) <strong>A:\u65af\u2026\u2026\u65af\u56fd\u4ee5<\/strong><\/p>\n<p>\u2511(\uffe3\u0414 \uffe3)\u250d <strong>Q:\u4e0d\u8fc7\u8fd9\u4e2a\u8bfb\u5165\u683c\u5f0f\u600e\u4e48\u641e<\/strong><\/p>\n<p>(\u25cf\u02c7\u2200\u02c7\u25cf) <strong>A:\u4f60\u8981\u662f\u4f1a\u53ef\u4ee5\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u554a\uff01\u4e0d\u8fc7scanf\u8bfb\u5165\u5b57\u7b26\u4e32\u4f1a\u5728\u7a7a\u683c\u5904\u7ed3\u675f\uff0c\u8fd9\u662f\u4e0d\u662f\u5929\u7136\u5206\u5f00\u4e86push\u548c\u6570\u5b57\uff0cstrcmp\u548cstrlen\u90fd\u80fd\u89e3\u51b3\u4f60\u7684\u95ee\u9898<\/strong><\/p>\n<pre><code class=\"language-cpp\">#include &lt;iostream&gt;\n#include &lt;stack&gt;\n#include &lt;cstring&gt;\nusing namespace std;\n\nint n;\n\nstruct TreeNode {\n    int val;\n    TreeNode *left;\n    TreeNode *right;\n    TreeNode(int v) : val(v), left(nullptr), right(nullptr) {}\n};\n\nvoid post(TreeNode *root) {\n    if(root == nullptr) return;\n    post(root-&gt;left);\n    post(root-&gt;right);\n    if (--n) printf(&quot;%d &quot;, root-&gt;val);\n    else printf(&quot;%d\\n&quot;, root-&gt;val);\n}\n\nint main() {\n    int t;\n    char s[5];\n    bool isL = true;\n    scanf(&quot;%d\\n&quot;, &amp;n);\n\n    stack&lt;TreeNode *&gt; stk;\n    TreeNode *top, *cur, *root;\n    for (int i = 0; i &lt; 2 * n; i++) {\n        scanf(&quot;%s&quot;, s);\n        if (strcmp(s, &quot;Push&quot;) == 0) {\n            scanf(&quot;%d&quot;, &amp;t);\n            if (i == 0) {\n                root = new TreeNode(t);\n                stk.push(root);\n                top = root;\n                continue;\n            }\n\n            cur = new TreeNode(t);\n            if (isL) top-&gt;left = cur;\n            else {\n                top-&gt;right = cur;\n                isL = true;\n            }\n            stk.push(cur);\n            top = cur;\n        }\n        else {\n            isL = false;\n            top = stk.top();\n            stk.pop();\n        }\n    }\n\n    post(root);\n}<\/code><\/pre>\n<h3>A1090 DFS\u904d\u5386\u6811<\/h3>\n<p>\u8fd9\u9053\u9898\u8fd8\u662f\u53f6\u8282\u70b9\u6df1\u5ea6\u95ee\u9898\uff0c\u65e0\u975e\u65b0\u589e\u4e86\u4e00\u6761\u4fdd\u5b58\u6700\u5927\u6df1\u5ea6\u3002\u5728A1079\u65f6\u91c7\u7528BFS\u505a\uff0c\u8fd9\u6b21\u6539\u7528DFS\u505a\u4e00\u4e0b<\/p>\n<p>(o\uff9fv\uff9f)\u30ce <strong>Q\uff1a\u6bcf\u5c42\u9012\u5f52\u90fd\u6bd4\u8f83\u4e00\u4e0b\u9ad8\u5ea6\u561b<\/strong><\/p>\n<p>(o\u309c\u25bd\u309c)o\u2606 <strong>A\uff1a\u9898\u8bbe\u6240\u95ee\u7684\u6700\u5927\u6df1\u5ea6\u4ec5\u5728\u53f6\u8282\u70b9\u5904\uff0c\u6240\u4ee5\u6211\u4eec\u4ec5\u5728\u53f6\u7ed3\u70b9\u5904\u8bb0\u5f55\u66f4\u65b0\u6700\u5927\u9ad8\u5ea6\u53ef\u4ee5\u5927\u5e45\u51cf\u5c11\u64cd\u4f5c\u6b21\u6570\uff0c\u51b5\u4e14\u53f6\u8282\u70b9\u5904\u4e5f\u662f\u9012\u5f52\u8fb9\u754c\u5594<\/strong><\/p>\n<p>(o\uff9fv\uff9f)\u30ce <strong>Q\uff1a\u54c7\u54e6\uff0c\u6811\u7684\u8868\u793a\u53c8\u8fdb\u4e00\u6b65\u7b80\u5316\u4e86<\/strong><\/p>\n<p>\u25d5\u203f\u25d5 <strong>A\uff1a\u6bd5\u7adf\u6bcf\u4e2a\u8282\u70b9\u4e0d\u9700\u8981\u989d\u5916\u5b58\u50a8\u503c\uff0c\u4ec5\u8bb0\u5f55\u6240\u6709\u8282\u70b9\u7684\u5b50\u8282\u70b9\u5c31\u597d\u5566\u3002\u7d22\u5f15\u8868\u793a\u8282\u70b9\uff0c\u7d22\u5f15\u503c\u662f\u5b50\u8282\u70b9\u96c6\u5408\uff0c\u5f88\u5bb9\u6613\u60f3\u5230\u662fvector\u6570\u7ec4\uff01<\/strong><\/p>\n<p>\u250f (\u309c\u03c9\u309c)=\u261e <strong>Q\uff1a~t\u4ee3\u8868\u4ec0\u4e48<\/strong><\/p>\n<p>(\u3002\u30fb\u2200\u30fb)\u30ce <strong>A\uff1a\u8fd9\u662f\u4e00\u4e2a\u5c0ftrick\uff0c-1\u5728\u8ba1\u7b97\u673a\u4e2d\u662f\u4ee5\u8865\u7801 $\\scriptsize (11111111)_B$ \u6765\u8868\u793a\u7684\uff0c\u53d6\u53cd\u4e3a0\u53ef\u7528\u6765\u5224\u65ad-1<\/strong><\/p>\n<pre><code class=\"language-cpp\">#include &lt;iostream&gt;\n#include &lt;vector&gt;\n#include &lt;cmath&gt;\nusing namespace std;\n\nconst int maxn = 100001;\nvector&lt;int&gt; sub[maxn];\nint maxH = 0, num = 0;\n\nvoid DFS(int root, int height) {\n    if (sub[root].size() == 0) {\n        if (height &gt; maxH) {\n            maxH = height;\n            num = 1;\n        }\n        else if (height == maxH)\n            num++;\n\n        return;\n    }\n\n    for (int &amp;x : sub[root])\n        DFS(x, height + 1);\n}\n\nint main() {\n    int n, t, root;\n    double P, r;\n    scanf(&quot;%d %lf %lf&quot;, &amp;n, &amp;P, &amp;r);\n\n    for (int i = 0; i &lt; n; i++) {\n        scanf(&quot;%d&quot;, &amp;t);\n        if (~t) sub[t].push_back(i);\n        else root = i;\n    }\n\n    DFS(root, 0);\n    printf(&quot;%.2f %d\\n&quot;, P * pow(1 + r \/ 100, maxH), num);\n\n    return 0;\n}<\/code><\/pre>\n<h3>A1094 \u6700\u5927\u6df1\u5ea6\u8282\u70b9<\/h3>\n<p>\u9898\u610f\u7b80\u5316\u4e3a\u67e5\u8be2\u6811\u4e2d\u6700\u5927\u8282\u70b9\u7684\u5c42\u6240\u5728\u5c42\u6570\u53ca\u8282\u70b9\u6570\u76ee<\/p>\n<p>(o-\u03c9\uff51)).oO \u56f0 <strong>A\uff1a\u6811\u7684\u7b80\u5316\u8868\u793a\uff0cBFS\u3001DFS\uff0c\u662f\u4e0d\u662f\u90fd\u5f88\u719f\u5566\uff0c\u8fd9\u9053\u9898\u624b\u987a\u5f88\u591a\u4e86\u5427<\/strong><\/p>\n<p>,,\u053e\u3142\u053e,, <strong>A\uff1a\u8fd8\u543c\u5566\uff0c\u591a\u8bbe\u4e2a\u53d8\u91cf\u6765\u4fdd\u5b58\u6700\u5927\u503c\u5c31OK\uff01<\/strong><\/p>\n<pre><code class=\"language-cpp\">#include &lt;iostream&gt;\n#include &lt;vector&gt;\n#include &lt;queue&gt;\nusing namespace std;\n\nint main() {\n    int maxNum = 0, maxLevel = 0;\n    int N, M, i, s, n, sz, level = 0;\n    scanf(&quot;%d %d\\n&quot;, &amp;N, &amp;M);\n    vector&lt;int&gt; tree[N + 1];\n    queue&lt;int&gt; q;\n\n    while (M--) {\n        scanf(&quot;%d %d&quot;, &amp;i, &amp;n);\n        while (n--) {\n            scanf(&quot;%d&quot;, &amp;s);\n            tree[i].push_back(s);\n        }\n    }\n\n    q.push(1);\n    while (!q.empty()) {\n        level++;\n        sz = q.size();\n        if (sz &gt; maxNum) {\n            maxNum = sz;\n            maxLevel = level;\n        }\n        for (int j = 0; j &lt; sz; j++) {\n            i = q.front();\n            q.pop();\n            for (int &amp;x : tree[i])\n                q.push(x);\n        }\n    }\n\n    printf(&quot;%d %d&quot;, maxNum, maxLevel);\n\n    return 0;\n}<\/code><\/pre>\n<h3>A1102 \u53cd\u8f6c\u4e8c\u53c9\u6811<\/h3>\n<p>\u4e8c\u53c9\u6811\u53cd\u8f6c\u5b9e\u9645\u662f\u5c06\u6bcf\u4e2a\u8282\u70b9\u7684\u5de6\u53f3\u5b50\u6811\u4e92\u6362\u3002<br \/>\n\u5982\u679c\u4e0d\u4fee\u6539\u6811\u7684\u539f\u7ed3\u6784\uff0c\u53ea\u9700\u4fee\u6539\u904d\u5386\u65b9\u5f0f\uff0c\u5148\u53f3\u518d\u5de6\u5373\u53ef\u3002<br \/>\n\u82e5\u9700\u4fee\u6539\u6811\u7684\u7ed3\u6784\uff0c\u4ee5\u524d\u4e2d\u540e\u4efb\u4e00\u987a\u5e8f\u5c06\u6bcf\u4e00\u8282\u70b9\u5de6\u53f3\u5b50\u6811\u4e92\u6362\u5373\u53ef\u3002<\/p>\n<p>(\u25cb\uff40 3\u2032\u25cb) <strong>Q\uff1a\u4f60\u6ca1\u6709\u5efa\u6811\uff01\u4f60\u6839\u8282\u70b9\u5728\u54ea\uff01\u8fd9\u4e0d\u662f\u6811\u7684\u7ed3\u6784\u4f53\u8868\u793a\u554a<\/strong><\/p>\n<p>\u266a(\u00b4\u25bd\uff40) <strong>A\uff1a\u53ea\u8981\u903b\u8f91\u4e0a\u6ee1\u8db3\u6811\u7684\u7ed3\u6784\u5373\u53ef\uff0c\u6570\u7ec4\u7d22\u5f15\u6807\u8bc6\u4e3a\u6839\u8282\u70b9\uff0c\u4e24\u4e2a\u6570\u7ec4\u5206\u522b\u5b58\u6839\u8282\u70b9\u5bf9\u5e94\u7684\u5de6\u53f3\u8282\u70b9\uff0c\u672c\u8d28\u4e0a\u662f\u4e00\u6837\u7684\uff0c\u751a\u81f3\u66f4\u7701\u5185\u5b58\u3002\u4f60\u770b\u9898\u7ed9\u8f93\u5165\u90fd\u662f\u5b50\u8282\u70b9\uff0c\u6ca1\u7ed9\u51fa\u7684\u81ea\u7136\u662f\u6839\u8282\u70b9\u54af\uff0c\u6807\u8bb0\u4e00\u4e0b\u904d\u5386\u7b5b\u9009\u5c31\u597d\u561b\u3002\u5efa\u6811\u7684\u65f6\u5019\u76f4\u63a5\u628a\u5b83\u7684\u5b50\u6811\u4e92\u6362\u53cd\u8f6c\uff0c\u8fd9\u6837\u8f93\u51fa\u987a\u5e8f\u8fd8\u53ef\u4ee5\u7edf\u4e00<\/strong><\/p>\n<p>(\u30ce\u3078\uffe3\u3001) <strong>Q\uff1a\u8c01\u80fd\u544a\u8bc9\u6211\u4e00\u4e0b\u6211\u7684\u8f93\u5165\u4e3a\u4ec0\u4e48\u4e00\u76f4\u53d6\u4e0d\u5bf9<\/strong><\/p>\n<p>\uff08\uffe3\ufe36\uffe3\uff09\u2197\u3000<strong>A\uff1a\u5148\u522b\u54ed\uff0c\u5176\u5b9e\u547d\u9898\u4eba\u8fd8\u662f\u86ee\u8d34\u5fc3\u7684(\u6211\u4fe1\u4e86)\u3002\u7531\u4e8e\u5b58\u5728&quot;-&quot;\u7b26\u53f7\u6240\u4ee5\u53ea\u80fd\u7edf\u4e00\u4ee5\u5b57\u7b26\u5f62\u5f0f\u8f93\u5165\uff0c\u4f60\u770b\u8f93\u5165\u6307\u5b9a\u4e86\u6700\u592710\u4e2a\u8282\u70b9\uff0c\u8fd9\u6837\u7684\u8bdd0-9\u5c31\u987a\u7406\u6210\u7ae0\u3002\u5982\u679c\u6ca1\u6709\u8fd9\u4e2a\u9650\u5b9a\uff0c\u8fd8\u9700\u8981\u8fdb\u884c\u591a\u4f4d\u5b57\u7b26\u4e32\u5230\u6570\u5b57\u7684\u8f6c\u6362\uff0c\u8981\u9ebb\u70e6\u5f88\u591a\u3002\u5751\u7239\u7684\u662f\uff0c\u9996\u5148\u4e0d\u8981\u5fd8\u8bb0\u628a\u5b57\u7b26\u8f6c\u6362\u56de\u6570\u5b57\uff01\u5176\u6b21&quot;%c&quot;\u4f1a\u5c06\u7a7a\u683c\u548c\u6362\u884c\u7b26\u4e5f\u8bfb\u5165\u8fdb\u6765\uff0c\u6240\u4ee5\u8f93\u5165\u683c\u5f0f\u4e00\u5b9a\u8981\u8003\u8651\u5230\u91c7\u7528&quot;%c %c\\n&quot;\u7684\u5f62\u5f0f\uff0c\u7b2c\u4e00\u4e2a\u8bfb\u5165\u7684\u6570\u5b57\u4e5f\u7ed9\u5b83\u628a\u6362\u884c\u7b26\u62ff\u6389\uff01<\/strong><\/p>\n<pre><code class=\"language-cpp\">#include &lt;iostream&gt;\n#include &lt;unordered_set&gt;\n#include &lt;bits\/stdc++.h&gt;\n#include &lt;queue&gt;\nusing namespace std;\n\nconst int maxn = 10;\nint root = 0, n;\nbool node[maxn];\nint lNode[maxn];\nint rNode[maxn];\nqueue&lt;int&gt; q;\n\nvoid invertLevelTraverse() {\n    int c, l, r, cnt = n;\n    q.push(root);\n    while (!q.empty()) {\n        c = q.front();\n        q.pop();\n        if (--cnt)  printf(&quot;%d &quot;, c);\n        else        printf(&quot;%d\\n&quot;, c);\n        l = lNode[c]; r = rNode[c];\n        if (l != -1) q.push(l);\n        if (r != -1) q.push(r);\n    }\n}\n\nvoid invertInTraverse(int c) {\n    if (c == -1) return;\n    invertInTraverse(lNode[c]);\n    if (--n) printf(&quot;%d &quot;, c);\n    else printf(&quot;%d\\n&quot;, c);\n    invertInTraverse(rNode[c]);\n}\n\nint main() {\n    char l, r;\n    scanf(&quot;%d\\n&quot;, &amp;n);\n    memset(lNode, -1, sizeof(lNode));\n    memset(rNode, -1, sizeof(rNode));\n\n    for (int i = 0; i &lt; n; i++) {\n        scanf(&quot;%c %c\\n&quot;, &amp;l, &amp;r);\n        if (l == &#039;-&#039; &amp;&amp; r == &#039;-&#039;) continue;\n        if (l != &#039;-&#039;) {\n            node[l - &#039;0&#039;] = true;\n            rNode[i] = l - &#039;0&#039;;\n        }\n        if (r != &#039;-&#039;) {\n            node[r - &#039;0&#039;] = true;\n            lNode[i] = r - &#039;0&#039;;\n        }\n    }\n\n    for (; node[root]; root++);\n    invertLevelTraverse();\n    invertInTraverse(root);\n\n    return 0;\n}<\/code><\/pre>\n<h3>A1106 \u6700\u5c0f\u6df1\u5ea6\u8282\u70b9<\/h3>\n<p>\u672c\u9898\u540cA1094<\/p>\n<p>(\u2267\u2207\u2266)\uff89 <strong>Q\uff1a\u6211\u4e00\u6b21\u5c31AC\u4e86\uff01<\/strong><\/p>\n<p>=\uffe3\u03c9\uffe3= <strong>A\uff1a\u68d2\u54e6\uff01\u4e0d\u8fc7\u6709\u4e24\u4e2a\u70b9\u8981\u63d0\u9192\u4e00\u4e0b\u3002\u4e00\u662fINT<em>MAX\u5728\u5224\u9898\u73af\u5883\u4e2d\u5e94\u4f7f\u7528\\<\/em>_INT<em>MAX_\\<\/em>\uff0c\u5426\u5219\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\uff1b\u4e8c\u662f\u4e00\u4e2a\u5c0f\u5c0f\u7684\u4f18\u5316\uff0c\u53f6\u8282\u70b9\u5904\u53ea\u6bd4\u8f83\u6df1\u5ea6\uff0c\u4ef7\u683c\u53ea\u5728\u5168\u5c40\u6700\u5c0f\u65f6\u8ba1\u7b97\u4e00\u6b21\u5c31OK<\/strong><\/p>\n<pre><code class=\"language-cpp\">#include &lt;iostream&gt;\n#include &lt;vector&gt;\n#include &lt;cmath&gt;\nusing namespace std;\n\nconst int maxn = 100001;\nvector&lt;int&gt; tree[maxn];\nint lowL = __INT_MAX__;\nint cnt = 0;\n\nvoid DFS(int root, int level) {\n    if (tree[root].size() &lt;= 0) {\n        if (level &lt; lowL) {\n            lowL = level;\n            cnt = 1;\n        }\n        else if (level == lowL)\n            cnt++;\n        return;\n    }\n\n    for (int &amp;x : tree[root])\n        DFS(x, level + 1);\n}\n\nint main() {\n    int n, s, q;\n    double P, r;\n    scanf(&quot;%d %lf %lf&quot;, &amp;n, &amp;P, &amp;r);\n    r \/= 100;\n\n    for (int i = 0; i &lt; n; i++) {\n        scanf(&quot;%d&quot;, &amp;q);\n        while (q--) {\n            scanf(&quot;%d&quot;, &amp;s);\n            tree[i].push_back(s);\n        }\n    }\n\n    DFS(0, 0);\n    printf(&quot;%.4f %d\\n&quot;, P * pow(1 + r, lowL), cnt);\n\n    return 0;\n}<\/code><\/pre>\n<h2>\u4e8c\u53c9\u641c\u7d22\u6811<\/h2>\n<h3>A1043 \u5efa\u7acb\u548c\u904d\u5386<\/h3>\n<p>\u672c\u9898\u4e3b\u8981\u8003\u5bdf\u7684\u662fBST\u7684\u5efa\u7acb\u548c\u904d\u5386\uff0c\u8981\u4ece\u9898\u8bbe\u548c\u6837\u4f8b\u89e3\u8bfb\u51fa\u8f93\u5165\u5e8f\u5217\u5373\u4e3a\u5efa\u6811\u63d2\u5165\u8282\u70b9\u987a\u5e8f\u3002\u672c\u9898\u8981\u5206\u6e05\u695a <strong>\u5148\u5e8f\u3001\u540e\u5e8f\u904d\u5386\u548c\u955c\u50cf\u904d\u5386\u7684\u533a\u522b<\/strong>\uff0c\u4e0d\u8981\u6df7\u6dc6\uff1a\u5148\u540e\u5e8f\u904d\u5386\u662f\u6307\u6839\u8282\u70b9\u64cd\u4f5c\u5728\u9012\u5f52\u4e4b\u524d\u8fd8\u662f\u9012\u5f52\u4e4b\u540e\uff0c\u955c\u50cf\u904d\u5386\u662f\u6307\u5148\u9012\u5f52\u5de6\u8282\u70b9\u8fd8\u662f\u5148\u9012\u5f52\u53f3\u8282\u70b9<\/p>\n<p>(\u30fb\u2200\u30fb(\u30fb\u2200\u30fb(\u30fb\u2200\u30fb*) <strong>Q\uff1a\u548c\u6211\u5728Robert Sedgewick\u8001\u7237\u5b50\u4e66\u4e0a\u770b\u5230\u7684BST\u4e0d\u592a\u4e00\u6837\uff1f<\/strong><\/p>\n<p>\u2570(\uffe3\u03c9\uffe3\uff4f) <strong>A\uff1a\u5bf9\u7684\u3002\u672c\u9898\u5728BST\u5efa\u7acb\u65f6\uff0c\u5141\u8bb8\u91cd\u590d\u8282\u70b9\u7684\u5b58\u5728\uff0c\u89c6\u65b0\u8282\u70b9 $\\scriptsize \\geqslant$ \u540c\u503c\u8282\u70b9\uff0c\u6ce8\u610f\u8bfb\u9898\uff01\u4e0d\u8fc7\u6211\u5efa\u6811\u4f7f\u7528\u7684\u662f\u8001\u7237\u5b50\u7684\u9012\u5f52\u7248\u672c\u54e6~<\/strong><\/p>\n<p>(o\u309c\u25bd\u309c)o\u2606  <strong>Q\uff1a\u6211\u6709\u66f4\u597d\u7684\u5efa\u6811\u65b9\u6cd5\uff0c\u53ef\u4ee5\u51cf\u5c11\u9012\u5f52\u8fd4\u56de\u4f20\u53c2\u3002\u6301\u6709root-&gt;child\u7684\u6307\u9488\u5c06\u5176\u6307\u5411\u65b0\u8282\u70b9new TreeNode(val)\uff0c\u4ee3\u7801\u5982\u4e0b<\/strong><\/p>\n<pre lang=\"cpp\">\nvoid insert(TreeNode *&root, int val) {\n    if(root == nullptr) {\n        root = new TreeNode(val);\n        return;\n    }\n    if(val < root->val) insert(root->left, val);\n    else insert(root->right, val);\n}\n<\/pre>\n<p>o\u0316\u2e1c((\u0335\u0301\u0335 \u0306\u0352\u035f\u02da\u0329\u032d \u0306\u0352)\u0335\u0300\u0335)\u2e1do\u0317 <strong>A\uff1a\u6765\u81ea\u84dd\u80d6\u7684\u9f13\u638c\uff01<\/strong><\/p>\n<p>\u1566(\uff65\u3142\uff65)\u1564 <strong>Q\uff1a\u7334\u91cc\u5b69\u54e6~\u8fb9\u5148\u5e8f\u904d\u5386\u8fb9\u548c\u8f93\u5165\u5e8f\u5217\u6bd4\u8f83<\/strong><\/p>\n<p>(\u0e51\u2022\u0301 \u2200 \u2022\u0300\u0e51) <strong>A\uff1a\u90a3\u5f53\u7136\uff01\u7b80\u4fbf\u65b9\u6cd5\u662f\u9012\u5f52\u5148\u5e8f\u904d\u5386\u5168\u90e8\u8282\u70b9\uff0c\u7528vector\u4fdd\u5b58\u904d\u5386\u7ed3\u679c\uff0c\u518d\u4e0e\u8f93\u5165\u5e8f\u5217vector\u6bd4\u8f83\u3002\u8fd9\u91cc\u505a\u4e86\u4e00\u4e2a\u5c0f\u4f18\u5316\uff0c\u9898\u8bbe\u4ec5\u5224\u65ad\u662f\u4e0d\u662f\u5373\u53ef\uff0c\u56e0\u6b64\u65e0\u9700\u904d\u5386\u6240\u6709\u8282\u70b9\uff1a\u7531\u4e8e&amp;&amp;\u7684\u77ed\u8def\u7279\u6027\uff0c\u53ea\u8981\u5de6\u5b50\u7ed3\u679c\u4e3afalse\uff0c\u5c31\u4f1a\u8282\u7701\u4e0d\u5fc5\u8981\u7684\u53f3\u5b50\u9012\u5f52<\/strong><\/p>\n<pre><code class=\"language-cpp\">#include &lt;iostream&gt;\nusing namespace std;\n\nconst int maxn = 1000;\nint N, cnt = 0;\nint pre[maxn];\n\nstruct TreeNode {\n    int val;\n    TreeNode *left;\n    TreeNode *right;\n    TreeNode(int v) : val(v), left(nullptr), right(nullptr) {}\n};\n\nTreeNode *insert(int val, TreeNode *root) {\n    if (root == nullptr) return new TreeNode(val);\n    if (val &lt; root-&gt;val) root-&gt;left = insert(val, root-&gt;left);\n    else root-&gt;right = insert(val, root-&gt;right);\n    return root;\n}\n\nbool PreTraverse(TreeNode *root) {\n    if (root == nullptr) return true;\n    if (root-&gt;val == pre[cnt++])\n        return PreTraverse(root-&gt;left) &amp;&amp; PreTraverse(root-&gt;right);\n    else return false;\n}\n\nbool MirrorTraverse(TreeNode *root) {\n    if (root == nullptr) return true;\n    if (root-&gt;val == pre[cnt++])\n        return MirrorTraverse(root-&gt;right) &amp;&amp; MirrorTraverse(root-&gt;left);\n    else return false;\n}\n\nvoid prePostPrint(TreeNode *root) {\n    if (root == nullptr) return;\n    prePostPrint(root-&gt;left);\n    prePostPrint(root-&gt;right);\n    if (--N) printf(&quot;%d &quot;, root-&gt;val);\n    else printf(&quot;%d\\n&quot;, root-&gt;val);\n}\n\nvoid mirPostPrint(TreeNode * root) {\n    if (root == nullptr) return;\n    mirPostPrint(root-&gt;right);\n    mirPostPrint(root-&gt;left);\n    if (--N) printf(&quot;%d &quot;, root-&gt;val);\n    else printf(&quot;%d\\n&quot;, root-&gt;val);\n}\n\nint main() {\n    int v;\n    TreeNode *root = nullptr;\n    scanf(&quot;%d&quot;, &amp;N);\n    for (int i = 0; i &lt; N; i++) {\n        scanf(&quot;%d&quot;, &amp;v);\n        pre[i] = v;\n        root = insert(v, root);\n    }\n\n    if (PreTraverse(root)) {\n        printf(&quot;YES\\n&quot;);\n        prePostPrint(root);\n    }\n    else if (MirrorTraverse(root)) {\n        cnt = 0;\n        printf(&quot;YES\\n&quot;);\n        mirPostPrint(root);\n    }\n    else printf(&quot;NO\\n&quot;);\n\n    return 0;\n}<\/code><\/pre>\n<h3>* A1064 \u4e2d\u5e8f\u5efa\u7acbBST<\/h3>\n<p>\u9898\u76ee\u672a\u7ed9\u5efa\u6811\u5e8f\u5217\u548c\u5efa\u6811\u65b9\u5f0f\uff0c\u53ea\u7ed9\u5c42\u5e8f\u7ed3\u679c\u5e76\u544a\u77e5\u662fBST\uff0c\u5e94\u662f\u8003\u5bdfBST\u6027\u8d28\uff1a\u4e2d\u5e8f\u8bbf\u95ee\u662f\u9012\u589e\u5e8f\u5217\u3002\u4e2d\u5e8f\u8bbf\u95ee\u7a7a\u6811\u65f6\u5c06\u8282\u70b9\u4ece\u5c0f\u5230\u5927\u4f9d\u6b21\u63d2\u5165\u5c31\u6784\u6210\u4e86\u6ee1\u8db3BST\u6027\u8d28\u7684CBT\uff0c\u7531\u6b64\u9700\u5bf9\u5f85\u63d2\u5143\u7d20\u5e8f\u5217\u6392\u5e8f<\/p>\n<p>\u1d98 \u1d52\u1d25\u1d52\u1d85  <strong>Q\uff1a\u8fd9\u4e2a\u65b9\u6cd5\u771f\u5fc3\u6ca1\u60f3\u5230<\/strong><\/p>\n<p>(o\u00b4\u03c9`o) <strong>A\uff1a\u7406\u89e3\u4e0d\u900f\u5f7b\uff0c\u4e2d\u5e8f\u8bbf\u95ee\u662f\u9012\u589e\u5e8f\u5217\u610f\u5473\u7740\uff1a\u4e2d\u5e8f\u64cd\u4f5c\u7684\u8bbf\u95ee\u6b21\u5e8f\u4e0e\u5143\u7d20\u9012\u589e\u6b21\u5e8f\u4e00\u81f4\uff0c\u4e5f\u5373\u4e2d\u5e8f\u64cd\u4f5c\u6240\u5728\u4f4d\u7f6e\u5c31\u662f\u5bf9\u5e94\u7684\u9012\u589e\u5143\u7d20\u5728BST\u4e2d\u7684\u4f4d\u7f6e\uff0c\u89c2\u5bdf\u8be6\u7ec6\u8fc7\u7a0b\u66f4\u5bb9\u6613\u7406\u89e3\uff1a\u9012\u5f52\u8fc7\u7a0b\u4e2d\u7b2c\u4e00\u6b21\u4e2d\u5e8f\u64cd\u4f5c\u662f\u5728\u6700\u5c0f\u8282\u70b9\u6240\u5728\u4f4d\u7f6e\uff0c\u7b2c\u4e8c\u6b21\u662f\u5728\u6b21\u5c0f\u8282\u70b9\u4f4d\u7f6e\u3002\u8fd8\u662f\u5237\u9898\u4e0d\u591f\u591a\uff0c\u8981\u591a\u79ef\u7d2f\u7ecf\u9a8c\uff01<\/strong><\/p>\n<p>(\u2261\u2022\u0300\u00b7\u032f\u2022\u0301\u2261)  <strong>A\uff1a\u4ec5\u6709\u6ee1\u4e8c\u53c9\u6811\u548c\u5806\u624d\u4f1a\u8003\u8651\u4f7f\u7528\u4ee51\u5f00\u59cb\u7684\u6570\u7ec4\u8868\u793a\u65b9\u5f0f\uff0c\u7236\u8282\u70b9\u8868\u793a\u4e3ai\/2\uff0c\u5b50\u8282\u70b9\u8868\u793a\u4e3a2i\u30012i+1\uff0c\u8bbf\u95ee\u7b80\u5355\u4e14\u6700\u5927\u9650\u5ea6\u8282\u7701\u7a7a\u95f4\u3002\u6ce8\u610f\u533a\u5206odr\u548ccbt\u54e6\uff0codr\u662f\u4e0b\u6807\u4ece0\u5f00\u59cb\u7684\u6392\u5e8f\u540e\u7684\u9012\u589e\u6570\u5b57\u5e8f\u5217\uff0ccbt\u662f\u4e0b\u6807\u4ece1\u5f00\u59cb\u7684\u6ee1\u4e8c\u53c9\u641c\u7d22\u6811<\/strong><\/p>\n<p>\u1d98 \u1d52\u1d25\u1d52\u1d85  <strong>Q\uff1a\u5b66\u5230\u4e86<\/strong><\/p>\n<pre><code class=\"language-cpp\">#include &lt;iostream&gt;\n#include &lt;algorithm&gt;\nusing namespace std;\n\nconst int maxn = 1001;\nint cbt[maxn];\nint odr[maxn];\nint N, j = 0;\n\nvoid inTraverse(int i) {\n    if (i &gt; N) return;\n    inTraverse(2 * i);\n    cbt[i] = odr[j++];\n    inTraverse(2 * i + 1);\n}\n\nint main() {\n    int t, i;\n    scanf(&quot;%d&quot;, &amp;N);\n    for (i = 0; i &lt; N; i++)\n        scanf(&quot;%d&quot;, &amp;odr[i]);\n\n    sort(odr, odr + N);\n    inTraverse(1);\n\n    for (i = 1; i &lt; N; i++)\n        printf(&quot;%d &quot;, cbt[i]);\n    printf(&quot;%d\\n&quot;, cbt[N]);\n    return 0;\n}<\/code><\/pre>\n<h3>A1099 \u4e2d\u5e8f\u5efa\u7acbBST<\/h3>\n<p>\u672c\u9898\u601d\u8def\u4e0eA1064\u5b8c\u5168\u76f8\u540c\u3002\u4e0d\u540c\u70b9\u5728\u4e8e\u672c\u9898\u6240\u7ed9\u7ed3\u6784\u662f\u9759\u6001\u4e8c\u53c9\u6811\uff0c\u800c\u975e\u6ee1\u4e8c\u53c9\u6811\uff0c\u56e0\u800c\u6570\u636e\u7ed3\u6784\u9009\u7528\u7ed3\u6784\u4f53\u6570\u7ec4\uff0c\u76f8\u5e94\u7684\u4e2d\u5e8f\u3001\u5c42\u5e8f\u904d\u5386\u5747\u6539\u7528\u901a\u7528\u65b9\u6cd5<\/p>\n<pre><code class=\"language-cpp\">#include &lt;iostream&gt;\n#include &lt;algorithm&gt;\n#include &lt;queue&gt;\nusing namespace std;\n\nconst int maxn = 101;\nint N, odr[maxn], j = 0;\n\nstruct TreeNode {\n    int val;\n    int left;\n    int right;\n} tree[maxn];\n\nvoid inOrder(int i) {\n    if (i == -1) return;\n    inOrder(tree[i].left);\n    tree[i].val = odr[j++];\n    inOrder(tree[i].right);\n}\n\nint main() {\n    int i;\n    queue&lt;int&gt; q;\n    scanf(&quot;%d&quot;, &amp;N);\n    for (i = 0; i &lt; N; i++)\n        scanf(&quot;%d %d&quot;, &amp;tree[i].left, &amp;tree[i].right);\n    for (i = 0; i &lt; N; i++)\n        scanf(&quot;%d&quot;, &amp;odr[i]);\n\n    sort(odr, odr + N);\n    inOrder(0);\n\n    q.push(0);\n    while (!q.empty()) {\n        i = q.front();\n        q.pop();\n        if (--N)    printf(&quot;%d &quot;, tree[i].val);\n        else        printf(&quot;%d\\n&quot;, tree[i].val);\n        if (tree[i].left != -1)  q.push(tree[i].left);\n        if (tree[i].right != -1) q.push(tree[i].right);\n    }\n    return 0;\n}<\/code><\/pre>\n<h2>\u5e76\u67e5\u96c6<\/h2>\n<h3>A1107 \u5206\u7ec4topK<\/h3>\n<p>\u9898\u610f\u4e3a\u6709\u5171\u540c\u7231\u597d\u7684\u4eba\u5206\u4e3a\u4e00\u7ec4\uff0c\u6c42\u5206\u7ec4\u603b\u6570\u548c\u6bcf\u7ec4\u4eba\u6570\u3002\u5e76\u67e5\u96c6\u9488\u5bf9\u5206\u7ec4\u95ee\u9898\u548c\u56fe\u7684\u8fde\u901a\u6027\u95ee\u9898\uff0c\u8be6\u7ec6\u89e3\u6790\u53c2\u8003\u672c\u535a\u5ba2<a href=\"http:\/\/47.101.202.111\/2020\/01\/12\/%e7%ae%97%e6%b3%95-%e5%b9%b6%e6%9f%a5%e9%9b%86-union-find\/#Path_Compression_Quick_Union\">\u7406\u8bba\u8bb2\u89e3<\/a><br \/>\n\u7c7btopK\u95ee\u9898\u53ef\u7528\u5806\u5904\u7406\uff0c\u4f7f\u7528queue\u5e93\u7684\u6570\u636e\u7ed3\u6784priority_queue\uff0c\u5b9e\u73b0\u81ea\u5b9a\u4e49\u6392\u5e8f\u7684\u58f0\u660e\u5199\u6cd5\u5982\u4e0b<\/p>\n<pre lang=\"cpp\">\n\/**\n  * @parameter \u6570\u636e\u7c7b\u578b\n  * @parameter \u5b58\u50a8\u5bb9\u5668\n  * @parameter \u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\n  *\/\npriority_queue<int, vector<int>, cmp> q;\n<\/pre>\n<p>\u30fc( \u00b4 \u25bd ` )\uff89 <strong>Q\uff1a\u5e76\u67e5\u96c6\u4e2d\u7684id\u6570\u7ec4\u5728\u8fd9\u91cc\u4ee3\u8868\u4ec0\u4e48\uff1f<\/strong><\/p>\n<p>\u03b5-(=\uff40\u03c9\u00b4=) A\uff1a<strong>id\u6570\u7ec4\u7d22\u5f15\u4ee3\u8868\u5e76\u67e5\u96c6\u4e2d\u5206\u7ec4\u5bf9\u8c61\u7684\u6807\u53f7\uff0c\u503c\u4ee3\u8868\u6240\u5c5e\u6811\u7684\u6839\u8282\u70b9\u7d22\u5f15\uff0c\u672c\u9898\u4e2d\u4ee3\u8868\u88ab\u5206\u7ec4\u7684\u4eba\u7684\u6807\u53f7<\/strong><\/p>\n<p>\u3023( \u00ba\u0394\u00ba )\u3023 <strong>Q\uff1a\u600e\u4e48\u8fdb\u884c\u4eba\u5458\u5206\u7ec4\uff1f<\/strong><\/p>\n<p>( \u2022o\u2022) <strong>A\uff1a\u4ed4\u7ec6\u8bfb\u9898\uff0c\u5e76\u6839\u636e\u6837\u4f8b\u7406\u89e3\u9898\u610f\uff0c\u62e5\u6709\u5171\u540c\u7231\u597d\u7684\u4eba\u5206\u4e3a\u4e00\u7ec4\uff1a\u4e00\u79cd\u601d\u8def\u662f\u5efa\u7acb\u4e00\u4e2a\u6570\u636e\u7ed3\u6784\u4fdd\u5b58\u540c\u6837\u7231\u597d\u7684\u4eba\uff0cmap<Key=\u7231\u597d\u6807\u53f7\uff0cVal=\u4eba\u5458\u96c6\u5408>\u518d\u9002\u5408\u4e0d\u8fc7\uff0c\u7231\u597d\u96c6\u5408\u4e2d\u53ea\u6709\u4e00\u4e2a\u4eba\u8bf4\u660e\u4e0d\u662f\u516c\u5171\u7231\u597d\u65e0\u9700\u5206\u7ec4\uff0c\u5426\u5219\u5c06\u5176\u4f9d\u6b21\u4e0e\u96c6\u5408\u4e2d\u7b2c\u4e00\u4e2a\u4eba\u5206\u4e3a\u540c\u4e00\u7ec4\uff1b\u66f4\u7701\u5185\u5b58\u7684\u601d\u8def\u662fmap\u4ec5\u4fdd\u5b58\u7b2c\u4e00\u4e2a\u62e5\u6709\u8be5\u7231\u597d\u7684\u4eba\uff0c\u6bcf\u6b21\u90fd\u5c06\u6620\u5c04\u76f8\u540c\u7684\u4eba\u4e0e\u8be5\u4eba\u5206\u7ec4<\/strong><\/p>\n<p>\u03a3( \u2686\u0d67\u2686) <strong>Q\uff1a\u5206\u7ec4\u603b\u6570\u600e\u4e48\u7b97\uff1f<\/strong><\/p>\n<p>( \u00b4\u0348 \u1d55 `\u0348 )\u25de\u2661 <strong>A\uff1a\u4e24\u79cd\u65b9\u6cd5\uff1a\u7ecf\u5178\u65b9\u6cd5\u662f\u6307\u5b9a\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\uff0c\u521d\u59cb\u6bcf\u4e2a\u5bf9\u8c61\u81ea\u6210\u4e00\u7ec4\uff0c\u6bcf\u8fdb\u884c\u4e00\u6b21\u5408\u5e76\u64cd\u4f5c\u51cf\u5c11\u4e00\u7ec4\uff0c\u5e76\u67e5\u96c6\u5efa\u7acb\u5b8c\u6bd5\u76f4\u63a5\u53ef\u8bfb\u53d6\u6570\u76ee\uff1b\u53e6\u5916\u4e00\u79cd\u662f\u5e76\u67e5\u96c6\u5efa\u7acb\u540e\uff0c\u904d\u5386\u6240\u6709\u8282\u70b9\u5e76\u7edf\u8ba1\u6839\u8282\u70b9\u7684\u6570\u76ee\u5373\u5f97\u5206\u7ec4\u603b\u6570\uff0c\u6839\u8282\u70b9\u7684\u5224\u5b9a\u4e3ai=id[i]<\/strong><\/p>\n<p>|\u0434\uff65)\u3063 <strong>Q\uff1a\u7ec4\u5185\u6570\u91cf\u600e\u4e48\u7b97\uff1f<\/strong><\/p>\n<p>o(\uff40\u03c9\u00b4 )o <strong>A\uff1a\u4e24\u79cd\u65b9\u6cd5\uff1a\u7ecf\u5178\u65b9\u6cd5\u662f\u6307\u5b9a\u4e00\u4e2a\u6570\u7ec4\u4fdd\u5b58\u5404\u8282\u70b9\u8868\u793a\u7684\u6811\u5927\u5c0f\uff0c\u521d\u59cb\u6bcf\u4e2a\u5bf9\u8c61\u81ea\u6210\u4e00\u7ec4\uff0c\u6bcf\u5408\u5e76\u65f6\u4fee\u6539\u6839\u8282\u70b9\u6811\u5927\u5c0f\uff1b\u53e6\u5916\u4e00\u79cd\u662f\u5e76\u67e5\u96c6\u5efa\u7acb\u540e\uff0c\u904d\u5386\u7d2f\u52a0\u540c\u6839\u8282\u70b9\u7684\u6570\u76ee\u5373\u5f97\u5206\u7ec4\u603b\u6570<\/strong><\/p>\n<p>o(\uff40\u03c9\u00b4 )o <strong>Q\uff1a\u5206\u7ec4\u6570\u91cf\u600e\u4e48\u6392\u5e8f\uff1f<\/strong><\/p>\n<p>\u0e51\u4e5b\u25e1\u4e5b\u0e51 <strong>A\uff1a\u5176\u5b9e\u6392\u7684\u662f\u5c11\u6570\u51e0\u4e2a\u6839\u8282\u70b9\u6240\u5bf9\u5e94\u7684\u6811\u7684\u5927\u5c0f\uff0c\u5efa\u7acb\u5e76\u67e5\u96c6\u4e4b\u540e\uff0c\u904d\u5386\u6240\u6709\u8282\u70b9\uff0c\u68c0\u67e5\u4ec5\u5c06\u6839\u8282\u70b9\u5165\u5806\u5373\u53ef<\/strong><\/p>\n<p>\u253b\u2501\u253b (\u30fd(`\u0414 \u0301)\u30ce( \u253b\u2501\u253b <strong>Q\uff1a\u8def\u5f84\u538b\u7f29\u7684\u5199\u6cd5\u6709\u70b9\u5947\u602a\uff1f<\/strong><\/p>\n<p>(\u2229\u2022\u0300\u03c9\u2022\u0301)\u2283-*\u22c6 <strong>A\uff1a\u770b\u60ef\u4e86Robert\u8001\u7237\u5b50\u7684\u9012\u5f52\u7248\u672c\uff0c\u4e00\u4e0b\u5b50\u662f\u4e0d\u662f\u6709\u70b9\u9002\u5e94\u4e0d\u8fc7\u6765\u4e86hhh\uff1f\u8fd9\u91cc\u6539\u4e3a\u8fed\u4ee3\u7248\u672c\u4e86\uff0c\u5c31\u4e24\u4e2a\u64cd\u4f5c\uff1a\u628a\u5f53\u524d\u8282\u70b9\u6307\u5411\u6811\u6839\uff0c\u628a\u5b50\u6811\u7684\u8282\u70b9\u4e5f\u6307\u5411\u6811\u6839<\/strong><\/p>\n<pre><code class=\"language-cpp\">#include &lt;iostream&gt;\n#include &lt;queue&gt;\n#include &lt;unordered_map&gt;\n#include &lt;vector&gt;\nusing namespace std;\n\nconst int maxn = 1001;\nint N, num, t, i, cnt;\nunordered_map&lt;int, vector&lt;int&gt;&gt; mp;\n\nint sz[maxn];\nint id[maxn];\n\nint find(int j) {\n    int t0 = j, t1;\n    while (j != id[j])\n        j = id[j];\n    while (t0 != id[t0]) {\n        t1 = t;\n        t0 = id[t0];\n        id[t0] = j;\n    }\n    return j;\n}\n\nvoid insert(int p, int q) {\n    int pRoot = find(p);\n    int qRoot = find(q);\n    if (pRoot == qRoot) return;\n    if (pRoot &gt;= qRoot) {\n        id[qRoot] = pRoot;\n        sz[pRoot] += sz[qRoot];\n    }\n    else {\n        id[pRoot] = qRoot;\n        sz[qRoot] += sz[pRoot];\n    }\n    cnt--;\n}\n\nint main() {\n    scanf(&quot;%d&quot;, &amp;N);\n    for (i = 1; i &lt;= N; i++) {\n        sz[i] = 1;\n        id[i] = i;\n    }\n    cnt = N;\n    for (i = 1; i &lt;= N; i++) {\n        scanf(&quot;%d:&quot;, &amp;num);\n        while (num--) {\n            scanf(&quot;%d&quot;, &amp;t);\n            mp[t].push_back(i);\n        }\n    }\n\n    for (unordered_map&lt;int, vector&lt;int&gt;&gt;::iterator it = mp.begin(); it != mp.end(); it++) {\n        vector&lt;int&gt; val = it-&gt;second;\n        int first = val[0];\n        if (val.size() &gt; 1)\n            for (i = 1; i &lt; val.size(); i++)\n                insert(first, val[i]);\n    }\n    printf(&quot;%d\\n&quot;, cnt);\n\n    priority_queue&lt;int&gt; q;\n    for (i = 1; i &lt;= N; i++)\n        if (i == id[i]) q.push(sz[i]);\n    for (i = 1; i &lt; cnt; i++) {\n        printf(&quot;%d &quot;, q.top());\n        q.pop();\n    }\n    printf(&quot;%d\\n&quot;, q.top());\n\n    return 0;\n}<\/code><\/pre>\n<h2>\u5806<\/h2>\n<h3>A1098 \u5806\u6392\u548c\u63d2\u6392<\/h3>\n<p>\u5b9e\u9645\u662f\u4e24\u79cd\u6392\u5e8f\u7b97\u6cd5\uff0c\u53ea\u662f\u5728\u6bcf\u4e00\u8f6e\u64cd\u4f5c\u540e\u6bd4\u8f83\u5f85\u6392\u5e8f\u5217\u662f\u5426\u662f\u6b64\u6b21\u64cd\u4f5c\u7ed3\u679c\u3002\u6392\u5e8f\u7b97\u6cd5\u8be6\u89c1<a href=\"http:\/\/47.101.202.111\/2019\/12\/16\/algorithm-sort\/\">\u7406\u8bba\u8bb2\u89e3<\/a><br \/>\n\u63d2\u5165\u6392\u5e8f\u662f\u6bcf\u6b21\u5c06\u5e8f\u5217\u53f3\u4fa7\u5143\u7d20\u63d2\u56de\u5df2\u6392\u5e8f\u5217\u3002<br \/>\n\u5806\u6392\u5e8f\u8981\u5efa\u5806\u3001\u8c03\u5806\u3001\u6392\u5e8f\u3002\u5206\u522b\u662f\uff1a\u521d\u59cb\u5e8f\u5217\u5efa\u5806\uff1bN\/2\u9012\u51cf\u4e0b\u6c89\u8c03\u5806\uff1b\u9876max\u6362\u672b\u5c3e\u6392\u5e8f\uff0c\u9876\u5143\u7d20\u4e0b\u6c89\u8c03\u5806\u3002\u8981\u7262\u8bb0\u4e0a\u6d6e\u548c\u4e0b\u6c89\u7b97\u6cd5\u7ec6\u8282\u3002<\/p>\n<p>\u252d\u252e\ufe4f\u252d\u252e <strong>Q\uff1a\u6211\u5199\u7684\u4ee3\u7801\u597d\u5197\u957f\u554a\uff0c\u6709\u70b9\u51cc\u4e71\uff0c\u5bbd\u9762\u6761\u6cea~<\/strong><\/p>\n<p>\u0669\u030b(\u02ca\u2022\u0348 \ua1f4 \u2022\u0348\u02cb)\u0648 <strong>A\uff1a\u4e24\u79cd\u65b9\u6cd5\u9664\u6392\u5e8f\u8fc7\u7a0b\u5916\u5176\u5b9e\u662f\u76f8\u540c\u7684\uff0c\u6bd4\u5982\u5e8f\u5217\u76f8\u540c\u5224\u5b9a\u3001\u5e8f\u5217\u8f93\u51fa\u4e24\u4e2a\u529f\u80fd\u53ef\u4ee5\u62bd\u51fa\u51fd\u6570\u5171\u7528\uff0c\u51cf\u5c11\u5de5\u4f5c\u91cf\u3002\u4e3a\u7701\u53bb\u9ebb\u70e6\uff0c\u5e8f\u5217\u4e0b\u6807\u7edf\u4e00\u4ece1\u5f00\u59cb\uff0c\u51cf\u5c11\u51fa\u9519\u53ef\u80fd\u6027\u3002\u5426\u5219\u4e00\u4e2ai\u53e6\u4e00\u4e2ai-1\u4e0d\u4ec5\u9ebb\u70e6\u800c\u4e14\u5f88\u5bb9\u6613\u5fd8\u8bb0\uff01<\/strong><\/p>\n<p>(\u0942\u02c3\u0323\u0323\u0323\u0323\u0323\u0323\ufe3f\u02c2\u0323\u0323\u0323\u0323\u0323\u0323 \u0942) <strong>Q\uff1a\u5806\u6392\u8fc7\u7a0b\u6709\u4e2aN&#8211;\u90e8\u5206\uff0c\u6bcf\u6b21\u90fd\u5199\u4e0d\u5bf9\uff0c\u843d\u96e8\u6cea~<\/strong><\/p>\n<p>((\ua706\ua704\u2022\u0bf0\u2022)\ua706\ua704 <strong>A\uff1a\u8fd9\u91cc\u4e3b\u8981\u662f\u7406\u89e3\u7b97\u6cd5\u3002\u4fdd\u7559N\u662f\u8981\u4e92\u6362\u5806\u9876a[1]\u548c\u5806\u672ba[N]\uff0c\u4e92\u6362\u4e4b\u540ea[N]\u5df2\u7ecf\u6709\u5e8f\u4e86\uff0c\u8fd9\u4e2a\u65f6\u5019\u5806\u9876\u7684\u4e0b\u6c89\u64cd\u4f5c\u4e00\u5b9a\u4e0d\u80fd\u518d\u53bb\u5e72\u6270a[N]\uff0c\u5e8f\u5217\u8fb9\u754c\u4e3a[1,N-1]\u3002\u6240\u4ee5\u5e94\u8be5\u5728\u4e92\u6362\u540e\u3001\u4e0b\u6c89\u64cd\u4f5c\u524d\u5c06N&#8211;\uff0cN\u5982\u679c\u522b\u6709\u4ed6\u7528\u7684\u8bdd\u53e6\u5916\u7528\u4e2a\u53d8\u91cf\u4fdd\u5b58\u4f20i-1<\/strong><\/p>\n<p>(\u3064\u0414\uff40)\uff65\uff9f\uff65 <strong>Q\uff1a\u8f93\u51fa\u4e0b\u6b21\u6392\u5e8f\u7ed3\u679c\u4e0d\u4f1a\u5b9e\u73b0\uff0c\u5624\u5624\u6cea~<\/strong><\/p>\n<p>(\uff61\u2022\u0300\u1d17-)\u2727 <strong>A\uff1a\u554a\u8fd9\u4e2a\uff01\u6bcf\u8f6e\u6392\u5e8f\u540e\u90fd\u4f1a\u6bd4\u8f83\u5e8f\u5217\uff0c\u6765\u5224\u65ad\u662f\u5426\u6240\u6c42\u6b65\u9aa4\u3002\u786e\u5b9a\u662f\u6240\u6c42\u6b65\u9aa4\u7684\u65f6\u5019\u8bbe\u7f6e\u4e00\u4e2a\u6807\u5fd7\uff0c\u5728\u4e0b\u8f6e\u6392\u5e8f\u7ed3\u675f\u540e\u8bfb\u53d6\u8fd9\u4e2a\u6807\u5fd7\u8d70\u5206\u652f\u9000\u51fa\u5373\u53ef<\/strong><\/p>\n<p>(;\u00b4\u0f0e\u0eb6\u0414\u0f0e\u0eb6`) <strong>Q\uff1a\u5806\u6392\u7684\u7ed3\u679c\u51d1\u4e0d\u6210\u6837\u4f8b\uff0c\u5927\u54ed\u4e0d\u6b62~<\/strong><\/p>\n<p>\u10e6( \u00b4\uff65\u1d17\uff65` ) <strong>A\uff1aPAT\u4e2d\u4e00\u822c\u9996\u5148\u63a8\u8350\u672c\u535a\u5ba2\u63d0\u4f9b\u7684\u6807\u51c6\u5efa\u5806\u65b9\u6cd5\u548c\u8c03\u6574\u65b9\u6cd5\uff0c\u5c06\u8f93\u5165\u5e8f\u5217\u89c6\u4e3a\u5806\u4ece\u800c\u65e0\u9700\u5efa\u5806\u8fc7\u7a0b\uff0c\u5e76\u4eceN\/2\u5f00\u59cb\u5012\u5e8f\u4f9d\u6b21\u4e0b\u6c89\u8c03\u6574\u5806\u5e8f\uff0c\u8fd9\u6837\u64cd\u4f5c\u7684\u8282\u70b9\u6570\u76f8\u6bd4\u5176\u4ed6\u65b9\u6cd5\u5927\u5e45\u51cf\u5c11\u3002\u4f60\u53ef\u80fd\u5efa\u5806\u7684\u65f6\u5019\u662f\u4f9d\u6b21\u6dfb\u52a0\u8fdb\u6765\u7684\uff0c\u8c03\u6574\u5806\u5e8f\u662f\u4ece\u540e\u5411\u524d\u4f9d\u6b21\u4e0a\u6d6e\u6216\u8005\u4ece\u524d\u5411\u540e\u4f9d\u6b21\u4e0b\u6c89\u7684\uff0c\u8fd9\u6837\u786e\u5b9e\u4f1a\u5bfc\u81f4\u5806\u4e0d\u552f\u4e00\u800c\u4e0d\u7b26\u5408\u6837\u4f8b<\/strong><\/p>\n<pre><code class=\"language-cpp\">#include &lt;iostream&gt;\nusing namespace std;\n\nconst int maxn = 101;\nint ieq[maxn];\nint heq[maxn];\nint seq[maxn];\nint N;\n\nbool isEqual(int *a, int *b) {\n    int k = 1;\n    for (; k &lt; N; k++)\n        if (a[k] != b[k])\n            break;\n    if (k == N) return true;\n    return false;\n}\n\nvoid printSeq(int *a) {\n    int k = 1;\n    for (; k &lt; N; k++)\n       printf(&quot;%d &quot;, a[k]);\n    printf(&quot;%d\\n&quot;, a[k]);\n}\n\nbool iSort() {\n    int i, j, k;\n    bool go = false;\n    for (i = 2; i &lt;= N; i++) {\n        for (j = i; j &gt;= 1; j--)\n            if (ieq[j] &lt; ieq[j - 1])\n                swap(ieq[j], ieq[j - 1]);\n\n        if (go) {\n            printf(&quot;Insertion Sort\\n&quot;);\n            printSeq(ieq);\n            return true;\n        }\n\n        go = isEqual(ieq, seq);\n    }\n    return false;\n}\n\nvoid dn(int i, int n) {\n    while (2 * i &lt;= n) {\n        int l = 2 * i;\n        int r = l + 1;\n        if (r &lt;= n &amp;&amp; heq[l] &lt; heq[r])\n            l = r;\n        if (heq[i] &lt; heq[l])\n            swap(heq[i], heq[l]);\n        else break;\n        i = l;\n    }\n}\n\nvoid hSort() {\n    bool go = false;\n    for (int i = N \/ 2; i &gt;= 1; i--)\n        dn(i, N);\n    for (int i = N; i &gt; 1; i--) {\n        swap(heq[1], heq[i]);\n        dn(1, i - 1);\n\n        if (go) {\n            printf(&quot;Heap Sort\\n&quot;);\n            printSeq(heq);\n            break;\n        }\n\n        go = isEqual(heq, seq);\n    }\n}\n\nint main() {\n    scanf(&quot;%d&quot;, &amp;N);\n    for (int i = 1; i &lt;= N; i++) {\n        scanf(&quot;%d&quot;, &amp;ieq[i]);\n        heq[i] = ieq[i];\n    }\n    for (int i = 1; i &lt;= N; i++)\n        scanf(&quot;%d&quot;, &amp;seq[i]);\n    if(!iSort()) hSort();\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u8be6\u7ec6\u89e3\u6790\u4e8c\u53c9\u6811\u3001\u4e8c\u53c9\u641c\u7d22\u6811\u3001\u54c8\u592b\u66fc\u6811\u3001\u5e76\u67e5\u96c6\u3001\u5806\u7b49\u6811\u5f62\u7ed3\u6784\u548c\u7b97\u6cd5\u5728PAT\u771f\u9898\u4e2d\u7684\u9009\u578b\u539f\u56e0\u3001\u5e94\u7528\u65b9\u6cd5\u53ca\u5b9e\u73b0\u7ec6 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/139.196.114.170\/?p=1891\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201cPAT &#8211; ADVANCED &#8211; \u6811\u5f62\u7ed3\u6784\u7cbe\u8bb2\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[12],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/posts\/1891"}],"collection":[{"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.196.114.170\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1891"}],"version-history":[{"count":31,"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/posts\/1891\/revisions"}],"predecessor-version":[{"id":2011,"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/posts\/1891\/revisions\/2011"}],"wp:attachment":[{"href":"http:\/\/139.196.114.170\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1891"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.196.114.170\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1891"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.196.114.170\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1891"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}