必威体育Betway必威体育官网
当前位置:首页 > IT技术

1021

时间:2019-09-08 10:10:00来源:IT技术作者:seo实验室小编阅读:79次「手机版」
 

1021

#include<iOStream>
#include<cstring>
#include<vector>
using namespace std;

//bool Map[10010][10010];
vector<int> Map[10010];
bool visited[10010];
short ans[10010];
int n;


void DFS(int v,int len);
int Search();
void Find();
int MaxLen;

int main()
{
	
	cin >> n;
	for (int i = 0; i < n - 1; i++)
	{
		int a, b;
		cin >> a >> b;
		Map[a].push_back(b);
		Map[b].push_back(a);
	}
	int k = Search();
	if (k > 1)
	{
		cout << "ERROR: " << k << " components" << endl;
	}
	else
	{
		Find();
	}
	system("pause");
	return 0;
}
void Find()
{
	int maxans = 0;
	for (int i = 1; i <= n; i++)
	{
		MaxLen = 0;
		memset(visited, 0, sizeof(visited));
		DFS(i,0);
		ans[i] = MaxLen;
		if (MaxLen > maxans)
			maxans = MaxLen;
	}
	for (int i = 1; i <= n; i++)
	{
		if (ans[i] == maxans)
			cout << i << endl;
	}
}
int Search()
{
	memset(visited, 0, sizeof(visited));
	int k = 0;
	for (int i = 1; i <= n; i++)
	{
		if (visited[i] == 0)
		{
			k++;
			DFS(i,0);
		}
	}
	return k;
}
void DFS(int v,int len)
{
	visited[v] = 1;
	MaxLen = (len > MaxLen) ? len : MaxLen;
	for (int i = 0; i < Map[v].size(); i++)
	{
		if (!visited[Map[v][i]])
			DFS(Map[v][i], len + 1);
	}
}

相关阅读

分享到:

栏目导航

推荐阅读

热门阅读