PAT甲级1021. Deepest Root (25)

来源:互联网 发布:sap hana数据库 编辑:程序博客网 时间:2024/05/16 17:31

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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 安运继续教育的登录密码忘了怎么办 金蝶k3账套管理打不开了怎么办 仁和会计课堂app不能用怎么办 光大银行已经下卡了终审被拒怎么办 过了上诉期和申诉期该怎么办 北外大四学生要实习半年课程怎么办 电脑发给手机的文件过期了怎么办 农民给土地卖了30年后怎么办 家长发家长群作业太多老师怎么办 在考试中心补不了四级成绩怎么办 微信登录密码不记得了怎么办 欠农民工工资不给怎么办老板说没钱 国外期刊催问稿件不理睬怎么办 老公离不开老婆也离不开小三怎么办 出轨被老婆发现还和小三联系怎么办 老公出轨后回家老婆不想原谅怎么办 小三和原配打架都住院了怎么办 毕业太多年查不到学历认证怎么办 没有做税种核定开了票怎么办 在学信网上查不到学历信息怎么办 学信网手机号换了密码忘了怎么办 学信网手机号换了密码也忘了怎么办 学信网上学习形式是星号怎么办 新手机号已被注册微店买家怎么办 微信号被冻结了里面的钱怎么办 不懂公司产品却要接待老外怎么办 上菜时发现桌面摆不下新菜怎么办 超市买到过期产品商家不赔尝怎么办 皇帝成长计划2俘虏的士兵怎么办 晚上楼上有挪桌子的声音怎么办 金灶茶具出故障码e7怎么办 起亚k2灯泡掉进大灯总成怎么办 衣服上拆过线的针孔怎么办 驾考科目二坡道定点熄火怎么办 穿着超短裤感觉要漏屁股怎么办 台式电脑开机后无法进入系统怎么办 产后两年了肚子肥胖松弛怎么办 在作文中写上自己的名字怎么办 外出玩耍时迷路了你会怎么办 外出玩耍时孩子总喜欢乱跑怎么办? 如果真的物价高的受不了怎么办