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 5Sample Output 1:
3 4 5Sample Input 2:
5 1 3 1 4 2 5 3 4Sample Output 2:
Error: 2 components
#include <iostream>
#include <vector>
#include <set>
using namespace std;
int longest_dist=-1;
vector<int> uset;
set<int> res;
vector<int> visited;
vector<vector<int> > tree;
int n;
void init_uset()
{
uset.resize(n+1);
for(int i=0;i<=n;i++)
{
uset[i]=i;
}
}
int find_root(int x)
{
int r=x;
while(uset[r] != r)
r=uset[r];
int t;
while(uset[x] != r)
{
t=uset[x];
uset[x]=r;
x=t;
}
return r;
}
void union_set(int r_x,int r_y)
{
uset[r_x]=r_y;
}
void dfs(int s,int d,set<int> & ans)
{
bool leaf=true;
visited[s]=true;
for(int i=0;i<tree[s].size();i++)
{
if(visited[tree[s][i]] == false )
{
leaf = false;
dfs(tree[s][i],d+1,ans);
}
}
if(leaf == true)
{
if(d > longest_dist)
{
longest_dist=d;
ans.clear();
ans.insert(s);
}
else if(d == longest_dist)
{
ans.insert(s);
}
}
return;
}
int main()
{
cin>>n;
tree.resize(n+1);
visited.resize(n+1);
init_uset();
int components=n;
for(int i=0;i<n-1;i++)
{
int n1,n2;
cin>>n1>>n2;
tree[n1].push_back(n2);
tree[n2].push_back(n1);
int x=find_root(n1);
int y=find_root(n2);
if(x != y)
{
components--;
union_set(x,y);
}
}
if(components != 1)
{
set<int> sets;
for(int i=1;i<=n;i++)
{
sets.insert(find_root(i));
}
printf("Error: %d components\n",(int)sets.size());
}
else
{
dfs(1,0,res);
longest_dist=-1;
set<int> ans;
int longest_node=*res.begin();
for(int i=1;i<=n;i++)visited[i]=false;
dfs(longest_node,0,ans);
for(auto itr = res.begin();itr != res.end();++itr)
ans.insert(*itr);
for(auto itr = ans.begin();itr != ans.end();++itr)
cout<<*itr<<endl;
}
return 0;
}
其实不用并查集判断是否是树,用dfs检测连通分量的个数,因为边数为顶点数-1,所以如果连通分量有多个,一定不是树
<pre name="code" class="cpp">#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
vector<vector<int> > adjm;
vector<int> visited;
int deepest=-1;
int components=0;
int n;
set<int> res;
set<int> ans;
void dfs(int v,int d,set<int> & res){
visited[v] = 1;
if(d > deepest){
deepest = d;
res.clear();
res.insert(v);
}else if(d == deepest){
res.insert(v);
}
for(int i=0;i<adjm[v].size();i++){
if(visited[adjm[v][i]] == 0){
dfs(adjm[v][i],d+1,res);
}
}
}
int main(){
cin>>n;
adjm.resize(n);
visited.resize(n);
for(int i=0;i<n-1;i++){
int a,b;
cin>>a>>b;
a--;b--;
adjm[a].push_back(b);
adjm[b].push_back(a);
}
for(int i=0;i<n;i++){
if(visited[i] == 0){
components++;
dfs(i,1,res);
}
}
if(components >1){
cout<<"Error: "<<components<<" components\n";
system("pause");return 0;
}
for(int i=0;i<n;i++){visited[i] = 0;}
deepest = -1;
dfs(*res.begin(),1,ans);
for(set<int>::iterator itr = res.begin();itr != res.end();++itr)ans.insert(*itr);
for(set<int>::iterator itr = ans.begin();itr != ans.end();++itr)cout<<*itr+1<<endl;
system("pause");
return 0;
}