1004 Counting Leaves (30 分)的解析与思路

A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 0

ID K ID[1] ID[2] ... ID[K]

where 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's of its children. For the sake of simplicity, let us fix the root ID to be 01.

The input ends with N being 0. That case must NOT be processed.

Output Specification:

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.

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.

Sample Input:

2 1
01 1 02

Sample Output:

0 1

  文章大致含义:

    第一行有两个数据,一个是总结点数n,一个非叶结点数m,接下来就是对m个非页结点具体信息,01是根节点,然后是儿子结点的个数,接下来才是儿子结点的数字;问题求每一层的没儿子的结点数量。

   设置结点结构体,由题意得需要知道层数,还有父亲结点以及是否有儿子结点,这三个属性封装成结构体。

源代码:

  1. #include
  2. using namespace std;
  3. struct Node{
  4.     int father;
  5.     int level;
  6.     bool havechild;
  7. };
  8. Node node[101];
  9. int sumnochild[101];
  10. int main()
  11. {
  12.     int n,m;
  13.     int nownode_childnum,nownode,childnode;
  14.     int maxlevel=1;
  15.     cin>>n>>m;
  16. //结构体结点的初始化
  17.     for(int i=0;i
  18.     {
  19.         node[i].father=0;
  20.         node[i].level=0;
  21.         node[i].havechild=0;
  22.     }
  23.     node[1].level=1;
  24. //因为第一个节点肯定是根节点,所以直接动第二个几点开始算
  25.     for(int i=1;i<=m;i++)
  26.     {
  27.         cin>>nownode;//目前所在结点
  28.         cin>>nownode_childnum;//拥有的儿子数量
  29.         if(nownode_childnum!=0)
  30.         {
  31.             node[nownode].havechild=1;//如果儿子数量不为0,则现在结点的bool变成true即=1
  32.         }
  33.         while(nownode_childnum--)
  34.         {
  35.             cin>>childnode;
  36.             node[childnode].father=nownode;//每一次输入记录父子信息
  37.         }
  38.     }
  39. //这里其实有点多余了但是为了简单,我就全局遍历了,就是使儿子结点的层数是父亲结点的层数+1
  40.     for(int i=1;i<=n;i++)
  41.     {
  42.         for(int j=1;j<=n;j++)
  43.         {
  44.             if(node[j].father==i)
  45.             {
  46.                 node[j].level=node[i].level+1;
  47.             }
  48.         }
  49.     }
  50.     for(int i=1;i<=n;i++)
  51.     {
  52.         if(node[i].havechild==0 && node[i].level!=0)
  53.             sumnochild[node[i].level]++;
  54.         if(node[i].level>maxlevel)
  55.             maxlevel=node[i].level;
  56.     }
  57.     for (int i=1;i
  58.     {
  59.         cout<
  60.     }
  61.     cout<
  62.     return 0;
  63. }

你可能感兴趣的:(1004 Counting Leaves (30 分)的解析与思路)