{"id":395,"date":"2019-12-22T16:39:29","date_gmt":"2019-12-22T08:39:29","guid":{"rendered":"http:\/\/www.guanhaobo.cn\/?p=395"},"modified":"2019-12-22T16:39:29","modified_gmt":"2019-12-22T08:39:29","slug":"pat-advanced-level-practice-1004-counting-leaves","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=395","title":{"rendered":"PAT (Advanced Level) Practice 1004 Counting Leaves"},"content":{"rendered":"<p>A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.<\/p>\n<h3>Input Specification:<\/h3>\n<p>Each input file contains one test case. Each case starts with a line containing 0&lt;N&lt;100, the number of nodes in a tree, and M (&lt;N), the number of non-leaf nodes. Then M lines follow, each in the format:<\/p>\n<p>ID K ID[1] ID[2] &#8230; ID[K]<br \/>\nwhere ID is a two-digit number representing a given non-leaf node, K is the number of its children, followed by a sequence of two-digit ID&#8217;s of its children. For the sake of simplicity, let us fix the root ID to be 01.<\/p>\n<h3>Output Specification:<\/h3>\n<p>For each test case, you are supposed to count those family members who have no child for every seniority level starting from the root. The numbers must be printed in a line, separated by a space, and there must be no extra space at the end of each line.<\/p>\n<p>The sample case represents a tree with only 2 nodes, where 01 is the root and 02 is its only child. Hence on the root 01 level, there is 0 leaf node; and on the next level, there is 1 leaf node. Then we should output 0 1 in a line.<\/p>\n<h3>Sample Input:<\/h3>\n<pre><code class=\"language-cpp line-numbers\">2 1\n01 1 02\n<\/code><\/pre>\n<h3>Sample Output:<\/h3>\n<pre><code class=\"language-cpp line-numbers\">0 1\n<\/code><\/pre>\n<h3>\u9898\u76ee\u5927\u610f<\/h3>\n<p>\u6709\u4e00\u4e2a\u6811\uff0c\u6839\u7ed3\u70b9\u7f16\u53f7\u4e3a01\uff0c\u4f60\u9700\u8981\u627e\u51fa\u6bcf\u4e00\u5c42\u7684\u53f6\u5b50\u7ed3\u70b9\u7684\u4e2a\u6570\u3002N\u662f\u7ed3\u70b9\u7684\u4e2a\u6570\uff0cM\u662f\u975e\u53f6\u5b50\u7ed3\u70b9\u7684\u4e2a\u6570\u3002\u7b2c\u4e00\u884c\u6570\u636e\u662fN\u548cM\uff0c\u7136\u540e\u662fM\u884c\u6570\u636e\uff0cID K ID[1] ID[2] \u2026 ID[K]\u8868\u793a\u7f16\u53f7\u4e3aID\u7684\u7ed3\u70b9\u6709K\u4e2a\u5b50\u8282\u70b9\uff0c\u5206\u522b\u662fID[1] ID[2] \u2026 ID[K]\u3002\u7ed3\u70b9\u7684\u7f16\u53f7\u662f\u4e24\u4f4d\u6570\u3002<\/p>\n<h3>\u9898\u76ee\u5206\u6790<\/h3>\n<p>\u8bbeans[x]\u8868\u793a\u7b2cx\u5c42\u7684\u53f6\u5b50\u7ed3\u70b9\u7684\u4e2a\u6570\uff0cnum\u8868\u793a\u6811\u7684\u5c42\u6570\u3002<br \/>\n\u904d\u5386\u6bcf\u4e2a\u7ed3\u70b9\uff0c\u5224\u65ad\u662f\u5426\u662f\u53f6\u5b50\u7ed3\u70b9\uff0c\u7136\u540e\u6c42\u51fa\u5b83\u7684\u5c42\u6570\uff0c\u7ef4\u62a4ans\u6570\u7ec4\u5373\u53ef\u3002\u6c42\u5c42\u6570\u53ef\u4ee5\u901a\u8fc7\u7236\u7ed3\u70b9\u4e00\u5c42\u4e00\u5c42\u7684\u5411\u4e0a\u627e\u3002<br \/>\n\u7ed3\u70b9\u7684\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-cpp line-numbers\">struct node\n{\n    vector&lt;int&gt; children;\n    int father;\n} p[105];\n<\/code><\/pre>\n<p>\u4f7f\u7528\u9012\u5f52\u6c42\u5c42\u6570<\/p>\n<pre><code class=\"language-cpp line-numbers\">int f(int x)                 \/\/\u8fd4\u56dep[x]\u6240\u5904\u7684\u5c42\u6570\uff0c\u8bbe\u6839\u7ed3\u70b9\u5c42\u6570\u4e3a0\n{\n    if (x == 1)\n        return 0;\n    return f(p[x].father) + 1;\n}\n<\/code><\/pre>\n<p>\u7531\u4e8e\u8f93\u51fa\u65f6\u9700\u8981\u8f93\u51fa\u6bcf\u4e00\u5c42\u7684\u53f6\u5b50\u7ed3\u70b9\u4e2a\u6570\uff0c\u4f46\u6211\u4eec\u5e76\u4e0d\u77e5\u9053\u4e00\u5171\u6709\u591a\u5c11\u5c42\uff0c\u6240\u4ee5\u5728\u904d\u5386\u7ed3\u70b9\u7684\u65f6\u5019\u987a\u4fbf\u7ef4\u62a4\u4e00\u4e0b\u6811\u7684\u5c42\u6570num\u3002<br \/>\n\u53e6\u5916\uff0cN=1,M=0\u65f6\u9700\u8981\u8fdb\u884c\u7279\u5224\u3002<\/p>\n<h3>AC\u4ee3\u7801<\/h3>\n<pre><code class=\"language-cpp line-numbers\">#include &lt;bits\/stdc++.h&gt;\nusing namespace std;\nstruct node\n{\n    vector&lt;int&gt; children;\n    int father;\n} p[105];\nint ans[105] = {0}, num = 0; \/\/\u6bcf\u4e00\u5c42\u7684\u53f6\u5b50\u7ed3\u70b9\u7684\u4e2a\u6570\uff0cnum\u4e3a\u6700\u5927\u5c42\u6570\u7684\u7f16\u53f7\nint f(int x)                 \/\/\u8fd4\u56dep[x]\u6240\u5904\u7684\u5c42\u6570\uff0c\u8bbe\u6839\u7ed3\u70b9\u5c42\u6570\u4e3a0\n{\n    if (x == 1)\n        return 0;\n    return f(p[x].father) + 1;\n}\nint main()\n{\n    int n, m, i, k, id, id2;\n    cin &gt;&gt; n &gt;&gt; m;\n    if (n == 1 &amp;&amp; m == 0) \/\/\u7279\u5224\n    {\n        cout &lt;&lt; 1 &lt;&lt; endl;\n        return 0;\n    }\n    while (m--)\n    {\n        cin &gt;&gt; id &gt;&gt; k;\n        while (k--)\n        {\n            cin &gt;&gt; id2;\n            p[id].children.push_back(id2);\n            p[id2].father = id;\n        }\n    }\n    for (i = 0; i &lt; 105; i++)\n    {\n        if (!p[i].children.size() &amp;&amp; p[i].father)\n        {\n            int l = f(i);\n            num = max(num, l);\n            ans[l]++;\n        }\n    }\n    for (i = 0; i &lt;= num; i++)\n    {\n        cout &lt;&lt; ans[i];\n        if (i != num)\n            cout &lt;&lt; \" \";\n    }\n    return 0;\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A family hierarchy is usually presented by a pedigree t [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[23,43],"class_list":["post-395","post","type-post","status-publish","format-standard","hentry","category-algo","tag-pat","tag-43"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/395","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=395"}],"version-history":[{"count":0,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/395\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}