https://pintia.cn/problem-sets/994805342720868352/problems/994805482919673856

A graph which is connected and acyclic can be considered a tree. The hight 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:

  1. 5
  2. 1 2
  3. 1 3
  4. 1 4
  5. 2 5

Sample Output 1:

  1. 3
  2. 4
  3. 5

Sample Input 2:

  1. 5
  2. 1 3
  3. 1 4
  4. 2 5
  5. 3 4

Sample Output 2:

  1. Error: 2 components

代码:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int maxn = 1e5 + 10;
  5. int N;
  6. vector<int> v[maxn];
  7. int vis[maxn], mp[maxn];
  8. int cnt = 0;
  9. int depth = INT_MIN;
  10. vector<int> ans;
  11.  
  12. void dfs(int st) {
  13. vis[st] = 1;
  14.  
  15. for(int i = 0; i < v[st].size(); i ++) {
  16. if(vis[v[st][i]] == 0)
  17. dfs(v[st][i]);
  18. }
  19. }
  20.  
  21. void helper(int st, int step) {
  22. if(step > depth) {
  23. ans.clear();
  24. ans.push_back(st);
  25. depth = step;
  26. } else if(step == depth) ans.push_back(st);
  27.  
  28. mp[st] = 1;
  29. for(int i = 0; i < v[st].size(); i ++) {
  30. if(mp[v[st][i]] == 0)
  31. helper(v[st][i], step + 1);
  32. }
  33. }
  34.  
  35. int main() {
  36. scanf("%d", &N);
  37. memset(vis, 0, sizeof(vis));
  38. for(int i = 0; i < N - 1; i ++) {
  39. int a, b;
  40. scanf("%d%d", &a, &b);
  41. v[a].push_back(b);
  42. v[b].push_back(a);
  43. }
  44.  
  45. for(int i = 1; i <= N; i ++) {
  46. if(vis[i] == 0) {
  47. dfs(i);
  48. cnt ++;
  49. }
  50. else continue;
  51. }
  52.  
  53. set<int> s;
  54. int beginn = 0;
  55. helper(1, 1);
  56. if(ans.size() != 0) beginn = ans[0];
  57. for(int i = 0; i < ans.size(); i ++)
  58. s.insert(ans[i]);
  59.  
  60. if(cnt >= 2)
  61. printf("Error: %d components\n", cnt);
  62. else {
  63. ans.clear();
  64. depth = INT_MIN;
  65. memset(mp, 0, sizeof(mp));
  66. helper(beginn, 1);
  67. for(int i = 0; i < ans.size(); i ++)
  68. s.insert(ans[i]);
  69.  
  70. for(set<int>::iterator it = s.begin(); it != s.end(); it ++)
  71. printf("%d\n", *it);
  72. }
  73. return 0;
  74. }

  第一个 dfs 搜索有多少个连通块 helper 来找树的直径的一个头 已知树的直径 树上任意一点到的最大距离的另一端一定是树的直径的一个端点  两次深搜

PAT 甲级 1021 Deepest Root的更多相关文章

  1. PAT甲级1021. Deepest Root

    PAT甲级1021. Deepest Root 题意: 连接和非循环的图可以被认为是一棵树.树的高度取决于所选的根.现在你应该找到导致最高树的根.这样的根称为最深根. 输入规格: 每个输入文件包含一个 ...

  2. PAT 甲级 1021 Deepest Root (并查集,树的遍历)

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

  3. 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 ...

  4. PAT甲级——A1021 Deepest Root

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

  5. PAT 1021 Deepest Root[并查集、dfs][难]

    1021 Deepest Root (25)(25 分) A graph which is connected and acyclic can be considered a tree. The he ...

  6. [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 ...

  7. PAT甲级:1066 Root of AVL Tree (25分)

    PAT甲级:1066 Root of AVL Tree (25分) 题干 An AVL tree is a self-balancing binary search tree. In an AVL t ...

  8. 1021. Deepest Root (25)——DFS+并查集

    http://pat.zju.edu.cn/contests/pat-a-practise/1021 无环连通图也可以视为一棵树,选定图中任意一点作为根,如果这时候整个树的深度最大,则称其为 deep ...

  9. 1021.Deepest Root (并查集+DFS树的深度)

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

随机推荐

  1. 不要以为字段以transient修饰的话就一定不会被序列化

    1: 先阅读这边文章:http://www.importnew.com/21517.html 2:被transient修饰真的会被序列化吗? 反例:java.util.ArrayList中底层存储数组 ...

  2. AES块加密与解密

    AES块加密与解密 解密目标 在CBC和CTR两种模式下分别给出十篇加密的样例密文,求解密一篇特定的密文 解密前提 全部密文及其加密使用的key都已给出 加密的方法遵循AES的标准 解密过程分析 实验 ...

  3. $Simpson$积分入门

    \(\rm{0x01}\) 前言 首先阐明一点,自适应辛普森算法(\(\rm{Adaptive ~Simpson's~ rule}\) )是一类近似算法(\(\rm{Approximation ~al ...

  4. Android处理ListView中的Item中的Button按钮不能点击的问题

    问题描述:ListView列表中的Button按钮按钮不能点击 解决办法:在ListView中的Item项的布局文件中加上:android:descendantFocusability="b ...

  5. day 26

    今日内容 classmethod 让这个类中的方法绑定自己类,这样就可以直接用类调用该方法. staticmethod 让类中的方法编程非绑定方法,也就是是这个类中的方法编程普通函数. ####### ...

  6. Oracle substr() instr() 用法

    转载:oracle中substr() instr() 用法 substr(字符串,截取开始位置,截取长度) = 返回截取的字符串instr(源字符串,目标字符串,起始字符串,匹配字符串) = 返回要截 ...

  7. 半导体热阻问题深度解析(Tc,Ta,Tj,Pc)

    半导体热阻问题深度解析(Tc,Ta,Tj,Pc) 本文是将我以前的<有关热阻问题>的文章重新梳理,按更严密的逻辑来讲解. 晶体管(或半导体)的热阻与温度.功耗之间的关系为: Ta=Tj-* ...

  8. Newtonsoft.Json.Linq对象读取DataSet数据

    Newtonsoft.Json.Linq对象读取DataSet数据: private void button4_Click(object sender, EventArgs e)        {   ...

  9. SVD(奇异值分解)小结

    注:奇异值分解在数据降维中有较多的应用,这里把它的原理简单总结一下,并且举一个图片压缩的例子,最后做一个简单的分析,希望能够给大家带来帮助. 1.特征值分解(EVD) 实对称矩阵 在理角奇异值分解之前 ...

  10. VS编程,WPF单独更改TextBlock中部分文字格式的一种方法

    原文:VS编程,WPF单独更改TextBlock中部分文字格式的一种方法 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_43307934/articl ...