释放无限光明的是人心,制造无边黑暗的也是人心,光明和黑暗交织着,厮杀着,这就是我们为之眷恋又万般无奈的人世间。

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 (≤10​4​​) 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 <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#include<climits>//INT_MAX
//#include<bits/stdc++.h>
#define PP pair<ll,int>
#define inf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3fll
#define dinf 1000000000000.0
#define PI 3.1415926
typedef long long ll;
using namespace std;
int const mod=1e9+7;
const int maxn=2e3+10;
vector<vector<int>> mp;
bool vis[10010];
int mxh=1;
vector<int> tmp;
set<int> s;
void dfs(int node, int height) {if (height>mxh){tmp.clear();tmp.push_back(node);mxh=height;}else if (height==mxh)tmp.push_back(node);vis[node]=true;for (int i=0; i<mp[node].size(); i++){if(vis[mp[node][i]]==false)dfs(mp[node][i], height+1);}
}
int main() {int n,a,b;scanf("%d", &n);mp.resize(n + 1);for (int i=1; i<n; i++) {scanf("%d %d", &a, &b);mp[a].push_back(b);mp[b].push_back(a);}int ct=0,s1;for (int i=1; i<=n; i++) {if (vis[i]==false) {dfs(i, 1);if (i==1) {if(tmp.size()!=0)s1=tmp[0];for (int j=0; j<tmp.size(); j++)s.insert(tmp[j]);}ct++;}}if (ct>=2)printf("Error: %d components\n", ct);else {tmp.clear();fill(vis, vis+10010, false);dfs(s1, 1);for (int i=0; i<tmp.size(); i++)s.insert(tmp[i]);for (set<int>::iterator it=s.begin(); it!=s.end(); it++)printf("%d\n", *it);}return 0;
}

PTA Deepest Root (25分)相关推荐

  1. 【分析】1021 Deepest Root (25 分)【DFS解法】

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 A graph which is connected and acyclic can be considered a tree. ...

  2. 【PAT - 甲级1021】Deepest Root (25分)(并查集,暴力枚举)

    题干: A graph which is connected and acyclic can be considered a tree. The height of the tree depends ...

  3. 1021 Deepest Root (25 分) 【难度: 中 / 知识点: 树的直径 连通块】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805482919673856 方法一: 数组模拟邻接表 第一步: 爆搜df ...

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

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

  5. 1021. Deepest Root (25)

    题目链接:http://www.patest.cn/contests/pat-a-practise/1021 题目: 1021. Deepest Root (25) 时间限制 1500 ms 内存限制 ...

  6. 浙大PAT 1021. Deepest Root (25)

    1021. Deepest Root (25) 时间限制 1500 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A graph ...

  7. PTA 1055 集体照 (25 分) C++实现

    1055 集体照 (25 分) 拍集体照时队形很重要,这里对给定的 N 个人 K 排的队形设计排队规则如下: 每排人数为 N/K(向下取整),多出来的人全部站在最后一排: 后排所有人的个子都不比前排任 ...

  8. PTA 星际探险 (25分)(Dijkstra改约束条件)

    7-15 星际探险 (25分) 在某个遥远的未来,新新人类将可能这样进行星际探险:宇宙中分布着若干个跳跃点,人类飞船在每个跳跃点可超光速跳至其它跳跃点.当然,一般来说每次跳跃是要消耗一定能量的,但因为 ...

  9. PTA 彩虹瓶 (25分)

    问题描述: 彩虹瓶的制作过程(并不)是这样的:先把一大批空瓶铺放在装填场地上,然后按照一定的顺序将每种颜色的小球均匀撒到这批瓶子里. 假设彩虹瓶里要按顺序装 N 种颜色的小球(不妨将顺序就编号为 1 ...

最新文章

  1. C 的大致运行原理。
  2. matlab 2017 ubuntu,ubuntu安装matlab R2017,
  3. Debian 升级到 PHP 7,并支持并行安装
  4. 在PHP服务器上使用JavaScript进行缓慢的Loris攻击[及其预防措施!]
  5. 大三软件工程小项目-小技术集合-Qt状态栏设置
  6. Flutter RefreshIndicator 下拉刷新组件 Material 风格的刷新效果
  7. Eclipse 常用快捷键及使用技巧!
  8. spring-security3 配置和使用
  9. 《异构信息网络挖掘: 原理和方法》—— 1.3 本书的内容组织
  10. 域名解析:记录类型的含义
  11. Wattagio for Mac(电池管理) 免激活版
  12. 第二次作业:分布式版本控制系统Git的安装与使用
  13. 在Ubuntu18.04TLS下安装小米随身wifi驱动
  14. 压箱底的10款在线工具平台
  15. [blockchain-042]eos 硅谷ecs docer编译 mongo存储
  16. 2018高中计算机会考时间,2018年高中会考什么时候考哪些科目
  17. 用Python删除含有特定字符串的行
  18. matplotlib中文显示以及设置图片大小
  19. Tomcat执行startup.bat出现闪退的可能原因
  20. 永磁同步电机的标么值系统

热门文章

  1. c#十二星座速配系统_星座物语 十二星座匹配腕表推荐
  2. 哈夫曼编码-Java实现
  3. 整理各类IT编程视频教程下载地址
  4. Buiding 7z source code on Mac
  5. (BAT批处理)如何穿透多个文件夹批量修改文件后缀名?
  6. oracle 跨平台adg,oracle ADG 跨版本跨平台搭建实测
  7. c语言写modbus协议,C语言编写modbus协议
  8. 智能交通系统的架构图
  9. 专题:手把手学习硬件基础------14、与或非以及触发器
  10. 计算机应用基础毕业能做什么工作,铁路专业能学什么?毕业能做哪些工作?