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
思路:
这道题可以分为两步来做。第一步,利用并查集判断图是否连通。每读入一条边的两个端点,判断这两个端点是否属于相同的集合(即集合的根节点是否相同),如果不同将它们合并到一个集合中。这样,当处理完所有边后就可以根据最终产生的集合个数是否为1来判定给定的图是否连通。第二步,当图连通的时候,我们需要找出合适的根节点,使树的高度最大。具体做法为,先任意选择一个结点,从该结点开始遍历整棵树,获取能达到最深的顶点(记为结点集合A),然后从集合A中任意一个结点出发遍历整棵树,获取能达到的最深的顶点(记为结点集合B),这样集合A与集合B的并集即为所求大的使树高最大的根结点。(证明过程见《算法笔记》第344页)
代码:
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 100010;
vector<int>G[N];//邻接表
bool isRoot[N];//记录每个结点是否是某个集合的根节点
int father[N];
int findFather(int x)
{
int a = x;
while (x!=father[x])
{
x = father[x];
}
//路径压缩
while (a!=father[a])
{
int z = a;
a = father[a];
father[z] = x;
}
return x;
}
void Union(int a, int b) {
int faA = findFather(a);
int faB = findFather(b);
if (faA != faB) {
father[faA] = faB;
}
}
void init(int n)
{
for (int i = 1; i <=n; i++)
{
father[i] = i;
}
}
int calBlock(int n)
{
int block = 0;
for (int i = 1; i <= n; i++)
{
isRoot[findFather(i)] = true;
}
for (int i = 1; i <= n; i++)
{
block += isRoot[i];
}
return block;
}
int maxH = 0;//最大高度
vector<int>temp, Ans;//temp临时存放DFS的最远结点结果,Ans保存答案
//DFS函数,u为当前结点的编号,Height为当前树高,pre为u的父节点
void DFS(int u, int Height, int pre)
{
if (Height > maxH) {
temp.clear();
temp.push_back(u);
maxH = Height;
}
else if (Height == maxH) {//如果树高等于最大树高
temp.push_back(u);//将当前结点加入到temp中
}
for (int i = 0; i < G[u].size(); i++)
{
if (G[u][i] == pre) continue;//由于邻接表中存放无向图,因此需要跳过回去的边
DFS(G[u][i], Height + 1, u);
}
}
int main() {
int a, b, n;
scanf("%d", &n);
init(n);//并查集初始化
for (int i = 1; i < n; i++) {
scanf("%d%d", &a, &b);
G[a].push_back(b);
G[b].push_back(a);
Union(a, b);
}
int Block = calBlock(n);
if (Block != 1) {
printf("Error: %d components\n", Block);
}
else {
DFS(1, 1, -1);
Ans = temp;
DFS(Ans[0], 1, -1);//从任一个根节点开始遍历
for (int i = 0; i < temp.size(); i++) {
Ans.push_back(temp[i]);//此时temp为集合B,将其加入到Ans中
}
sort(Ans.begin(), Ans.end());
printf("%d\n", Ans[0]);
for (int i = 1; i < Ans.size(); i++)
{
if (Ans[i] != Ans[i - 1]){//重复编号不输出
printf("%d\n", Ans[i]);
}
}
}
return 0;
}
复习:
这道题思路不是很难,但是在实现的时候有些地方是很需要注意的。首先是用temp保存以1号结点为根节点时,数深度最大的节点,令Ans=temp;然后在temp里面任取一个结点(这里为temp[0]作为根结点进行遍历找出最深的结点)再找出树深度最大的结点,然后加入到Ans中去,输出的时候要防止输出重复的元素,即比较Ans[i]和[i-1]是否相等。还有由于图是无向的,在使用DFS的时候,要保留当前结点的前驱结点,遍历的时候要防止从子结点到父结点的逆向访问。
二刷代码:
#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn = 10010;
vector<int>G[maxn];
int father[maxn];
bool isRoot[maxn];
int findFather(int x)
{
int a = x;
while (x!=father[x])
{
x = father[x];
}
//路径压缩
while (a!=father[a])
{
int z = a;
a = father[a];
father[z] = x;
}
return x;
}
void Union(int a, int b)
{
int father_a = findFather(a);
int father_b = findFather(b);
if (father_a != father_b)
father[father_a] = father_b;
}
void init(int n)
{
for (int i = 1; i <= n; i++)
father[i] = i;
}
int calBlock(int n)
{
int block = 0;
for (int i = 1; i <= n; i++)
{
isRoot[findFather(i)] = true;
}
for (int i = 1; i <= n; i++)
{
block += isRoot[i];
}
return block;
}
int maxH = 0;
vector<int>temp, Ans;//temp临时存放DFS的最远结点结果,Ans保存答案
//DFS函数,u为当前访问结点,Height为当前树高,pre为u的父结点,因为图是无向的,保存父结点是防止回头访问父结点
void DFS(int u, int Height, int pre)
{
if (Height > maxH)
{
temp.clear();
temp.push_back(u);
maxH = Height;
}
else if(Height==maxH)
{
temp.push_back(u);
}
for (int i = 0; i < G[u].size(); i++)
{
if (G[u][i] == pre) continue;//由于图是无向图,所以需要跳过回去的边
DFS(G[u][i], Height + 1, u);
}
}
int main()
{
int a, b, n;
scanf("%d", &n);
init(n);
for (int i = 1; i < n; i++)
{
scanf("%d%d", &a, &b);
G[a].push_back(b);
G[b].push_back(a);
Union(a, b);
}
int Block = calBlock(n);
if (Block != 1)
{
printf("Error: %d components", Block);
}
else
{
DFS(1, 1, -1);
Ans = temp;
DFS(temp[0], 1, -1);
for (int i = 0; i < temp.size(); i++)
{
Ans.push_back(temp[i]);
}
sort(Ans.begin(), Ans.end());
printf("%d\n", Ans[0]);
for (int i = 1; i < Ans.size(); i++)
{
if (Ans[i] != Ans[i - 1])
{
printf("%d\n", Ans[i]);
}
}
}
return 0;
}