1004 Counting Leaves (30point(s))
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
题目大意:给一个树,求每一层的叶子结点个数。
解题思路:这里直接对树进行了层序遍历,用参数p存储每一层的末尾元素,当目前访问的now==p时,进行结果输出,并对num进行归零操作。当然这道题,也可以采用dfs的方式,思路为:从根结点开始遍历,直到遇见叶子结点,然后对当前层数index对应的叶子结点值进行++操作(book[index]++),并记录图中最深深度maxdepth,最后对数组book输出即可。
Java代码:
import java.util.LinkedList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();//the number of nodes
int m = sc.nextInt();//the number of non-leaf nodes
int num = 0;
int[][] G = new int[n+1][n+1];
for(int i=0; i<m; i++)
{
int ID = sc.nextInt();
int k = sc.nextInt();
for(int j=0; j<k; j++)
{
int id = sc.nextInt();
G[ID][id] = 1;
}
}
LinkedList<Integer> q = new LinkedList<Integer>();
int p = 1;
q.offer(1);
while(!q.isEmpty())
{
int now = q.peek();//peek读队首元素,当为空时,返回null
q.poll();
boolean flag = false;
for(int i=1; i<=n; i++)
{
if(G[now][i]!=0)
{
q.offer(i);
flag = true;
}
}
if(!flag) {
num++;
}
if(now==p) {
if(!q.isEmpty()) p=q.getLast();
if(now==1)
{
System.out.print(num);
}else {
System.out.print(" "+num);
}
num=0;
}
}
}
}
注意:使用Java容器时,取末尾元素一定要判断队列是否为空,不然会报错 java.util.NoSuchElementException异常。
C++代码(bfs):
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 105;
int G[maxn][maxn],m,n;
int nonLeafNode,itsChildNum;
int childId;
int number=0;
void levelTral(){
queue<int> q;
int p=1;
q.push(1);
while(!q.empty()){
int now = q.front();
q.pop();
bool flag = false;
for(int i=1;i<=n;i++){
if(G[now][i]!=0){
q.push(i);
flag = true;
}
}
if(!flag){
number++;
}
if(p==now){
p=q.back();
if(now==1){
printf("%d",number);
}else{
printf(" %d",number);
}
number=0;
}
}
}
int main(){
fill(G[0],G[0]+maxn*maxn,0);
scanf("%d%d",&n,&m);
for(int i=0; i<m; i++){
scanf("%d%d",&nonLeafNode,&itsChildNum);
for(int j=0; j<itsChildNum; j++){
scanf("%d",&childId);
G[nonLeafNode][childId]=1;
}
}
levelTral();
return 0;
}
C++代码(dfs):
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> v[100];
int book[100], maxdepth = -1;
void dfs(int index, int depth)
{
if(v[index].size() == 0)
{
book[depth]++;
maxdepth = max(maxdepth, depth);
return ;
}
for(int i = 0; i < v[index].size(); i++)
dfs(v[index][i], depth + 1);
}
int main()
{
int n, m, k, node, c;
scanf("%d %d", &n, &m);
for(int i = 0; i < m; i++)
{
scanf("%d %d",&node, &k);
for(int j = 0; j < k; j++)
{
scanf("%d", &c);
v[node].push_back(c);
}
}
dfs(1, 0);
printf("%d", book[0]);
for(int i = 1; i <= maxdepth; i++)
printf(" %d", book[i]);
return 0;
}