题目地址:http://www.patest.cn/contests/pat-a-practise/1004
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
提交代码
/* 1004. Counting Leaves (30) http://www.patest.cn/contests/pat-a-practise/1004 树,求出最大层数 和每一层的叶子节点数目, 利用队列和map */
#include <cstdio>
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <stack>
#include <string>
#include <queue>
#include <unordered_map>
#include <iterator>
using namespace std;
#define N 100
int n , m;
vector<int> v[N] ;
int cengci[N];
unordered_map<int ,int> um;
void mfun()
{
queue<int> que;
que.push(1);
cengci[1] = 1;
int maxx = 0 ;
while(!que.empty())
{
int nowNum = que.front();
que.pop();
if(cengci[nowNum] > maxx)
maxx = cengci[nowNum];
if((int)v[nowNum].size() == 0)
um[cengci[nowNum]] ++;
int len = v[nowNum].size();
for(int i = 0 ; i < len ; i ++)
{
int nextNum = v[nowNum][i];
cengci[nextNum] = cengci[nowNum] + 1;
que.push(nextNum);
}
}
printf("%d",um[1]);
for(int i = 2 ; i <= maxx ; i++)
{
printf(" %d",um[i]);
}
}
int main()
{
//freopen("in.txt","r",stdin);
int i , j;
scanf("%d%d",&n,&m);
int id , k ,tmpid;
for(i = 0 ; i < m ;i++)
{
scanf("%d%d" , &id , &k);
for(j = 0 ; j < k ;j ++)
{
scanf("%d",&tmpid);
v[id].push_back(tmpid);
}
}
mfun();
printf("");
return 0;
}
#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
#define N 102
vector<int> ve[N] ;
int cengci[N] ;
int cengCount[N] ; // N个结点 最多N层
int m , n ;
void bfs()
{
memset(cengCount , 0 , sizeof(int)*N);
queue<int> que ;
que.push(1) ;
cengci[1] = 1 ;
int maxCeng = 1 ;
while(!que.empty())
{
int topNum = que.front();
que.pop() ;
int len = ve[topNum].size();
if(cengci[topNum] > maxCeng)
{
maxCeng = cengci[topNum] ;
}
if(len == 0)
{
cengCount[cengci[topNum]] ++ ;
}else{
for(int i = 0 ; i < len ; i++)
{
int nextNum = ve[topNum][i] ;
cengci[nextNum] = cengci[topNum] + 1 ;
que.push(nextNum) ;
}
}
}
printf("%d",cengCount[1]);
for(int i = 2 ; i <= maxCeng ; i++)
{
printf(" %d",cengCount[i]);
}
printf("\n");
}
int main()
{
//freopen("in.txt" , "r" , stdin) ;
scanf("%d%d" , &n , &m) ;
int i , j ;
int id , k , idTmp ;
for(i = 0 ; i < m ; i ++)
{
scanf("%d%d",&id , &k);
for(j = 0 ; j < k ; j++)
{
scanf("%d" , &idTmp) ;
ve[id].push_back(idTmp);
}
}
bfs();
return 0 ;
}