PAT甲级1021. 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

求树的最长路(直径)问题:
1.用邻接表储存题目所给的图;
2.用并查集判断连通分量的个数,如果大于1,就不是树,输出错误信息和连通分量的个数。
3.如果是树,就求该树的直径:从树上的任意结点v出发,用DFS/BFS找到距v最远的点w(可能不止一个),w 即是直径上的一个端点;再以 w 为起点再次DFS/BFS,可以找到直径上的其他端点。

> 证明:
设树上有一条直径ST。需要证明的是:从树上的任意结点v出发,找到的w一定是直径的端点。
分情况讨论:
1)如果v在ST路径上,如果找到的w不是S或T,即
=>distance(v,w)>distance(v,S) 和 distance(v,w)>distance(v,T)
=> distance(v,w) + distance(v,T) > distance(v,S) +distance(v,T)
即路径wT的距离大于ST,与ST是直径的前提矛盾。
2)如果v不在ST路径上,
a.如果vw和ST有交点X,v首先走到X,从X出发,找到的w一定是S或者T,这一点在1)中已经证明过了;
b.如果vw和ST没有交点,即
distance(v,w) > distance(v,X) + distance(X,S) 和 distance(v,w) > distance(v,X) + distance(X,T),
其中X是vS和vT路径的交点。
=>distance(v,w) + distance(v,X)+distance(X,T) > distance(X,S) + distance(X,T)
情况1
这里写图片描述
情况2-a
这里写图片描述
情况2-b
这里写图片描述

参考资料
http://stackoverflow.com/questions/25649166/linear-algorithm-of-finding-tree-diameter
http://www.xuebuyuan.com/225744.html

#include <cstdio>
using namespace std;
#include <set>
#include <cstring>
#include <vector> 

const int MAX=10000+10;

vector<int> Node[MAX];//邻接表
int vis[MAX]={0};
int maxHeight=0;
int currentHeight=0;
vector<int> tmp;
set<int> ans;

int N;//结点数
int cnt; //连通分量数
int P[MAX];

int Find(int x){
    if(P[x]<0) return x;
    return x=Find(P[x]);
}

void Union(int x,int y){
    int px=Find(x),py=Find(y);
    if(px!=py) {
        if(-P[px]>-P[py]) {
            P[px]+=P[py];
            P[py]=px;
        }
        else{
            P[py]+=P[x];
            P[px]=py;
        }
        cnt--;
    }
}

void DFS(int index){
    vis[index]=1;
    if(currentHeight>maxHeight) {
        maxHeight=currentHeight;
        tmp.clear();
        tmp.push_back(index);
    }
    else if(currentHeight==maxHeight){
        tmp.push_back(index);
    }
    for(int i=0;i<Node[index].size();i++){
        if(vis[Node[index][i]]==0) {
            currentHeight++;
            DFS(Node[index][i]);

            currentHeight--;
        }
    }
}

int main(){
    scanf("%d",&N);
    cnt=N;

    for(int i=1;i<=N;i++) P[i]=-1;

    int u,v;
    for(int i=0;i<N-1;i++){
        scanf("%d %d",&u,&v);
        Node[u].push_back(v);
        Node[v].push_back(u);
        Union(u,v);
    }

    if(cnt>1) {
        printf("Error: %d components\n",cnt);   
    }
    else{
        DFS(1);

        memset(vis,0,sizeof(vis));
        currentHeight=0;
        for(int i=0;i<tmp.size();i++) ans.insert(tmp[i]);
        DFS(tmp[0]);    
        for(int i=0;i<tmp.size();i++) ans.insert(tmp[i]);
        set<int>::iterator it;
        for(it=ans.begin();it!=ans.end();it++) {
            printf("%d\n",*it);
        }
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值