PAT甲级1021

PAT甲级1021

题目:
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个节点,接下来给出n-1行边,判断这些节点是否构成一棵树(所有节点都是连通且无环),若是一棵树则输出以哪些节点为根达到的树高最大;若不是一棵树则输出有几个连通块。
由于n个节点n-1条边,所以只要判断他们连通则一定是一棵树,判断连通用DFS即可。这里只能用表,用图的话第三个测试点会超时。至于求树根博主用的方法是设置一个开关flag,0的话判断连通,1的话遍历树高,要用两个visit数组判断是否访问过节点,求树高的visit必须每个根节点都重新初始化(我试过只用一个visit,初始化的话连通块就会出错,不初始化求树高的结果是错的,不知道有没有大佬给出好的解决办法)

#include <iostream>
#include <vector>
#include <algorithm>
#define MAXN 10001
using namespace std;

struct node{
	vector<int> adjv;
}nd[MAXN];
int n,component=0,maxHeight=1,maxtmpHeight=1;	//分别记录总节点数,连通块,所有节点为根当中的最大高度,以当前节点为根的最大高度
bool visit[MAXN]={false},visit1[MAXN]={false};	//一个处理连通块的遍历,一个处理最大高度的遍历
int height[MAXN];
void DFS(int index,int depth,int flag){
	if(flag==0){
		if(visit[index]==true) return ;
		visit[index]=true;
		for(int i=0;i<nd[index].adjv.size();i++){
			if(!visit[nd[index].adjv[i]]){
				DFS(nd[index].adjv[i],depth+1,flag);
			}
		}
	}else{
		if(visit1[index]==true) return ;
		visit1[index]=true;
		if(depth>maxtmpHeight) maxtmpHeight=depth;
		if(maxtmpHeight>maxHeight) maxHeight=maxtmpHeight;
		for(int i=0;i<nd[index].adjv.size();i++){
			if(!visit1[nd[index].adjv[i]]){
				DFS(nd[index].adjv[i],depth+1,flag);
			}
		}
	}
}
void DFSTraversal(){	//遍历连通块的个数以及统计以每个节点为根所能达到的最大树高
	for(int i=1;i<=n;i++){
		fill(visit1+1,visit1+n+1,false);
		if(!visit[i]){
			DFS(i,1,0);	//未访问过j且i到j有边则向下遍历
			component++;
		}
		if(!visit1[i]){
			maxtmpHeight=1;
			DFS(i,1,1);
			height[i]=maxtmpHeight;
		}
	}
}
int main(){
	int v1,v2;
	scanf("%d",&n);
	for(int i=0;i<n-1;i++){
		scanf("%d%d",&v1,&v2);
		nd[v1].adjv.push_back(v2);
		nd[v2].adjv.push_back(v1);
	}
	fill(visit+1,visit+n+1,false);
	DFSTraversal();
	if(component>1) printf("Error: %d components",component);
	else{
		for(int i=1;i<=n;i++)
			if(height[i]==maxHeight) printf("%d\n",i);
	}
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值