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, the number of nodes in a tree, and M (<), 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
题意:
输出树中每一层的叶子节点数
方法一:层序遍历
#include<iostream> #include<algorithm> #include<cstdio> #include<cstdlib> #include<vector> #include<cmath> #include<cstring> #include<queue> #include<string.h> using namespace std; typedef long long ll; int level[100]={0}; int n,m,id,k; vector<vector<int> > tree; vector<int> ans;//存放每层叶子结点数 void findLeaf(int start){ //层序遍历 queue<int> q; q.push(start); int cnt=0; int last=start; while(!q.empty()){ int temp=q.front();//父结点 if(tree[temp].size()==0){ cnt++; } for(int i=0;i<tree[temp].size();i++){ q.push(tree[temp][i]); } if(temp==last){ last=q.back();//把最后一个子节点赋值给last,当temp从第一个子节点到最后一个子节点时,说明该层结束 ans.push_back(cnt); cnt=0; } q.pop(); } } int main(){ int d; tree.resize(100); scanf("%d %d",&n,&m); for(int i=0;i<m;i++){ scanf("%d %d",&id,&k); for(int i=0;i<k;i++){ scanf("%d",&d); tree[id].push_back(d); } } findLeaf(1); for(int i=0;i<ans.size();i++){ if(i<ans.size()-1){ printf("%d ",ans[i]); } else{ printf("%d\n",ans[i]); } } return 0; }
方法二:dfs
#include<iostream> #include<algorithm> #include<cstdio> #include<cstdlib> #include<vector> #include<cmath> #include<cstring> #include<queue> #include<string.h> using namespace std; typedef long long ll; const int maxn=1010; int n,m,id,k; vector<vector<int> > tree; int ans[maxn];//存放每层叶子结点数,注意此处不能用vector int maxlevel=-1;//保存最大层数 void dfs(int root,int depth){ if(tree[root].size()==0){ if(depth>maxlevel){//若层数增加 maxlevel=depth; } ans[depth]++; return ; } for(int i=0;i<tree[root].size();i++){ dfs(tree[root][i],depth+1); } } int main(){ int d; tree.resize(100); scanf("%d %d",&n,&m); for(int i=0;i<m;i++){ scanf("%d %d",&id,&k); for(int i=0;i<k;i++){ scanf("%d",&d); tree[id].push_back(d); } } dfs(1,0); for(int i=0;i<=maxlevel;i++){ if(i<maxlevel){ printf("%d ",ans[i]); } else{ printf("%d\n",ans[i]); } } return 0; }