1021. Deepest Root (25分)
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
int maxheight=0;
vector<vector<int>>v;
vector<bool>visit;
set<int>s;
vector<int>temp;
void dfs(int node,int height){
visit[node]=true;
if(height>maxheight){
maxheight=height;
temp.clear();
temp.push_back(node);
}else if(height==maxheight){
temp.push_back(node);
}
for(int i=0;i<v[node].size();i++){
if(visit[v[node][i]]==false)
dfs(v[node][i],height+1);
}
}
int main() {
int n,a,b,cnt=0,s1=0;
cin>>n;
v.resize(n+1),visit.resize(n+1);
for(int i=0;i<n-1;i++){
cin>>a>>b;
v[a].push_back(b);
v[b].push_back(a);
}
for(int i=1;i<=n;i++){
if(visit[i]==false){
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>1) printf("Error: %d components", cnt);
else{
temp.clear();
maxheight=0;
fill(visit.begin(),visit.end(),false);
dfs(s1,1);
for(int i=0;i<temp.size();i++)
s.insert(temp[i]);
for(auto it=s.begin();it!=s.end();it++)
cout<<*it<<endl;
}
return 0;
}