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. C语言宏定义相关

    写好C语言,漂亮的宏定义很重要,使用宏定义可以防止出错,提高可移植性,可读性,方便性 等等.下面列举一些成熟软件中常用得宏定义......1,防止一个头文件被重复包含#ifndef COMDEF_H# ...

  2. NSBundle介绍及使用

    bundle 是一个目录,其中包含了程序会使用到的资源.这些资源包含了如图像,声音,编译好的代码,nib文件(用户也会把bundle称为plug-in).对应bundle, cocoa提供了类NSBu ...

  3. oracle10g 和oracle11g同时安装时PL/SQL连不上解决方案

    oracle10g 和oracle11g同时安装的时候,PL/SQL连不上解决办法:找到两者的配置文件改成一致 oracle10g服务端和oracle11g客户端同时安装的时候,PL/SQL连不上解决 ...

  4. ARM Linux bootloader笔记

    .text //指定了后续编译出来的内容放在代码段[可执行] .global //告诉编译器后续跟的是一个全局可见的名字[可能是变量,也可以是函数名] _start /*函数的其实地址,也是编译.链接 ...

  5. Hibernate的几种主键生成策略

    主键类型: 业务主键(natural key):业务主键的值是来源于一个业务数据. 代理主键(surrogate key):代理主键需要采用一种方式来生成某个唯一值. 代理主键的生成策略: 1.hib ...

  6. 解析$.grep()源码及透过$.grep()看(两次取反)!!的作用

    先上jquery源码: grep: function( elems, callback, inv ) { var retVal, ret = [], i = 0, length = elems.len ...

  7. LeetCode 326

    Power of Three Given an integer, write a function to determine if it is a power of three. Follow up: ...

  8. hdu 2838 树状数组

    思路:从后面往前面插,用一个二维树状数组保存,c[i][0]表示比i小的元素和,c[i][1]表示比i小的元素个数. #include<iostream> #include<cstr ...

  9. Python Quick Start

    1.安装Python 官网下载python: https://www.python.org/ 有2.x 3.x版本, 注意,python3.0不向下兼容2.x版本,有很多包3.0不提供 下载完后直接点 ...

  10. iOS app应用界面加载卡顿的问题

    刚发布版本,忽然发现加载界面需要3-5秒延迟,那么问题来了. 首先,发现问题: 1.看代码,基于之前版本更新都没出问题,还是比较确信不是代码中的bug,以防万一,还是仔细看了下关于界面跳转部分的代码, ...