题意:

  连通则输出最深点。第一步找某个点的最深的,然后从这个最深的点查找其他最深点,做并集。

  不连通则输出连通图个数。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<set>
#include<vector>
#include<cstring>
#include<iterator>
#include<algorithm>
using namespace std;
#define N       (10010)

int n;
vector<int> vcMap[N];
bool bVis[N] = {0};
int maxLen = 0;
set<int> setRes;
set<int> setRes2;
set<int> setRes3;
void dfs(int curI,int iLen)
{
    bVis[curI] = 1;
    ++iLen;
    if(iLen > maxLen)
    {
        setRes.clear();
        maxLen = iLen;
        setRes.insert(curI);
    }
    else if(iLen == maxLen)
    {
        setRes.insert(curI);
    }
    for(int i=0;i<vcMap[curI].size();++i)
    {
        if(!bVis[vcMap[curI][i]])
            dfs(vcMap[curI][i],iLen);
    }
}
int main()
{
    scanf("%d",&n);
    for(int i=0;i<n-1;++i)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        vcMap[a].push_back(b);
        vcMap[b].push_back(a);
    }
    int k=0;
    for(int i=1;i<=n;++i)
    {
        if(!bVis[i])
        {
            ++k;
            dfs(i,0);
            set_union(setRes.begin(), setRes.end(), setRes2.begin(),
                      setRes2.end(), inserter(setRes2, setRes2.begin()));
        }
    }
    if(k==1)
    {
        for(int i=1;i<=n;++i)
        {
            if(setRes2.find(i)!=setRes2.end())
            {
                memset(bVis,0,sizeof(bVis));
                dfs(i,0);
                break;
            }
        }
        set_union(setRes.begin(), setRes.end(), setRes2.begin(),
                      setRes2.end(), inserter(setRes2, setRes2.begin()));
        set<int>::iterator it = setRes2.begin();
        for(;it != setRes2.end();it++)
            printf("%d\n",*it);
    }
    else
        printf("Error: %d components\n",k);
    return 0;
}