PAT甲级真题(并查集)——1004 Counting Leaves (30 分)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_29978597/article/details/85685877

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<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

题目大意:

输出树每一行的非叶子节点数

题目解析:

拿到这道题,想到可以用并查集做,开辟三个数组

  • bool nonleaf[MAXN]//记录节点是否为叶子节点,初值全为false
  • int parent[MAXN]//记录每个节点的父节点序号
  • int count[MAXN]//保存每一行的非叶子节点数目,初值为0

每一行读入的数据,开头的ID为非叶子节点,故nonleaf[ID]设为true,后面的节点全为ID的子节点,parent[序号]=ID;
遍历节点数组,递归获取所有nonleaf值为false所在的行号line,对应的count[line]++,同时记录最大行号maxline;
然后输出即可。

具体代码:

#include<iostream>

using namespace std;

#define MAXN 110

bool nonleaf[MAXN]={false};//noleaf[节点序号]==true表示为非叶子节点 
int parent[MAXN];//记录每个节点的父节点序号 
int count[MAXN];//保存每一行的非叶子节点数目

int getline(int n){//计算节点所在行号 
	if(parent[n]==0)
		return 1;
	return getline(parent[n])+1;
}

int main() {
	int n,m;//n(0,100)为节点总数 
	parent[1]=0;//节点1为根节点 
	cin>>n>>m;
	while(m--){
		int pnode,num;//pnode为父节点,num为子节点个数 
		cin>>pnode>>num;
		nonleaf[pnode]=true;//有子节点 ,所以为非叶子节点 
		while(num--){
			int cnode;
			cin>>cnode;
			parent[cnode]=pnode;//pnode是cnode的父节点 
		}
	}
	fill(count,count+MAXN,0);//初始化
	int maxline=1;//记录最大行数 
	for(int i=1;i<=n;i++){
		if(nonleaf[i]==false){//如果i是叶子节点,则计算它属于哪一行 
			int line=getline(i);
			count[line]++;//对应行的叶子结点数目加一 
			if(line>maxline)//若所在行数大于最大行,更新最大行 
				maxline=line;
		}
	}
	//按要求输出
	for(int i=1;i<=maxline;i++){
		cout<<count[i];
		if(i!=maxline)
			cout<<" ";
	}
	return 0;
}

发现此题可直接用vector数组+dfs做,正向思维,代码如下:

#include<iostream>
#include<vector>

#define MAXN 110

using namespace std;

vector<int> v[MAXN];
int count[MAXN];
int maxdepth=-1;

void dfs(int index,int depth){
	if(v[index].size()==0)//若是叶子节点 
	{
		count[depth]++;
		if(depth>maxdepth)
			maxdepth=depth;
		return;
	}
	for(int i=0;i<v[index].size();i++)
		dfs(v[index][i],depth+1);
}

int main()
{
	int n,m;
	cin>>n>>m;
	while(m--){
		int ID,num;
		cin>>ID>>num;
		while(num--)
		{
			int k;
			cin>>k;
			v[ID].push_back(k);
		}	
	}
	fill(count,count+MAXN,0);
	dfs(1,0);
	for(int i=0;i<=maxdepth;i++)
	{
		cout<<count[i];
		if(i!=maxdepth)
			cout<<" ";
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_29978597/article/details/85685877
今日推荐