立志用最少的代码做最高效的表达


PAT甲级最优题解——>传送门


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


题意:给出N,表示N个点,接下来有N-1对数,表示某两个点间有连接。判断是否为无环图, 如果是无环图,则将它看做一棵树,求在哪个节点为根节点的情况下,树最深。 如有并列,则升序输出根节点。


本题是一道细节题。

如果一幅图是一个无环连通图,则该图能构成一棵树。所以要判断给出的图是否是无环连通图,当然这道题降低了难度,因为题目指定了图中有N个节点,且只有N-1条边,那么如果这幅图是连通的,则必然无环;如果这幅图不连通,则必然有环。

因此,只需判断连通分量(连通块)是否大于1,就可知是否Error。 下面贴出代码,请读者自行体会。


#include<bits/stdc++.h>
using namespace std;const int INF = 1e4+5;    //结点的最大数量
vector<vector<int>>graph(INF);
bool visit[INF];
int maxlevel[INF], level[INF];  //最大深度、当前深度void DFS(int v, int start) { //深度优先遍历 visit[v] = true;for(int i : graph[v])if(!visit[i]) {level[i] = level[v] + 1;    //当前节点深度为父节点深度+1maxlevel[start] = max(maxlevel[start], level[i]);DFS(i, start); }
} int main() {int n; cin >> n;for(int i = 1; i < n; i++) {int a, b; cin >> a >> b;graph[a].push_back(b);graph[b].push_back(a);}int num = 0;        //记录连通分量数量for(int i = 1; i <= n; i++) { //深搜求连通分量数量 if(!visit[i]) {++num;      //每进行一次遍历连通分量数+1DFS(i, i); } } if(num > 1) printf("Error: %d components\n", num);else {for(int i = 1; i <= n; i++) {memset(level, 0, sizeof(level));   //将level数组元素置为0memset(visit, false, sizeof(visit)); //将visit数组元素置为falseDFS(i, i); } vector<int>v; //存储结果int deepLevel = 0;for(int i = 1; i <= n; i++) if(maxlevel[i] > deepLevel) {v.clear();v.push_back(i);deepLevel = maxlevel[i];} else if(maxlevel[i] == deepLevel)v.push_back(i);for(int i = 0; i < v.size(); i++) printf("%d\n", v[i]);} return 0;
}

耗时:

【分析】1021 Deepest Root (25 分)【DFS解法】相关推荐

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

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

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

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

  3. 1021. Deepest Root (25)

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

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

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

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

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

  6. PTA Deepest Root (25分)

    释放无限光明的是人心,制造无边黑暗的也是人心,光明和黑暗交织着,厮杀着,这就是我们为之眷恋又万般无奈的人世间. A graph which is connected and acyclic can b ...

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

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

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

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

  9. 7-121 深入虎穴 (25 分)(dfs,bfs)

    7-121 深入虎穴 (25 分) 著名的王牌间谍 007 需要执行一次任务,获取敌方的机密情报.已知情报藏在一个地下迷宫里,迷宫只有一个入口,里面有很多条通路,每条路通向一扇门.每一扇门背后或者是一 ...

最新文章

  1. 诺基亚没放弃 它买下这家公司,要成为电信界的扛把子
  2. emwin生成c文件格式的汉字库,GB2312编码,模拟器可用
  3. 自动化测试之alert弹窗的切换
  4. 计算机硬件常见问题及修复,pc机输入输出设备的常见故障现象及故障修复方法...
  5. TCP/IP / 如何进行堵塞控制?
  6. java中 4 种取整函数
  7. WebSocket笔记(一) 初步认识
  8. Xamarin.Forms 简介
  9. 局域网的主机如何连接外网
  10. 【C语言视频教程完整版】从入门到进阶,适合C语言初学者计算机考研党考计算机二级大一大二学生学习观看~~~
  11. cpu性能测试那个软件准,cpu对比测试软件 CPU性能测试软件推荐
  12. raspberry pi 4检查ch340/ch341驱动
  13. NX拉伸实体实例 UF_MODL_create_extruded
  14. 【072】Breathe Machine-减压深呼吸训练
  15. 快速保存网页中所有图片的方法
  16. 如何使用阿里云接口对系统用户【身份证】实名认证
  17. opencv python:使用cv2.waitKey(1)控制多个按键
  18. 清分系统层即综合中央计算机,轨道交通AFC系统介绍
  19. Android编译系统apk并进行系统签名安装
  20. 视频同步笔记:狂神聊Git

热门文章

  1. git 撤销文件的修改(checkout/reset )
  2. 关于JUnit5 你必须知道的(二)JUnit 5的新特性
  3. Kylin、Druid、ClickHouse核心技术对比
  4. 好文推荐 | etcd 问题、调优、监控
  5. 使用 litmus 验证内存重排
  6. 拍乐云基于AV1的实时视频系统技术实践
  7. 熊猫直播P2P分享率优化(下):ASN组网
  8. 梁俊斌:音频技术可以延展众多应用场景
  9. 多长的企业视频最受欢迎?小于60秒
  10. 熊猫TV直播H5播放器架构探索