题干:

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

题目大意:

一个连通的非循环图可以看作是一个树。树的高度取决于所选的根。现在你应该找到根,结果是最高的树。这样的根叫做最深的根。对于每个测试用例,在一行中打印每个最深的根。如果这样的根不是惟一的,则按其数字的递增顺序打印它们。如果给定的图形不是树,则打印Error: K components,其中K是图中连图分量的数量。

解题报告:

因为时限是两秒,直接枚举每一个点当根节点就可以。但是这题其实可以优化,只枚举叶子节点就可以。甚至可以用树的直径去乱搞一波,时间复杂度会更优。但这题时限很宽,所以没必要优化。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
int n,m,f[MAX],rt[MAX],dep[MAX];
vector<int> vv[MAX],ans;
int getf(int v) {return f[v] == v ? v : f[v] = getf(f[v]);
}
void merge(int u,int v) {int t1 = getf(u),t2 = getf(v);f[t2] = t1;
}
int mx;
void dfs(int u,int fa) {dep[u] = dep[fa] + 1;mx = max(mx,dep[u]);int up = vv[u].size();for(int i = 0; i<up; i++) {int v = vv[u][i];if(v == fa) continue;dfs(v,u);}
}
int main()
{cin>>n;for(int i = 1; i<=n; i++) f[i] = i;for(int u,v,i = 1; i<=n-1; i++) cin>>u>>v,vv[u].pb(v),vv[v].pb(u),merge(u,v);int cnt = 0;for(int i = 1; i<=n; i++) {if(f[i] == i) cnt++;}if(cnt != 1) {printf("Error: %d components\n",cnt);return 0 ;}for(int root = 1; root<=n; root++) {mx = 0;dfs(root,0);rt[root] = mx;}mx = 0;for(int i = 1; i<=n; i++) {if(rt[i] > mx) {mx = rt[i];ans.clear();ans.pb(i);}else if(rt[i] == mx) ans.pb(i);} sort(ans.begin(),ans.end());for(auto x : ans) {printf("%d\n",x);}return 0 ;
}

【PAT - 甲级1021】Deepest Root (25分)(并查集,暴力枚举)相关推荐

  1. PAT甲级1021 Deepest Root :[C++题解]树的最大深度、并查集、dfs求树的深度

    文章目录 题目分析 题目链接 题目分析 分析: 考察知识点:并查集.dfs.树的深度 给定n个结点,n-1条边,只要能保证只有1个连通分量,就是一棵树.否则的话就不是树,它是不连通的. 用并查集来看是 ...

  2. PAT甲级-1021 Deepest Root(25分)

    题目:1021 Deepest Root(25分) 分析:找出以某个节点为根时,最深的根,这题可能会超时要用vector来表示二维数组 疑问:代码一是第二次写的超时了,代码二是第一次写的AC了,找不出 ...

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

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

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

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

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

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

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

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

  7. 1021. Deepest Root (25)

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

  8. 【PAT - 甲级1003】Emergency (25分)(Dijkstra,最短路条数,双权值最短路)

    题干: As an emergency rescue team leader of a city, you are given a special map of your country. The m ...

  9. 【PAT甲级A1003 】Emergency (25分)(c++)

    1003 Emergency (25分) 作者:CHEN, Yue 单位:浙江大学 代码长度限制:16 KB 时间限制:400 ms 内存限制:64 MB As an emergency rescue ...

最新文章

  1. boost Release 1.47.0
  2. 利用最大熵进行阈值分割从而实现灰度图像的二值化的原理概要及OpenCV代码
  3. keil for 51 汉字显示问题
  4. 行号 设置vim_Vim从小白到入门
  5. 【算法】算法秋招个人总结
  6. lombok pom.xml依赖
  7. HBase 默认配置项详细解读
  8. 牛逼站是怎样炼成的?-推荐系统篇
  9. 天池 在线编程 LR String
  10. php 语法验证_PHP用户登录验证模块
  11. hive join on 条件 与 where 条件区别
  12. Java基础篇:为Box类添加一个方法
  13. 详解2021华为笔试三道编程题
  14. SAP笔记-abap SD 定价公式(例程,即Formula)
  15. 领导邀请一起跳槽?搞清楚这5件事再决定
  16. 美国黄岩超级计算机,飓风预测 揭秘最快气候研究“黄石”超算
  17. 微信公众平台测试账号本地配置
  18. 01旭锋集团运营平台v2项目概述
  19. 学完高性能计算后的发展怎么样?
  20. MOS管驱动电路设计,如何让MOS管快速开启和关闭?

热门文章

  1. Oracle中用rownum替代Top函数的方法
  2. Dos下命令运行带有包名的Java类
  3. 翻译记忆软件:Trados 7/2006,兼容性和基本用法讨论
  4. [Java]==和equals()的区别(按照数据类型区分)
  5. [Leedcode][第215题][JAVA][数组中的第K个最大元素][快排][优先队列]
  6. [剑指offer][JAVA]面试题第[27]题[二叉树的镜像][递归][栈]
  7. android p 权限控制,android 权限控制
  8. windows c语言 redis,windows上使用VS2012 C++语言调用Redis的解决方案
  9. php全局cors,PHP开启CORS - slagga的个人页面 - OSCHINA - 中文开源技术交流社区
  10. matlab cuda的.cu文件应该放在那里_无人机基于Matlab/Simulink的模型开发(连载一)