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

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;
}
全部评论

相关推荐

05-12 18:33
门头沟学院 Java
牛客93345081...:疯狂捞我,然后hr面过了横向给我挂,啥意思啊请问😭😭
点赞 评论 收藏
分享
04-12 21:52
南开大学 Java
鼠鼠有点摆,去年边学着没敢投简历,没实习。从1月到现在总共面了五次,四次字节的日常(HR打电话约面试才敢去的),然后一次腾讯的暑期,都是一面挂,其他则是没给面。暑期的岗,4.2才开始海投,前面想着等字节第四次一面后再投,结果挂,而且感觉投晚了。字节投了11个,9个简历挂,剩下2个没动静。阿里全都简历挂,剩下的在&quot;投递简历&quot;。腾讯给了一次面。然后其他大中厂、手机厂什么的都是做完测评or笔试就没下文,打开几个看也是终止流程,感觉剩下的也应该是简历挂了。感觉是简历的原因?项目部分,几次面试,感觉面试官主要就拷问过秒杀这一个点。自己说的时候会尝试把sse那条说成亮点,但除了腾讯面试官问过一下这整个点在业务方面对用户有什么用之类的问题外,其他最多只是问一下sse八股...感觉也许不是很让面试官感兴趣。这个短链接也是无人问津,就被问过一回雪花算法的设计。也许我该拿点评改改,然后再在网上找一个什么项目,凑两个,而不是用自己现在这两个项目?或者是点评改改放前面,然后原本第一个项目,把秒杀抽掉,剩下的想办法从网上火的RAG项目里移植点亮点,或者直接就用网上的RAG项目?感觉我主要还是偏向后端开发,但是感觉如果除开点评,再拿一个项目,想不到有什么自己能掌控且跟点评不重的。然后鼠鼠之前主要的问题是担心面试让打开项目演示,然后就一直花时间在用AI整第一个项目,第二个项目都没时间整,第四次面试之前还因为太害怕被认为不熟悉项目,跟AI一起把简历的说辞做了大幅度弱化,然后暑期都是拿弱化后的简历投的,感觉是不是看上去太没有吸引力就直接给简历挂了。(图1是弱化后的,图2是弱化前的,但之前3月初投了几家好像也是简历挂。)而且因为3月花了很多时间整在跟AI整代码,导致八股和算法都没怎么看,算法之前有跟灵神题单刷一些,还算入门,但是八股只看了一些基本的,可能面试的时候只答得上来60-70%,而且表述有些混乱,都是想到哪说到哪;前面几回面试基本上都有大板块的基础八股没答出来,比如RedisZ&nbsp;Set数据结构,MQ延时消息、可靠性保证,JVM内存分配的过程、GC&nbsp;roots,JUC锁,设计模式。现在有点不知道该怎么办。求大佬们给点简历修改建议或者面试准备建议,不胜感激!
何时能不做牛马:简历每个点之间的间距可以缩一下。几乎没遇到过要演示项目的情况,即使万一遇上了你也可以说部署在其他电脑上本地没代码。nku不应该简历挂吧?抓紧背背八股练练表达,不要放弃,五六月份找到也不晚(不然还得提前入职
应届生简历当中,HR最关...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务