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. 判断某个点是否在某个view上

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObjec ...

  2. 超详细,多图文使用galera cluster搭建mysql集群并介绍wsrep相关参数

    超详细,多图文使用galera cluster搭建mysql集群并介绍wsrep相关参数 介绍galera cluster原理的文章已经有一大堆了,百度几篇看一看就能有相关了解,这里就不赘述了.本文主 ...

  3. office一直提示配置进度与图标问题

    原来安装了wps office,广告太烦,于是卸载了wps安装Microsoft office 2010,安装完成后每次打开excel文件都要重新安装配置,修改注册表norereg和设置兼容模式都不行 ...

  4. Spark实战--搭建我们的Spark分布式架构

    Spark的分布式架构 如我们所知,spark之所以强大,除了强大的数据处理功能,另一个优势就在于良好的分布式架构.举一个例子在Spark实战--寻找5亿次访问中,访问次数最多的人中,我用四个spar ...

  5. HDU 5448 Marisa’s Cake

    给定一个由n个整点构成的凸多边形,求从n个点里任意选不少于3个点组成的所有凸多边形的面积之和,显然整点构成的多边形面积一定是0.5的整数倍,所以题目需要你算出答案的2倍 mod1000000007的值 ...

  6. Spring Cloud - Nacos注册中心入门单机模式及集群模式

    近几年微服务很火,Spring Cloud提供了为服务领域的一整套解决方案.其中Spring Cloud Alibaba是我们SpringCloud的一个子项目,是提供微服务开发的一站式解决方案. 包 ...

  7. Java-迭代器(新手)

    //导入的包.import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;//创建的一个类.pub ...

  8. Python下载各种功能包出问题

    问题详情 点击之后出现 AttributeError: module 'importlib._bootstrap' has no attribute 'SourceFileLoader' 解决方法 c ...

  9. pycharm 更换pip镜像源为国内,解决下载慢的问题

    参考链接:https://www.cnblogs.com/hkgov/p/7799078.html 官方源下载速度太慢,换成国内源会很快. 推荐清华的源:https://pypi.tuna.tsing ...

  10. Spring框架——IOC 容器的创建与使用

    企业级开发框架 Spring Framework 是整个 Spring 生态的基础,各个模块都是基于 Spring Framework 衍生出来的. Spring 的两大核心机制 IOC 控制翻转.A ...