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 (≤) 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

题目分析:最开始我理解错题意了 我认为给的连通图会有回路 但实际上是没有的

有回路的应该是不连通的

还要注意 用数组存会使空间过大 用vector<vector<int> >比较好

 #define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
int Highest = -;
vector<vector<int> >G;
int Dist[];
int Collected[];
int N;
int Components = ;
vector<int> V;
void dfs(int v)
{
Collected[v] = ;
for (int i = ; i < G[v].size(); i++)
{
if (!Collected[G[v][i]])
{
Dist[G[v][i]] = Dist[v] + ;
dfs(G[v][i]);
}
}
}
int main()
{
cin >> N;
G.resize(N + );
for (int i = ; i < N; i++)
{
int v1, v2;
cin >> v1 >> v2;
G[v1].push_back(v2);
G[v2].push_back(v1);
}
int i = ;
for (; i <= N; i++)
{
fill(Dist, Dist + N + , );
fill(Collected, Collected + N + , );
dfs(i);
for (int j = ; j <= N; j++)
{
if (!Collected[j])
{
dfs(j);
Components++;
}
}
if (Components != )
break;
int Max = -;
for (int i = ; i <= N; i++)
if (Max < Dist[i])
Max = Dist[i];
if (Max > Highest)
{
Highest = Max;
V.clear();
V.push_back(i);
}
else if (Max == Highest)
V.push_back(i);
}
if (Components == )
{
for (int i = ; i < V.size() - ; i++)
printf("%d\n", V[i]);
printf("%d", V[V.size() - ]);
}
else
printf("Error: %d components", Components);
return ;
}

1021 Deepest Root (25 分)的更多相关文章

  1. 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 ...

  2. 【PAT甲级】1021 Deepest Root (25 分)(暴力,DFS)

    题意: 输入一个正整数N(N<=10000),然后输入N-1条边,求使得这棵树深度最大的根节点,递增序输出.如果不是一棵树,输出这张图有几个部分. trick: 时间比较充裕数据可能也不是很极限 ...

  3. [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 ...

  4. 1021. Deepest Root (25)——DFS+并查集

    http://pat.zju.edu.cn/contests/pat-a-practise/1021 无环连通图也可以视为一棵树,选定图中任意一点作为根,如果这时候整个树的深度最大,则称其为 deep ...

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

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

  6. 1021. Deepest Root (25)

    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)(25 point(s))

    problem A graph which is connected and acyclic can be considered a tree. The height of the tree depe ...

  8. PAT-1021 Deepest Root (25 分) 并查集判断成环和联通+求树的深度

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

  9. PAT (Advanced Level) 1021. Deepest Root (25)

    先并查集判断连通性,然后暴力每个点作为根节点判即可. #include<iostream> #include<cstring> #include<cmath> #i ...

随机推荐

  1. 内网渗透之权限维持 - MSF与cs联动

    年初六 六六六 MSF和cs联动 msf连接cs 1.在队伍服务器上启动cs服务端 ./teamserver 团队服务器ip 连接密码 2.cs客户端连接攻击机 填团队服务器ip和密码,名字随便 ms ...

  2. ggplot2(7) 定位

    7.1 简介 位置调整:调整每个图层中出现重叠的对象的位置,对条形图和其他有组距的图形非常有用: 位置标度:控制数据到图形中位置的映射,常用的是对数变换: 分面:先将数据集划分为多个子集,然后将每个子 ...

  3. js中的堆和栈

    http://www.jscwwd.com/article/5e533ae2552a8e2bf45d3d69 这里先说两个概念:1.堆(heap)2.栈(stack)堆 是堆内存的简称.栈 是栈内存的 ...

  4. 为什么 String 是 immutable 类

    二哥,你能给我说说为什么 String 是 immutable 类(不可变对象)吗?我想研究它,想知道为什么它就不可变了,这种强烈的愿望就像想研究浩瀚的星空一样.但无奈自身功力有限,始终觉得雾里看花终 ...

  5. 浅谈ASP.NET Core中的DI

    DI的一些事 传送门马丁大叔的文章 什么是依赖注入(DI: Dependency Injection)?     依赖注入(DI)是一种面向对象的软件设计模式,主要是帮助开发人员开发出松耦合的应用程序 ...

  6. 【Weiss】【第03章】练习3.26:双端队列

    [练习3.26] 双端队列(deque)是由一些项的表组成的数据结构,对该数据结构可以进行下列操作: Push(X,D):将项X插入到双端队列D的前端. Pop(D):从双端队列D中删除前端项并返回. ...

  7. Mol Cell Proteomics. | Identification of salivary biomarkers for oral cancer detection with untargeted and targeted quantitative proteomics approaches (解读人:卜繁宇)

    文献名:Identification of salivary biomarkers for oral cancer detection with untargeted and targeted qua ...

  8. 理解Golang组件protobuf

    什么是protobuf protocol buffers 是一种语言无关.平台无关.可扩展的序列化结构数据的方法,它可用于(数据)通信协议.数据存储等.是一种灵活,高效,自动化机制的结构数据序列化方法 ...

  9. mpy开发物联网系列:1.mpy与服务器数据库方案

    ini配置文件与非关系型数据库 在使用micropython开发esp32过程中,经常涉及到一些数据的配置读取,而esp32本身micropython难以安装很多数据库客户端的库,只能基于本地文件使用 ...

  10. 一些Nmap NSE脚本推荐

    前言 Nmap是一款强大的开源扫描工具.同时Nmap提供了强大的脚本引擎(Nmap Scripting Engine),支持通过Lua脚本语言来扩展Nmap的功能,在Nmap的发行版中已经包含了数百个 ...