Deepest Root

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


#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <queue>
using namespace std;
#define MAX 10001
struct Node
{
	int v;
	Node* pre;
	Node* next;
	Node()
	{
		v = -1;
		pre = NULL;
		next = NULL;
	}
	Node(int x)
	{
		v = x;
		pre = NULL;
		next = NULL;
	}
};
struct List
{
	Node* head;
	Node* tail;
	List()
	{
		head = new Node();
		tail = head;
	}
	void push(int x)
	{
		Node* node = new Node(x);
		node->pre = tail;
		tail->next = node;
		tail = tail->next;
	}
	~List()
	{
		Node* t = head;
		while(t != NULL)
		{
			Node* t2 = t->next;
			delete t;
			t = t2;
		}
		head = NULL;
		tail = NULL;
	}
};
struct Edge
{
	int a,b;
}edge[MAX];
int BFS_Search(List* g,int n,int root)
{
	queue<int> q;
	q.push(root);
	bool visited[MAX];
	int depth[MAX];
	memset(visited,false,sizeof(visited));
	depth[root] = 0;
	visited[root] = true;
	int count(1);
	while(!q.empty())
	{
		int v = q.front();
		q.pop();
		for(Node* index = g[v].head->next;index != NULL;index = index->next)
		{
			if(visited[index->v])
				continue;
			++ count;
			depth[index->v] = depth[v] + 1;
			q.push(index->v);
			visited[index->v] = true;
		}
		if(n == count)
			break;
	}
	int max(-1);
	for(int i = 1;i <= n;++ i)
	{
		if(depth[i] > max)
			max = depth[i];
	}
	return max;
}
int findcate(int k,int* belong)
{
	int t = k;
	while(k != belong[k])
		k = belong[k];
	belong[t] = k;
	return k;
}
void union_vetex(int a,int b,int* belong)
{
	int fa = findcate(a,belong);
	int fb = findcate(b,belong);
	if(fa < fb)
		belong[b] = fa;
	else
		belong[a] = fb;
}
int comp(const void* a,const void* b)
{
	return *(int*)a - *(int*)b;
}
int main()
{
	int n;
	scanf("%d",&n);
	List g[MAX];
	int degree[MAX];
	int belong[MAX];
	memset(degree,0,sizeof(degree));
	for(int i = 0;i < n - 1;++ i)
	{
		int a,b;
		scanf("%d %d",&a,&b);
		g[a].push(b);
		g[b].push(a);
		++ degree[a];
		++ degree[b];
		edge[i].a = a;
		edge[i].b = b;
	}
	for(int i = 1;i <= n;++ i)
		belong[i] = i;
	for(int i = 0;i < n - 1;++ i)
		union_vetex(edge[i].a,edge[i].b,belong);
	qsort(belong,n,sizeof(int),comp);
	int componts(1);
	int tmp_cate = belong[1];
	for(int i = 2;i <= n;++ i)
	{
		if(belong[i] != tmp_cate)
		{
			tmp_cate = belong[i];
			++ componts;
		}
	}
	if(componts > 1)
	{
		printf("Error: %d components",componts);
	}
	else
	{
		int depth;
		int max = -1;
		int final_depth[MAX];
		memset(final_depth,0,sizeof(final_depth));
		for(int root = 1;root <= n;++ root)
		{
			if(degree[root] > 1)
				continue;
			else
			{
				depth = BFS_Search(g,n,root);
				final_depth[root] = depth;
				if(depth > max)
					max = depth;
			}
		}
		for(int i = 1;i <= n;++ i)
			if(max == final_depth[i])
				printf("%d\n",i);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值