思路:
- 先任意选择一个结点,从该结点开始遍历整棵树,获取能达到的最深的顶点(记为集合A);然后从集合A中任意一个结点开始遍历整棵树,获取能达到的最深的顶点(记为集合B);集合A与集合B的并集即为使树高最大的根结点。
- set容器内元素的特点为自动有序(默认为从小到大)且不含重复元素
题目描述:
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 (≤104) 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<iostream>
#include<vector>
#include<cstring>//用于memset()---第二次DFS前对visit数组进行初始化
#include<set>
using namespace std;
const int MAXN = 1e4 + 10;
set<int>answer;//存放答案
vector<int>node[MAXN];//邻接表
bool visit[MAXN]= {false}; //用来标记结点是否被访问过
int maxHeight = 0;//记录最大高度
vector<int>temp;//temp临时存放DFS最远的结点结果
void DFS(int current,int height) {
visit[current]=true;
if(height>maxHeight) {
maxHeight = height;
temp.clear();//原先temp中存放的点已不满足距离最远
temp.push_back(current);//更新
} else if(height==maxHeight) {
temp.push_back(current);//添加满足条件的结点
}
//对当前结点的邻接点进行深搜DFS——node[current][i]表示current的邻接点
for(int i=0; i<node[current].size(); i++) {
if(visit[node[current][i]]==false) {
DFS(node[current][i],height+1);
}
}
}
int main() {
int n;
cin>>n;
int cnt=0;//记录连通分量的个数
int edge=n-1;//根据题意,边的个数比点的个数少1
while(edge--) {
int x,y;
cin>>x>>y;
node[x].push_back(y);//无向图
node[y].push_back(x);
}
int first;
for(int i=1; i<=n; i++) {
if(visit[i]==false) {
DFS(i,1);
cnt++;//连通分量的个数
if(i==1) { //记录第一次DFS的结果结点
if(temp.size()!=0){
first=temp[0];//结果结点中的一个结点
for(int i=0; i<temp.size(); i++) {
answer.insert(temp[i]);
}
}
}
}
}
if(cnt!=1) {
printf("Error: %d components",cnt);
return 0;
} else {
memset(visit,false,sizeof(visit));
temp.clear();
DFS(first,1);
for(int i=0; i<temp.size(); i++) {
answer.insert(temp[i]);
}
}
for(set<int>::iterator it=answer.begin(); it!=answer.end(); it++) {
cout<<*it<<endl;
}
return 0;
}
证明:(参考了《算法笔记》)
(1)证明从任意结点进行遍历,得到的最深结点一定是所求根结点集合的一部分。
假设使得树高最大的根结点为R,那么一定存在叶子结点L,使得R到L的长度为该树的最大高度(显然,R、L同属于所求根结点集合的一部分)。这里把R到L的路径拉成一条直线(称为树的直径,下同),如图所示,图中省略了不必要的结点,以线段概念长度代表节点之间的距离。此时,若从某个结点X进行遍历,若其得到的对大深度的根结点为Y,Y既不是L也不是R,那么此时一定有 O Y > O L OY>OL OY>OL,此时 R O + O L < R O + O Y RO+OL<RO+OY RO+OL<RO+OY,与假设的 R O + O L RO+OL RO+OL最大不符合,因此假设不成立。故从X进行遍历,得到的最深结点一定是L或R,即所求根结点集合的一部分。
(2)所有直径一定有公共重合区域或是交于一个公共点。
任意两条直径一定相交
如下图所示,假设存在两条长度相同且不相交的直径X-Y和W-Z,由于树是连通的,故一定存在P位于 X − Y X-Y X−Y,Q位于 W − Z W-Z W−Z,使得PQ可以相互到达。此时便可以利用PQ 拼凑出比 X − Y X-Y X−Y和 W − Z W-Z W−Z更长的路径,故假设不成立,即任意两条路径一定相交。
所有直径一定有一条公共区间或公共点
若直径 X 1 − Y 1 、 X 2 − Y 2 、 X 3 − Y 3 , X_1-Y_1、X_2-Y_2、X_3-Y_3, X1−Y1、X2−Y2、X3−Y3,相互相交但不交于一点,如下图所示,则 X 2 P + P R + R Y 2 < X 2 P + P Q + Q R + R Y 2 。 X_2P+PR+RY_2<X_2P+PQ+QR+RY_2。 X2P+PR+RY2<X2P+PQ+QR+RY2。公共区间的证明同理(公共区间我不太明白emm)。
(3)证明两次遍历结果的并集为所求的根结点集合。
A集合到P的距离都相等,B集合到Q的距离都相等
若选择的结点在PQ之间,则最深的结点一定位于A集合或B集合,再次遍历时一定是另一个集合,求并集则包含AB两集合的全部结点。
若在A或B集合中选择结点遍历,则结果一定为另一个集合