PAT--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<N<100, the number of nodes in a tree, and M (<N), the number of non-leaf nodes. Then M lines follow, each in the format:

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

这个题我的思路完全复杂化,且吧自己绕进了不知道怎么写的地方。

我的思路:根据题目建一棵树,树的结构为:id,child,layer(这里由于m行输入的只是没有叶子的结点信息,所以输入的时候,在输入孩子的时候,建立孩子结点和更新高度);然后根据层次遍历,当其child的size==0时,即为叶子结点。

 错误代码:(有两个错误过不去),是因为我默认其高层次的结点先出现,但是是不一定的,如果其父亲结点在本节点后出现,其层次就错误。

#include <iostream>
#include <bits/stdc++.h>

using namespace std;
const int MAX=110;

struct TreeNode
{
    int id;
    vector<int> child;
    int layer=0;
};
int main()
{
    int idMax = 1;
    int n,m;
    cin>>n>>m;
    TreeNode tree[MAX];
    int id,k,childNum;
    tree[1].layer=1;
    int maxLevel=1;
    for(int i=1; i<=m; i++)
    {
        cin>>id>>k;
        tree[i].id = id;
        if(id>idMax)
            idMax = id;
        while(k--)
        {
            cin>>childNum;
            tree[id].child.push_back(childNum);
            tree[childNum].id = childNum;
        if(childNum>idMax)
            idMax = childNum;
            
            tree[childNum].layer = tree[id].layer+1;
            
            if(maxLevel<tree[childNum].layer)
                maxLevel=tree[childNum].layer;
        }
    }

    int leaf[MAX];
    memset(leaf,0,sizeof(leaf));
    for(int i=1; i<=maxLevel;i++){
        for(int j=1;j<=idMax;j++){
            if(tree[j].layer==i && !tree[j].child.size()){
                leaf[i]++;
            }
        }
    }
    for(int i=1;i<=maxLevel;i++){
        if(i==1){
            cout<<leaf[i];
        }else{
            cout<<" "<<leaf[i];
        }
    }
    cout<<endl;
        //cout << "Hello world!" << endl;
        return 0;
}

参考代码:

#include <iostream>
#include <bits/stdc++.h>

using namespace std;
int level[100]; //保存当前结点的所在层数
int book[100];   //记录每层
int maxlevel = -1;
vector<int> v[100];

void bfs()
{
    queue<int> q;
    q.push(1);
    level[1] = 0;
    while(!q.empty())
    {
        int index = q.front();
        q.pop();
        maxlevel = max(level[index],maxlevel);
        if(v[index].size()==0)
        {
            book[level[index]]++;
        }
        for(int i=0; i<v[index].size(); i++)
        {
            q.push(v[index][i]);
            level[v[index][i]] = level[index]+1;
        }
    }
}

int main()
{
    int n,m,k,node,c;
    cin>>n>>m;
    for(int i=0; i<m; i++)
    {
        cin>>node>>k;
        for(int j=0; j<k; j++)
        {
            cin>>c;
            v[node].push_back(c);
        }
    }

    bfs();
    cout<<book[0];
    for(int i=1;i<=maxlevel;i++){
        cout<<" "<<book[i];
    }
    cout<<endl;


    return 0;
}

猜你喜欢

转载自blog.csdn.net/jackson_j/article/details/99709897