【PAT 甲级】1004 Counting Leaves (30)(30 分)(dfs/bfs)

题目链接

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

Input

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.

Output

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

题意:求一棵树上每一层的叶子节点的个数(1为根节点)。

思路:容易想到dfs,因为是树结构,从深度出发,子节点的深度=父节点深度+1;另外也可以用bfs,从题目要求出发,求每一层叶子节点个数,层数,可以是bfs进行扩展

代码【bfs】:

#include <iostream>
#include <sstream>
#include <string>
#include <cstdio>
#include <queue>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N = 1e2+10;
const ll INF = 0x3f3f3f3f;
const double eps=1e-4;
const int T=3;

vector<int>g[N];
int maxDepth=0,numLeaf[N];
int level[N];
void bfs() {
    queue<int>que;
    que.push(1);
    level[1]=0;
    while(!que.empty()) {
        int now=que.front();
        que.pop();
        if(!g[now].size())///当前节点是叶子节点
            numLeaf[level[now]]++;
        maxDepth=max(maxDepth,level[now]);
        for(auto v:g[now]) {///按层向外扩展
            que.push(v);
            level[v]=level[now]+1;
        }
    }
}
void init() {
    maxDepth=0;
    memset(numLeaf,0,sizeof(numLeaf));
}

int main() {
    int n,m;
    init();
    cin>>n>>m;
    int root,k,child;
    for(int i=0; i<m; i++) {
        cin>>root>>k;
        for(int j=0; j<k; j++) {
            cin>>child;
            g[root].push_back(child);
        }
    }
    bfs();
    cout<<numLeaf[0];
    for(int i=1; i<=maxDepth; i++) {
        cout<<" "<<numLeaf[i];
    }
    return 0;
}


代码【dfs】:

#include <iostream>
#include <sstream>
#include <string>
#include <cstdio>
#include <queue>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N = 1e2+10;
const ll INF = 0x3f3f3f3f;
const double eps=1e-4;
const int T=3;

vector<int>g[N];
int maxDepth=0,numLeaf[N];
void dfs(int node,int depth) {
    if(!g[node].size()) {
        numLeaf[depth]++;
        maxDepth=max(maxDepth,depth);
        return ;
    }
    for(auto v: g[node]) {
        dfs(v,depth+1);
    }
}
void init() {
    maxDepth=0;
    memset(numLeaf,0,sizeof(numLeaf));
}

int main() {
    int n,m;
    init();
    cin>>n>>m;
    int root,k,child;
    for(int i=0; i<m; i++) {
        cin>>root>>k;
        for(int j=0; j<k; j++) {
            cin>>child;
            g[root].push_back(child);
        }
    }
    dfs(1,0);
    cout<<numLeaf[0];
    for(int i=1; i<=maxDepth; i++) {
        cout<<" "<<numLeaf[i];
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/feng_zhiyu/article/details/80917920