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 ...
随机推荐
- 开源一个使用python和pyQT实现的产测工具
导语 之前给朋友友情开发的一个产测工具,现开源,有需要的朋友可以在这个基础上进行二次开发. 操作界面如下 主要特性 自动识别启动信息,然后进入产测写入状态 序列号和MAC地址自动按指定数目增加 每次操 ...
- java算法--稀疏数组
数据结构必要知识 线性结构 线性结构是最常用的数据结构,数据元素之间存在一对一的线性关系. 线性结构有两种不同的存储结构,即顺序存储结构和链式存储结构.顺序存储的线性表称为顺序表,顺序表中的存储元素是 ...
- 李宏毅深度学习与人类语言处理-introduction
深度学习与人类语言处理(Deep learning for Human Language Processing) 李宏毅老师深度学习与人类语言处理课程笔记,请看正文 这门课会学到什么? 为什么叫人类语 ...
- CSS 文本截断方案
单行截断 .ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } 此方法兼容到ie6过.不过只能单行 ...
- javaee作业
一.单选题(共5题,50.0分) 1 在SqlSession对象的openSession()方法中,不能作为参数executorType的可选值 的是( ). A. ExecutorTyp ...
- 一起了解 .Net Foundation 项目 No.19
.Net 基金会中包含有很多优秀的项目,今天就和笔者一起了解一下其中的一些优秀作品吧. 中文介绍 中文介绍内容翻译自英文介绍,主要采用意译.如与原文存在出入,请以原文为准. Salesforce To ...
- JavaScript规范----DOM操作
一般通过JS代码操作DOM结构,会触发浏览器进行页面渲染.所以要尽量减少DOM操作,避免频繁的页面渲染对性能造成影响. 如有以下场景:程序执行ajax请求,并根据请求结果动态添加列表项.常见的做法是循 ...
- 【python】定时锁屏,保护身体
前言 最近越来越懒,一上班坐到电脑前就不愿意动,不喝水也不起来走动,一下班离开电脑就头晕眼花.想起前两年被肾结石支配的恐惧o(╥﹏╥)o,,,还是写个小工具强制自己喝水防止复发吧.VS Code启动 ...
- jQuery万能放大镜插件(普通矩形放大镜)
插件链接:http://files.cnblogs.com/files/whosMeya/magnifier.js 1.在jquery下插入. 2.格式:magnifier("需要插入的位置 ...
- 【Weiss】【第03章】练习3.26:双端队列
[练习3.26] 双端队列(deque)是由一些项的表组成的数据结构,对该数据结构可以进行下列操作: Push(X,D):将项X插入到双端队列D的前端. Pop(D):从双端队列D中删除前端项并返回. ...