题目描述:
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
题目大意:
第一行告诉你树的总的结点的个数和非叶结点的个数。其余每一行就是非叶结点是谁,它有几个孩子,这几个孩子没分别是谁。最后让输出每一层上的叶结点的个数。(树的深度优先遍历(DFS)或者广度优先遍历(BFS))
代码如下:
这个是dfs:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
vector<int> v[100];
int flag[100]={0},maxdepth=-1;
void dfs(int n,int depth)
{
if(v[n].size()==0){
flag[depth]++;
maxdepth=max(maxdepth,depth);
return;
}
for(int i=0;i<v[n].size();i++)
dfs(v[n][i],depth+1);
}
int main()
{
int n,m,node,k,a;
cin>>n>>m;
for(int i=0;i<m;i++){
cin>>node>>k;
for(int j=0;j<k;j++){
cin>>a;
v[node].push_back(a);
}
}
dfs(1,0);
for(int i=0;i<=maxdepth;i++){
if(i!=0) cout<<" ";
cout<<flag[i];
}
return 0;
}
来总结一下:
1)结合了一下别人的思路,感觉这个思路真的挺好的,vector感觉有点像图中的邻接表,dfs就是递归,最主要的是要记录下来树的层数。flag[]数组就是用于存储每一层上的叶节点的个数。
BFS
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
vector<int> v[100];
int flag[100]={0},level[300]={0};
void bfs(int m)
{
level[1]=1;
for(int j=1,k=1;k<=m;j++){
if(v[j].size()!=0){
for(int i=0;i<v[j].size();i++){
level[v[j][i]]=level[j]+1;
if(v[v[j][i]].size()==0) flag[level[v[j][i]]]++;
}
k++;
}
}
}
int main()
{
int n,m,node,k,a;
cin>>n>>m;
for(int i=0;i<m;i++){
cin>>node>>k;
for(int j=0;j<k;j++){
cin>>a;
v[node].push_back(a);
}
}
bfs(m);
sort(level,level+200);
for(int i=1;i<=level[199];i++){
if(i!=1) cout<<" ";
cout<<flag[i];
}
return 0;
}
很难受,测试点二一直过不去!
看了别人的思路发现,广度优先都是用队列来写的。(我就也换了换)
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
vector<int> v[100];
int flag[100]={0},level[100]={0},maxlevel=-1;
void bfs()
{
level[1]=1;
queue<int>q;
q.push(1);
while(!q.empty()){
int index=q.front();
if(v[index].size()==0){
flag[level[index]]++;
maxlevel=max(level[index],maxlevel);
}
for(int i=0;i<v[index].size();i++){
q.push(v[index][i]);
level[v[index][i]]=level[index]+1;
}
q.pop();
}
}
int main()
{
int n,m,node,k,a;
cin>>n>>m;
for(int i=0;i<m;i++){
cin>>node>>k;
for(int j=0;j<k;j++){
cin>>a;
v[node].push_back(a);
}
}
bfs();
for(int i=1;i<=maxlevel;i++){
if(i!=1) cout<<" ";
cout<<flag[i];
}
return 0;
}
来总结一下:(说一下在写广度优先的时候遇到的几个问题)
1)q.pop()就是移除队列的队首元素,不需要给任何的参数。q.front()就是访问队首元素。q.back()就是访问队尾元素。