题目链接:http://www.patest.cn/contests/pat-a-practise/1021

题目:

1021. Deepest Root (25)

时间限制
1500 ms

内存限制
65536 kB

代码长度限制
16000 B

判题程序
Standard

作者
CHEN, Yue

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 (<=10000) 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

分析:

用并查集来考察能否构成一棵树,
找出最长树的深度基于这种一个事实:
从随意一个节点S0出发,找到最大的深度H0和其相应的叶子节点D0。然后再从D0出发(当作S1)找到最深的长度H1,假设和H0相等则其就是最大的H,否则继续找,直到两次找到的H相等。

当中,找最大深度函数find_height()。我是用类似层序遍历去做的,先把根节点和-1(一个标志位)放入队列中。然后每次取对头,假设是元素的话。则把其子节点都放入队列。假设是-1的话。则把深度++,而且再把-1放入,相当于-1变成了每层的结尾的标志。

当然,求树最大深度能够用递归非常easy做出来,这里仅仅是用别的方法做个拓展(事实上这代码曾经写的,当时瞎想就想到这个)。

AC代码:

#include<stdio.h>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
vector<int>V[10001];
queue<int>Q;
int Tree[10001];
int ans[10001];
int findRoot(int x){if (Tree[x] == -1)return x;else {int tmp = findRoot(Tree[x]);Tree[x] = tmp;return tmp;}
}
bool mark[10001];
int tail;
int n;
int find_height(int x){//找到最大深度的函数mark[x] = true;int height_tmp = 0;Q.push(x); Q.push(-1);while (!Q.empty()){if (Q.front() == -1){height_tmp++;Q.pop();if (Q.empty())break;else Q.push(-1);}int front = Q.front();tail = front;for (int i = 0; i < V[front].size(); i++){if (!mark[V[front][i]])Q.push(V[front][i]);mark[V[front][i]] = true;}Q.pop();}for (int i = 1; i <= n; i++){mark[i] = false;}return height_tmp;
}
int main(void){//freopen("F://Temp/input.txt", "r", stdin);while (scanf("%d", &n) != EOF){for (int i = 1; i <= n; i++){ //initTree[i] = -1;ans[i] = 0;mark[i] = false;}if (!Q.empty())Q.pop();int a, b;for (int i = 0; i < n - 1; i++){//并查集部分scanf("%d%d", &a, &b);V[a].push_back(b);V[b].push_back(a);a = findRoot(a);b = findRoot(b);if (a != b) Tree[a] = b;}int com = 0;for (int i = 1; i <= n; i++){if (Tree[i] == -1)com++;}if (com > 1){//假设并查集找出超过两部分,则输出error...printf("Error: %d components", com);continue;}else{int h_max;int head;for (int i = 1; i <= n; i++){if (V[i].size() == 1){head = i;break;}}h_max = find_height(head);while (h_max != find_height(tail)){//假设找到的高度不同样。则继续找head = tail;h_max = find_height(head);}int j = 0;for (int i = 1; i <= n; i++){if (find_height(i) == h_max){ans[j++] = i;}}sort(ans, ans + j);for (int i = 0; i < j; i++){printf("%d\n", ans[i]);}}}return 0;
}

截图:

——Apie陈小旭

转载于:https://www.cnblogs.com/yutingliuyl/p/6721486.html

1021. Deepest Root (25)相关推荐

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

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

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

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

  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. PAT甲级-1021 Deepest Root(25分)

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

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

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

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

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

  8. PTA Deepest Root (25分)

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

  9. 1021 Deepest Root

    要解决两大问题: 1. 数包含几个连通分量 2. 如何找到最深结点 注意:connected components的意思是连通分量 问题1我用并查集解决 问题2转化为如何得到每个结点的深度 值得注意之 ...

最新文章

  1. c++primer 5th习题12.25答案
  2. 静态库和动态库(转)
  3. php aes 128位加密,php实现AES 128位加密的相关操作技巧分享
  4. JavaScript入门几个概念
  5. favicon.ico 404的问题(title栏前面的图标)
  6. c语言中的双周期指令,时钟周期 机器周期 指令周期的概念
  7. java 脚本怎么写_编写java的运行脚本
  8. 2021高考厦门科技中学成绩查询,2021年厦门重点高中名单及排名,厦门高中高考成绩排名榜...
  9. 2021中国华录杯·算法大赛直通车!
  10. 移动平均线rolling()与加权移动平均线ewm()
  11. 深入理解DirectX D3D9
  12. Handler sync barrier(同步屏障)
  13. 用java解一元二次方程组
  14. mac彩色球转不停,Mac电脑一直在转圈怎么办?
  15. 傻傻分不清楚?带你了解设备id
  16. notification源码分析_antd源码解读(10)- notification
  17. jspseverlet学习笔记
  18. 正则表达式--匹配简体繁体中文姓名,还有少数民族的·号
  19. 不同年龄段的孩子如何选择编程课程?它又能为我们带来什么?
  20. RDS SQL Server死锁(Deadlock)系列之五利用Extended Events获取死锁信息

热门文章

  1. 鸿蒙系统小卡片,升级鸿蒙101版本,UI审美升级了
  2. CRC循环校验码原理及计算举例
  3. 5G NR Search space和CORESET
  4. Eclipse下Tomcat常用设置
  5. sessionbean entitybean 区别
  6. java多线程学习-java.util.concurrent详解
  7. AIX 命令 more
  8. OSX 10.8+下开启Web 共享 的方法
  9. 2017-2018 20155309南皓芯 信息安全系统基础设计第十四周博客
  10. 循环语句练习题2(打印三角形和菱形)