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
题意:
给出n个结点,里面有m个非叶子结点,然后下面m行就是这些非叶子结点的编号,子结点数目和子节点编号,要求该树每层的叶子结点个数。(根节点编号为1,根节点所在层次编号为1)
思路:
没有权重,是编号和结点之间的关系,考虑用vector表示结点和子结点的关系即可
DFS:就是遍历所有结点,用一个hashtable数组,i代表层数,来记录i层下的叶子结点个数。
BFS:多一个level数组用来存储结点所在的层数,不然在同一个队列内分辨不出那个结点是那个层次的,然后遍历所有结点,是叶子结点就按照level数组找到其所在的层次,作为hashtable的下标来对叶子结点计数。
DFS:
#include<iostream>
#include<vector>
using namespace std;
const int maxn=110;
vector<int > Node[maxn];
int hashtable[maxn]={0};
int n,m,k,father,child,maxdepth=1;
void DFS(int root,int depth){
if(Node[root].size( )==0){
if(depth>maxdepth){//树的深度和叶子结点有关,找最深的那个叶子结点所在层数就是树的深度了;
maxdepth=depth;
}
hashtable[depth]++;
return ;
}
for(int i=0;i<Node[root].size( );i++){//因为子结点是Push_back进去的,尽管根节点是从1开始,但是遍历子结点就是另外一回事了;
DFS(Node[root][i],depth+1);
}
}
int main ( ){
scanf("%d %d",&n,&m);//结点个数,非叶子结点个数;
for(int i=0;i<m;i++){
scanf("%d %d",&father,&k);//结点即其子结点的个数;
for(int j=0;j<k;j++){
scanf("%d",&child);//子结点;
Node[father].push_back(child);
}
}
DFS(1,1);//找出每层结点个数和有多少层,根节点编号为1,层次从1开始;
for(int i=1;i<=maxdepth;i++){
printf("%d",hashtable[i]);
if(i!=maxdepth)printf(" ");
}
return 0;
}
BFS:
#include<iostream>
#include<queue>
using namespace std;
const int maxn=110;
vector<int > Node[maxn];
int hashtable[maxn]={0};//记录层次的叶子结点的个数;
int level[maxn]={0};//记录结点所在的层次;
int n,m,k,father,child,maxdepth=1;
void BFS(int root){
queue<int >q;
q.push(root);
level[root]=1;//根结点的层次是1;
while(!q.empty()){
int top=q.front();
q.pop( );
if(Node[top].size()==0)hashtable[level[top]]++;//top结点是叶子结点,top所在的层次的叶子结点数目++;
if(level[top]>maxdepth)maxdepth=level[top]; //结点所在层次大于目前最大层次,找到树的深度,好为下面遍历每层的叶子结点数做准备;
for(int i=0;i<Node[top].size();i++){//遍历top结点的子节点
if(Node[top][i]!=0){
q.push(Node[top][i]);
level[Node[top][i]]=level[top]+1;//将top结点的子结点的层数记录下来;
}
}
}
}
int main ( ){
scanf("%d %d",&n,&m);//结点个数,非叶子结点个数;
for(int i=0;i<m;i++){
scanf("%d %d",&father,&k);//结点即其子结点的个数;
for(int j=0;j<k;j++){
scanf("%d",&child);//子结点;
Node[father].push_back(child);
}
}
BFS(1);//找出每层结点个数和有多少层,根节点编号为1,层次从1开始;
for(int i=1;i<=maxdepth;i++){
printf("%d",hashtable[i]);
if(i!=maxdepth)printf(" ");
}
return 0;
}