1021 Deepest Root (25 分)
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 分)的更多相关文章
- 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 ...
- 【PAT甲级】1021 Deepest Root (25 分)(暴力,DFS)
题意: 输入一个正整数N(N<=10000),然后输入N-1条边,求使得这棵树深度最大的根节点,递增序输出.如果不是一棵树,输出这张图有几个部分. trick: 时间比较充裕数据可能也不是很极限 ...
- [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 ...
- 1021. Deepest Root (25)——DFS+并查集
http://pat.zju.edu.cn/contests/pat-a-practise/1021 无环连通图也可以视为一棵树,选定图中任意一点作为根,如果这时候整个树的深度最大,则称其为 deep ...
- 1021. Deepest Root (25) -并查集判树 -BFS求深度
题目如下: A graph which is connected and acyclic can be considered a tree. The height of the tree depend ...
- 1021. Deepest Root (25)
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
- 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 ...
- 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 ...
- PAT (Advanced Level) 1021. Deepest Root (25)
先并查集判断连通性,然后暴力每个点作为根节点判即可. #include<iostream> #include<cstring> #include<cmath> #i ...
随机推荐
- SpringBoot内置的各种Starter是怎样构建的?--SpringBoot源码(六)
注:该源码分析对应SpringBoot版本为2.1.0.RELEASE 1 温故而知新 本篇接 外部配置属性值是如何被绑定到XxxProperties类属性上的?--SpringBoot源码(五) 温 ...
- axios请求拦截器
import axios from 'axios'; // 创建axios实例 let service = null; if (process.env.NODE_ENV === 'deve ...
- java -输入年龄判断是否符合范围。
//创建的一个包名. package demo3; //定义一个类. public class Test { //公共静态的主方法. public static void main(String[] ...
- [Alg] 文本匹配-单模匹配-KMP
1. 暴力求解 如下图所示.蓝色的小三角表示和sequence比较时的开始字符,绿色小三角表示失败后模式串比对的开始字符,红色框表示当前比较的字符对. 当和模式串发生不匹配时,蓝色小三角后移一位,绿色 ...
- Android UI性能测试——使用 Systrace 查找问题
一 官方文档翻译 官文地址:https://developer.android.com/studio/command-line/systrace systrace命令允许您在系统级别上收集和检查所有运 ...
- 发布一个npm包(webpack loader)
发布一个npm包,webpack loader: reverse-color-loader,实现颜色反转. 初始化项目 mkdir reverse-color-loader cd ./reverse- ...
- Android RadioGroup+Fragment 底部导航
- 一款带Web面板的轻量级、高性能内网穿透工具:nps使用教程
说明:内网穿透工具之前已经介绍了不少了,比如Frp.lanproxy.Holer等,现在再介绍个带Web面板的穿透工具nps,之前叫easyProxy,只是改名了而已,该工具是一款使用go语言编写的轻 ...
- 使用burp插件captcha-killer识别图片验证码
0x01 开发背景 说起对存在验证码的登录表单进行爆破,大部分人都会想到PKav HTTP Fuzzer,这款工具在前些年确实给我们带来了不少便利.反观burp一直没有一个高度自定义通杀大部分图片验证 ...
- 常用的FTP命令
FTP命令 ftp> ascii # 设定以ASCII方式传送文件(缺省值) ftp> bell # 每完成一次文件传送,报警提示. ftp> binary # 设定以二进制方式传送 ...