PAT A1021 Deepest Root (25 分)(10.3 判断给定图是否为树,并求最大高度,输出最大高度时的根结点)

8 篇文章 0 订阅
5 篇文章 0 订阅

1021 Deepest Root (25 分)

A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to find the root that results in a highest tree. Such a root is called the deepest root.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (≤10​4​​) which is the number of nodes, and hence the nodes are numbered from 1 to N. Then N−1 lines follow, each describes an edge by given the two adjacent nodes' numbers.

Output Specification:

For each test case, print each of the deepest roots in a line. If such a root is not unique, print them in increasing order of their numbers. In case that the given graph is not a tree, print Error: K components where K is the number of connected components in the graph.

Sample Input 1:

5
1 2
1 3
1 4
2 5

Sample Output 1:

3
4
5

Sample Input 2:

5
1 3
1 4
2 5
3 4

Sample Output 2:

Error: 2 components

由于连通,边数为n-1的图一定是树。所以可以利用并查集判断是否为树。具体做法是判断根结点的个数是否为1。

求最大高度的方法是从任一顶点出发遍历,获取能到达的最深的顶点(记为集合A),然后从A中任一顶点出发遍历获取能达到的最深顶点(记为集合B),则集合A与B的并集即为所求。

参考代码:

#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int maxv=10010;

vector<int>  G[maxv];
bool isroot [maxv];
int father[maxv];
int getfather(int x) //寻找根结点
{
	int a=x;
	while(x!=father[x])
	{
		x=father[x];
	}
	while(a!=father[a])
	{
		int z=a;
		a=father[a];
		father[z]=x;
	}
	return x;
 } 
void Union(int a,int b)
{
	int fa=getfather(a);
	int fb=getfather(b);
	if(fa!=fb)
		father[fa]=fb;
}
void init(int n)
{
	for(int i=1;i<=n;++i)
	{
		father[i]=i;
	}
}
int cal(int n)//计算连通分量的个数
{
	int count=0;
	for(int i=1;i<=n;++i)
	{
		isroot[getfather(i)]=true; //i的根结点是getfather(i) 
	}
	for(int i=1;i<=n;++i)
		count+=isroot[i]; //累加根结点的个数
	return count; 
} 

int maxh=0; //最大深度
vector<int> temp,ans; //temp保存临时DFS的最远结点,ans记录答案

//DFS函数,其中index为当前访问结点,h为树高,pre为父节点
void DFS(int index,int h,int pre)
{
	if(h>maxh) //如果存在更大的树高 
	{
		temp.clear(); //清空向量
		temp.push_back(index); //将当前结点index加入temp
		maxh=h; //更新最大高度 
	}
	else if(h==maxh) 
	{
		temp.push_back(index);// 将当前结点index加入temp
	} 
	for(int i=0;i<G[index].size();++i)
	{
		//由于邻接表存放的是无向图,因此需要跳过回去的边
		if(G[index][i]==pre)
			continue;
		DFS(G[index][i],h+1,index); //递归访问子结点 
	}
}

int main()
{
	int a,b,n;
	scanf("%d",&n);
	init(n);
	for(int i=1;i<n;++i)
	{
		scanf("%d%d",&a,&b);
		G[a].push_back(b);
		G[b].push_back(a);
		Union(a,b);
	}
	int num=cal(n);
	if(num!=1)
	{
		printf("Error: %d components\n",num);
	}
	else
	{
		DFS(1,1,-1);
		ans=temp; //将temp为集合A,赋给ans 
		DFS(ans[0],1,-1); //从任意一个根结点开始遍历
		for(int i=0;i<temp.size();++i)
		{
			ans.push_back(temp[i]); //此时temp为集合B,将其加入ans 
		}
		sort (ans.begin(),ans.end());
		printf("%d\n",ans[0]) ;
		for(int i=1;i<ans.size();++i)
		{
			if(ans[i]!=ans[i-1])
				printf("%d\n",ans[i]);
		}
	}
	return 0;
 } 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值