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. iOS 获取内存大小使用情况(进度条显示)

    一.获取设备内存大小方法 //返回存储内存占用比例 - (NSString *)getFreeDiskspaceRate{ float totalSpace; .f; NSError *error = ...

  2. 关于php一些数组函数

    array_column($array,"key") 将二维数组中的键名为key的数据生成一个新数组 array_unique($array) 去除重复值

  3. javaweb学习总结六(泛型)

    一:泛型的概念 泛型用来现在集合中的存储类型,防止取出时强制转换发生错误. 1:没有使用泛型时,如下: @Test public void test1() { List list = new Arra ...

  4. [书目20131114]微软技术丛书•快速软件开发(珍藏版) Rapid Development Taming Wild Software Schedules By Steve McConnell

    本文摘自:http://shop.oreilly.com/product/9781556159008.do EFFICIENT DEVELOPMENT Chapter 1 Welcome to Rap ...

  5. LeetCode 238

    Product of Array Except Self Given an array of n integers where n > 1, nums, return an array outp ...

  6. [改善Java代码]线程优先级只使用三个等级

    线程的优先级(priority)决定了线程获得CPU运行的机会,优先级越高获得的运行机会越大,优先级越低获得的机会越小.Java的线程有10个级别(准确的说是11个级别,级别为0的线程是JVM,应用程 ...

  7. WCF简介

    WCF(Windows communication Foundation),顾名思义,就是在windows平台下解决通信的基础框架.WCF做为.NET Framework 3.0的一个组件发布出来的, ...

  8. 和阿文一起学H5--如何把H5压缩到最小

    三种压缩图片的方法: 1.PS 但是PS每次只能压缩一张,下面介绍第二个神器 2.TinyPng压缩 https://tinypng.com/ 3.IloveIMG压缩 http://www.ilov ...

  9. 开源而又强大的迷你型web服务器推荐

    appweb显然是不二之选,看了下最新版,已经到了4了 下载下来,http://appwebserver.org/software/appweb-4.4.4-0-src.tgz,十几M,直接吓傻,离我 ...

  10. Jquery 获得服务器控件值的方法小结(转)

    由于ASP.NET网页运行后,服务器控件会随机生成客户端id,jquery获取时候不太好操作,google了下,总结有以下3种方法. <!--服务器控件代码:--> <asp:Tex ...