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 <iostream>

 #include <vector>

 #include <queue>

 using namespace std;

 vector<int> adj[];
int visit[];
int Tree[];
int root[];
int MM[];
int num;
int getroot(int x) { if(Tree[x]==-) return x; else {
int tem=getroot(Tree[x]);
Tree[x]=tem;
return tem;
} } void DFS(int x,int d) {
visit[x]=;
int i;
for(i=;i<adj[x].size();i++)
{ if(visit[adj[x][i]]==)
DFS(adj[x][i],d+);
}
root[num++]=d;
} int main() {
int n,a,b,i,j;
while(cin>>n)
{
for(i=;i<=n;i++)//初始化
{
Tree[i]=-;
adj[i].clear();
}
for(i=;i<n-;i++)
{
cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
a=getroot(a);//并查集
b=getroot(b);
if(a!=b)
{
Tree[a]=b;
}
}
int count=;//极大连通图个数
for(i=;i<=n;i++)
{
if(Tree[i]==-) count++;
}
if(count!=)
{
cout<<"Error: "<<count<<" components"<<endl;//不是树
}
else
{
for(i=;i<=n;i++)
{
for(j=;j<=n;j++)//每次查找都要初始化
visit[j]=;
num=;
DFS(i,);
MM[i]=;
for(j=;j<num;j++)
{
if(MM[i]<root[j])
MM[i]=root[j];
}
}
int max=;
for(i=;i<=n;i++)
{
if(max<MM[i])
max=MM[i];
}
for(i=;i<=n;i++)
{
if(max==MM[i])
cout<<i<<endl;
}
}
}
return ;
}

1021.Deepest Root (并查集+DFS树的深度)的更多相关文章

  1. PAT 甲级 1021 Deepest Root (并查集,树的遍历)

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

  2. PAT 1021 Deepest Root[并查集、dfs][难]

    1021 Deepest Root (25)(25 分) A graph which is connected and acyclic can be considered a tree. The he ...

  3. PAT 甲级 1021 Deepest Root (25 分)(bfs求树高,又可能存在part数part>2的情况)

    1021 Deepest Root (25 分)   A graph which is connected and acyclic can be considered a tree. The heig ...

  4. PAT甲级1021. Deepest Root

    PAT甲级1021. Deepest Root 题意: 连接和非循环的图可以被认为是一棵树.树的高度取决于所选的根.现在你应该找到导致最高树的根.这样的根称为最深根. 输入规格: 每个输入文件包含一个 ...

  5. [PAT] 1021 Deepest Root (25)(25 分)

    1021 Deepest Root (25)(25 分)A graph which is connected and acyclic can be considered a tree. The hei ...

  6. BZOJ 3910 并查集+线段树合并

    思路: 1. 并查集+线段树合并 记得f[LCA]==LCA的时候 f[LCA]=fa[LCA] 2.LCT(并不会写啊...) //By SiriusRen #include <cstdio& ...

  7. UVA1455 - Kingdom(并查集 + 线段树)

    UVA1455 - Kingdom(并查集 + 线段树) 题目链接 题目大意:一个平面内,给你n个整数点,两种类型的操作:road x y 把city x 和city y连接起来,line fnum ...

  8. 【bzoj5133】[CodePlus2017年12月]白金元首与独舞 并查集+矩阵树定理

    题目描述 给定一个 $n\times m$ 的方格图,每个格子有 ↑.↓.←.→,表示从该格子能够走到相邻的哪个格子.有一些格子是空着的,需要填上四者之一,需要满足:最终的方格图中,从任意一个位置出发 ...

  9. 并查集&线段树&树状数组&排序二叉树

    超级无敌巨牛逼并查集(带权并查集)https://vjudge.net/problem/UVALive-4487 带删点的加权并查集 https://vjudge.net/problem/UVA-11 ...

随机推荐

  1. CoreDate的使用

    勾选 xcode的 CoreDate会帮我们自动创建 CoreData 但是我们通常不那样使用,通常把 CoreDate 在单利类中创建, // // ZYDAO.h // StoryboardTes ...

  2. yii2在ubuntu下执行定时任务

    一.编辑yii console/controllers TestController.php 二./usr/ 包括与系统用户直接有关的文件和目录创建sh_scripts目录,/usr/sh_scrip ...

  3. 如何用C表示排列组合?

    问题来自<Linux C一站式编程>,是个挺有意思的题目. 2.定义一个数组,编程打印它的全排列.比如定义: #define N 3 int a[N] = { 1, 2, 3 }; 则运行 ...

  4. Java Classloader原理分析

       类的加载过程指通过一个类的全限定名来获取描述此类的二进制字节流,并将其转化为方法区的数据结构,进而生成一个java.lang.Class对象作为方法区这个类各种数据访问的入口.这个过程通过Jav ...

  5. 使用迭代器遍历List的时候修改List报ConcurrentModificationException异常的解决办法

    为了避免这种异常,我们可以使用CopyOnWriteArrayList来代替ArrayList,CopyOnWriteArrayList支持并发访问,所以同时进行迭代和修改是没有问题的.

  6. 转:iOS基于MVC的项目重构总结

    转:http://www.cocoachina.com/ios/20160519/16346.html 关于MVC的争论 关于MVC的争论已经有很多,对此我的观点是:对于iOS开发中的绝大部分场景来说 ...

  7. 修改ie版本的注册表webbroswer

    Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Exp ...

  8. JMS - ExceptionListener

    If a JMS provider detects a problem with a connection, it will inform the connection’s ExceptionList ...

  9. Ubuntu系统应用程序创建快捷方式的方法

    大家安装了最新版的Ubuntu 14.0系统之后可能觉得很不习惯,因为Ubuntu的桌面干干净净没有任何快捷方式,任务栏的图标拖不下来,右键点击程序图标也没有创建快捷方式的菜单选项: 那如何把自己经常 ...

  10. TCP/IP——内外网IP+子网掩码作用+PING(网络总结)

    目录: 1.如何区分内网IP和外网IP? 保留字段 2.子网掩码是起什么作用的? 将DNS和IP异或,表示哪段起作用 3.ping到底起什么作用? ping本地.ping远程 下面针对上面三个问题分别 ...