# PAT甲级--1021 Deepest Root（25 分）【dfs+连通分量+STL】

1021 Deepest Root（25 分）

A graph which is connected and acyclic can be considered a tree. The hight 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


#include<bits/stdc++.h>
using namespace std;
int maxheight=0;//初始化最高高度为0
bool visit[10010];//判断有没有遍历过
set<int>s;//set容器，没有重复元素，并且有序，默认从小到大
vector<vector<int> >v;//存储边，相当于二维数组
vector<int>temp;//临时存储最高的节点
void dfs(int root,int height)
{
if(height>maxheight)
{
maxheight=height;
temp.clear();
temp.push_back(root);
}
else if(maxheight==height)
{
temp.push_back(root);
}
visit[root]=true;
for(int i=0;i<v[root].size();i++)
{
if(visit[v[root][i]]==false)
dfs(v[root][i],height+1);
}
}
int main(void)
{
int a,b,n;
scanf("%d",&n);
v.resize(n+1);//一定要初始化，要不然输不进去数据
for(int i=0;i<n-1;i++)//是n-1条边，一开始不小心写成了n，导致答案输不出来
{
scanf("%d %d",&a,&b);//a是父亲或者孩子
v[a].push_back(b);
v[b].push_back(a);
}
int cnt=0,s1=0;
for(int i=1;i<=n;i++)
{
if(visit[i]==false)//如果是树，那么i=1的时候，所有节点都遍历过，除非不连通
{
dfs(i,1);
if(i==1)
{
if(temp.size()!=0) s1=temp[0];
for(int j=0;j<temp.size();j++)
s.insert(temp[j]);
}
cnt++;
}
}
if(cnt>=2) printf("Error: %d components",cnt);
else
{
temp.clear();
fill(visit,visit+10010,false);//是树，然后所有节点都赋值为false
dfs(s1,1); //等于1时最高高度的节点，随便选一个来dfs，找出其他最高高度的节点，一头一尾的都是最高
for(int i=0;i<temp.size();i++)
s.insert(temp[i]);
for(auto it = s.begin(); it != s.end(); it++)//set容器是有序的
printf("%d\n", *it);
}
return 0;
}