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
 #include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int G[][];
int father[];
int maxDepth = -;
int N;
vector<int> temp_, ans;
void dfs(int vt, int from, int level){
level++;
if(maxDepth < level){
maxDepth = level;
temp_.clear();
temp_.push_back(vt);
}else if(maxDepth == level){
temp_.push_back(vt);
}
for(int i = ; i <= N; i++){
if(G[vt][i] == && i != from){
dfs(i, vt, level);
}
}
}
int findFather(int vt){
int temp = vt;
while(vt != father[vt]){
vt = father[vt];
}
int temp2;
while(temp != vt){
temp2 = father[temp];
father[temp] = vt;
temp = temp2;
}
return vt;
}
int main(){
scanf("%d", &N);
int tempA, tempB;
int root1, root2;
for(int i = ; i <= N; i++){
father[i] = i;
}
for(int i = ; i < N - ; i++){
scanf("%d%d", &tempA, &tempB);
G[tempA][tempB] = G[tempB][tempA] = ;
root1 = findFather(tempA);
root2 = findFather(tempB);
if(root1 != root2)
father[root2] = root1;
}
int cnt = ;
for(int i = ; i <= N; i++){
if(father[i] == i)
cnt++;
}
if(cnt > ){
printf("Error: %d components\n", cnt);
}else{
maxDepth = -;
dfs(, -, );
ans = temp_;
temp_.clear();
maxDepth = -;
dfs(ans[], -, );
for(int i = ; i < temp_.size(); i++)
ans.push_back(temp_[i]);
sort(ans.begin(), ans.end());
printf("%d\n", ans[]);
for(int i = ; i < ans.size(); i++){
if(ans[i] != ans[i - ])
printf("%d\n", ans[i]);
}
}
cin >> N;
return ;
}

总结:

1、题意:给出一个可能是树的图,要求找到root(可能多个),使得从root出发可以使树的高度最高。但如果给出的图不是树(因为N个顶点 N - 1条边,如果产生回路只能说明输入的图不是一个连通图),要求输出连通分量的个数。 起初没注意到N个节点 N - 1条边这个条件,没有搞清楚题意,还以为判断是否是树是通过判断图里有没有回路来进行的,莫名奇妙为什么不是树之后就要求联通分量。于是还通过找回路来判断是否是树,结果当然不对。

2、求连通分量首选并查集,如果两个点是联通的就把它们并为一个集合。在读入边的时候就可以做合并,结束之后计数father[ i ] = i 的节点个数就行了。其次可以借助深搜广搜配合visit数组,在遍历完每一个点,检查下一个点的visit为0的即作为一个连通分量。显然,当输入的图连通分量大于1时说明不是树。

3、找能使得树最高的root方法:先随便找一个点A,从它开始做一遍dfs并在过程中将level最大的节点全部保存。这些节点就是答案的一部分,但还存在遗漏。再从已经选出的root中随意选择一个B,从它开始遍历一遍dfs并保存level最大的节点,两部分并起来就是最终答案。(没看书之前我是暴力破解,把所有节点遍历一次以找到最大深度的根,结果有一个测试点超时)

4、visit数组:平时做bfs使用visit数组标记节点是否曾经加入过队列,做dfs标记节点是否被访问过,其实都是为了防止重复访问节点。但本题中一旦确定了是一棵树,说明不存在回路,则可以不需要visit数组。但在无向图中仍然需要一个变量保存上一次访问过的节点,以防止同一条边上的重复访问。比如节点A、B在一条边上,A->B访问结束,到B之后B的联通的节点中又会有A,造成再次访问A(用孩子法存储的树无需考虑该问题,但用图的形式存储的树必须考虑同一条边的重复访问)。

5、找回路:利用visit数组与上次访问节点的序号即可。在碰到一个与当前节点相连接、不是上一次访问过的再同一条边上的节点、visit数组为1,则存在回路。

6、sort的默认排序是从小到大。

A1021. Deepest Root的更多相关文章

  1. PAT A1021 Deepest Root (25 分)——图的BFS,DFS

    A graph which is connected and acyclic can be considered a tree. The hight of the tree depends on th ...

  2. PAT甲级——A1021 Deepest Root

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

  3. PAT Advanced A1021 Deepest Root (25) [图的遍历,DFS,计算连通分量的个数,BFS,并查集]

    题目 A graph which is connected and acyclic can be considered a tree. The height of the tree depends o ...

  4. [PAT] A1021 Deepest Root

    [题目大意] 给出n个结点和n-1条边,问它们能否形成一棵n个结点的树,如果能,从中选出结点作为树根,使整棵树的高度最大.输出所有满足要求的可以作为树根的结点. [思路] 方法一:模拟. 1 连通.边 ...

  5. PAT_A1021#Deepest Root

    Source: PAT A1021 Deepest Root (25 分) Description: A graph which is connected and acyclic can be con ...

  6. 1021.Deepest Root (并查集+DFS树的深度)

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

  7. 1021. Deepest Root (25) -并查集判树 -BFS求深度

    题目如下: A graph which is connected and acyclic can be considered a tree. The height of the tree depend ...

  8. PAT1021:Deepest Root

    1021. Deepest Root (25) 时间限制 1500 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A graph ...

  9. 1021. Deepest Root (25)

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

随机推荐

  1. picker-view 组件 的value失效问题

    首先检查是不是漏了绑定关系 组件内 组件引用 如过还不行就用下面的方法,顺序问题 在给暂时列表赋值之后再对value赋值

  2. 本地上传项目到github

    https://www.cnblogs.com/rosej/p/6056467.html(copy)

  3. C# Note23: 如何自定义类型使用foreach循环

    前言 在foreach语句代码中,我们经常是对List,Collection,Dictionary等类型的数据进行操作,不过C#允许用户自定义自己的类型来使用foreach语句.那么自定义类型能够使用 ...

  4. rpm和yum

    RMP(红帽软件包管理器) RPM有点像Windows系统中的控制面板,会建立统一的数据库文件,详细记录软件信息并能够自动分析依赖关系. YUM(软件仓库)

  5. 通用模块设计UMD

    https://leohxj.gitbooks.io/front-end-database/content/javascript-modules/about-umd.html UMD(universa ...

  6. flask 保存文件到 七牛云

    上篇文章队长讲述了如何把前端上传的文件保存到本地项目目录 本篇 讲述一下把前端上传的文件保存到 第三方存储(七牛云) 七牛云相关步骤思路: 首先 进去七牛云官网,注册并实名认证来获取一个七牛云账号和存 ...

  7. poj-1459(网络流-最大流)

    题意:给你n个点的电网系统,有一些点是电站,能提供p的电能,有些点是用户,能消耗c的电能,有些是过渡站,不消耗不产生(等于没用),然后m条电线(x,y,w),代表x可以向y运输w的电能,问你这个电网系 ...

  8. 一、Java多人博客系统-开篇

    作为一个程序员,工作之外的不断学习是必须的.这个项目是我个人课外学习和练手的项目.最开始是一个个人网站.当时发现京东云可以免费部署网站的,就立即写了一个网站,当时就使用jsp技术,可以实现发布博客.评 ...

  9. Qt QLabel的使用

    QLabel类主要用来文本和图像的显示,没有提供用户交互功能.QLabel对象的视觉外观可以由用户自定义配置. 它还可以为另外一个可获得焦点的控件作为焦点助力器. QLabel可以显示下列的所有类型: ...

  10. C# 两个类是否继承关系

    IsAssignableFrom:确定指定类型的实例是否可以分配给当前类型的实例 B继承自A static void Main(string[] args) { Type a = typeof(A); ...