A1021. Deepest Root (25)

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 <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <string>
#include <stack> 
#include <queue>
#include <vector>
using namespace std;
const int maxn=100010;
vector<int> G[maxn]; 
int father[maxn];
//并查集初始化
void init()
{
     for(int i=1;i<maxn;i++)
     {
         father[i]=i;
     }
} 
int findFather(int x)
{
    int a=x;
    while(x!=father[x])
    {
        x=father[x];
    }
    //路径压缩
    while(a!=father[a])
    {
        int tmp=a;
        a=father[a];
        father[tmp]=x;    
    } 
    return x;
 } 
void Union(int a,int b)//合并 
{
    int faA=findFather(a);
    int faB=findFather(b);
    if(faA!=faB)
    {
        father[faA]=faB;
    }
}
//计算集合数目
int isRoot[maxn];
int cal(int n)
{
    int block=0;
    for(int i=1;i<=n;i++)
    {
        isRoot[findFather(i)]=true;
    }
    for(int i=1;i<=n;i++)
    {
        block+=isRoot[i];
    }
    return block;
} 
//以某节点为根,遍历
int maxH=0;
vector<int> tmp,Ans; 
void DFS(int u,int height,int pre)
{
    if(height>maxH)
    {
    tmp.clear();
    tmp.push_back(u);
    maxH=height;    
    } else if(height==maxH)
    {
        tmp.push_back(u);
    }
    for(int i=0;i<G[u].size();i++)
    {
        if(G[u][i]==pre)continue;
        DFS(G[u][i],height+1,u);
    } 
}
int main(){
   int a,b,n;
   scanf("%d",&n);
   init(); 
   for(int i=1;i<n;i++)//从一开始 
   {
       scanf("%d%d",&a,&b);//无向图,节点用vector存储
    G[a].push_back(b);
    G[b].push_back(a);  
    Union(a,b); 
   }
   int block=cal(n);
   if(block!=1)
   {
       printf("Error: %d components",block);
   }else
   {
       DFS(1,1,-1);
       Ans=tmp;
       DFS(Ans[0],1,-1);
       for(int i=0;i<tmp.size();i++)
       {
          Ans.push_back(tmp[i]);    
    }
    sort(Ans.begin(),Ans.end());
    printf("%d
",Ans[0]);
    for(int i=1;i<Ans.size();i++)
    {
        if(Ans[i]!=Ans[i-1])
        {
            printf("%d
",Ans[i]);
        }
    }
   }
   
    return 0;
}
原文地址:https://www.cnblogs.com/ligen/p/4323685.html