1021. Deepest Root (25)


时间限制
1500 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 calledthe deepest root.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (<=10000) 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

参照算法笔记P343.

1.连通分量问题用并查集。

2.选择合适的根结点,使得树的高度最大。具体做法:先任意选择一个结点,从该结点开始遍历整颗树,获得能达到的最深的顶点(记为结点集合A)。然后从集合A中任意一个结点出发遍历整棵树,获得能达到的最深的顶点(记为集合B)。这样从集合A与集合B的并集即为所求的使树高度最大的根结点。


对vector排序:sort(ans.begin(),ans.end());


#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std; 
vector<int>adj[10001];//邻接表 
vector<int>temp,ans;
int tree[10001];
int visit[10001];
int maxH;
int findroot(int x){
	if(tree[x]==-1)return x;
	return tree[x]=findroot(tree[x]);
}
void init(int n){
	int i;
	for(i=1;i<=n;i++){
		tree[i]=-1;
		visit[i]=0;
	}
}
void dfs(int s,int high){
	if(high>maxH){
		temp.clear();
		temp.push_back(s);
		maxH=high;
	}
	else if(high==maxH){
		temp.push_back(s);
	}
	visit[s]=1;
	int i;
	for(i=0;i<adj[s].size();i++){
		if(visit[adj[s][i]]==0){
			dfs(adj[s][i],high+1);
		}
	}
}
int main(){
	int n,i,c1,c2;
	scanf("%d",&n);
	init(n);
	for(i=1;i<n;i++){
		scanf("%d %d",&c1,&c2);
		adj[c1].push_back(c2);
		adj[c2].push_back(c1);
		c1=findroot(c1);
		c2=findroot(c2);
		if(c1!=c2){
			tree[c1]=c2;
		}
	}
	int cou=0;
	for(i=1;i<=n;i++){
		if(tree[i]==-1){
			cou++;
		}
	}
	if(cou!=1){
		printf("Error: %d components",cou);
	}
	else{
		dfs(1,1);
		ans=temp;
		init(n);
		dfs(temp[0],1);
		for(i=0;i<temp.size();i++){
			ans.push_back(temp[i]);
		}
		sort(ans.begin(),ans.end());
		printf("%d\n",ans[0]);
		for(i=1;i<ans.size();i++){
			if(ans[i]!=ans[i-1]){
				printf("%d\n",ans[i]);
			}
		}
	}
} 


#include<stdio.h>
#include<vector>
#include<set>
using namespace std;
int tree[10010];
int visit[10010];
vector<int>adj[10010];
set<int>temp,ans;
int deepest=0;
void init(int n){
	int i;
	for(i=1;i<=n;i++){
		tree[i]=-1;
		visit[i]=0;
	}
}
int findroot(int x){
	if(tree[x]==-1)return x;
	return tree[x]=findroot(tree[x]);
}
void dfs(int s,int depth){
	visit[s]=1;
	if(depth>deepest){
		deepest=depth;
		temp.clear();
		temp.insert(s);
	}
	else if(depth==deepest){
		temp.insert(s);
	}
	int i;
	for(i=0;i<adj[s].size();i++){
		int next=adj[s][i];
		if(visit[next]==0){
			dfs(next,depth+1);
		}
	}
}
int main(){
	int n,i,c1,c2,fa1,fa2;
	scanf("%d",&n);
	init(n);
	for(i=0;i<n-1;i++){
		scanf("%d %d",&c1,&c2);
		adj[c1].push_back(c2);
		adj[c2].push_back(c1);
		fa1=findroot(c1);
		fa2=findroot(c2);
		if(fa1!=fa2){
			tree[fa1]=fa2;
		}
	}
	int cou=0;
	for(i=1;i<=n;i++){
		if(tree[i]==-1){
			cou++;
		}
	}
	if(cou>1){
		printf("Error: %d components",cou);
	}
	else{
		dfs(1,1);
		ans=temp; 
		init(n);
		set<int>::iterator it=temp.begin();
		dfs(*it,1);
		it=temp.begin();
		for(;it!=temp.end();it++){
			ans.insert(*it);
		}
		it=ans.begin();
		for(;it!=ans.end();it++){
			printf("%d\n",*it);
		}
	}
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值