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;}
- 【PAT甲级】1021. Deepest Root (25)
- PAT甲级练习1021. Deepest Root (25)
- 1021. Deepest Root (25) PAT 甲级
- PAT甲级1021. Deepest Root (25)
- PAT 甲级 1021. Deepest Root (25)
- PAT 甲级1021. Deepest Root (25)
- [PAT-甲级]1021.Deepest Root
- PAT 甲级 Deepest Root (25)
- PAT甲级练习题A1021. Deepest Root (25)
- 1021. Deepest Root (25)-PAT
- PAT 1021. Deepest Root (25)
- PAT 1021. Deepest Root (25)
- PAT 1021. Deepest Root (25)
- PAT 1021. Deepest Root (25)
- PAT 1021. Deepest Root (25)
- 【PAT】1021. Deepest Root (25)
- pat 1021. Deepest Root (25)
- 【PAT】1021. Deepest Root (25)
- c++上机报告
- 第一个Python程序,猜大小
- Java__给定一个数组 AA,除了一个数出现一次之外,其余数都出现三次。找出出现一次的数。
- javascript学习记录
- Windows错误码编码问题
- PAT甲级1021. Deepest Root (25)
- Gym
- MFC 两个窗口 传递值
- HRBUST
- 斐波那契数列的四种简单实现方式
- Java OOP day03
- C# 分词算法,ChineseAnalyzer,源代码分析,其他地方的代码都是稀烂。。。。
- 蚂蚁感冒
- RHEL 5.7 Yum配置本地源[Errno 2] No such file or directory