PAT-1004 Counting Leaves

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

题意

计算每层中有多少个叶子节点,输出

Code

DFS

#include <iostream>
#include <vector>
using namespace std;

// 参考柳婼
vector<int> node[105];
int res[105];
int maxdepth; //记录最深层次

void dfs(int index,int depth){
    
    
    if(node[index].size() == 0){
    
    
        res[depth]++;  // 当前层数叶子数量+1
        maxdepth = max(depth,maxdepth);  // 记录最大深度
        return;
    }
    for(int i=0; i<node[index].size(); i++){
    
    
        dfs(node[index][i], depth+1);
    }
}
int main()
{
    
    
    int n, m;
    cin >> n >> m;
    int father;
    int nums_children;
    for (int i = 0; i < m; i++)
    {
    
    
        cin >> father >> nums_children;
        int child;
        for (int i = 0; i < nums_children; i++)
        {
    
    
            cin >> child;
            node[father].push_back(child);
        }
    }
    dfs(1, 0);
    cout << res[0];
    for(int i=1; i<=maxdepth; i++){
    
    
        cout<<" "<< res[i];  //格式输出
    }
    return 0;
}

BFS

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

vector<int> node[105];
int res[105];
int maxdepth; //记录最深层次

// bfs 原创,bfs就是需要记录每个节点所处的层数,然后思路就是典型的bfs模板
void bfs(){
    
    
    int height[105]; // 记录每个节点所处的层数,不然bfs不能判断每个节点的层数
    // height[]数组,节点编号做下标,值为层数
    fill(height, height+105, -1);
    height[1] = 0; // 根节点为0层
    queue<int>q;
    q.push(1);
    while(!q.empty()){
    
    
        int index = q.front();
        q.pop();
        if(node[index].size()==0){
    
    
            res[height[index]]++;   // 叶子节点+1,height[index]表示层数,index是当前节点的下标
        }
        for(int i=0; i<node[index].size(); i++){
    
     
            // 将每一层的节点压入对列中,同时记录他们的层数
            q.push(node[index][i]);  
            height[node[index][i]] = height[index]+1; // 层数加1
            maxdepth = max(maxdepth, height[node[index][i]]);  // 记录最大层数,输出结果
        }
    }
}
int main()
{
    
    
    int n, m;
    cin >> n >> m;
    int father;
    int nums_children;
    for (int i = 0; i < m; i++)
    {
    
    
        cin >> father >> nums_children;
        int child;
        for (int i = 0; i < nums_children; i++)
        {
    
    
            cin >> child;
            node[father].push_back(child);
        }
    }
    bfs();
    cout << res[0];
    for(int i=1; i<=maxdepth; i++){
    
    
        cout<<" "<< res[i];  //格式输出
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42100456/article/details/108699716