1021 Deepest Root (25)(25 分)

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:

  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

题目大意:对于图如果其是联通无环的,那么就是树,找出最深的树的所有起点。

//不知道怎么找到所有的,找到两个可还行。

//使用并查集判断是否是树,然后呢?

代码来自:https://www.liuchuo.net/archives/2348

  1. #include <iostream>
  2. #include <vector>
  3. #include <set>
  4. #include <algorithm>
  5. using namespace std;
  6. int n, maxheight = ;
  7. vector<vector<int>> v;
  8. bool visit[];
  9. set<int> s;
  10. vector<int> temp;
  11. void dfs(int node, int height) {
  12. if(height > maxheight) {
  13. temp.clear();//只存放最深的。
  14. temp.push_back(node);
  15. maxheight = height;
  16. } else if(height == maxheight){
  17. temp.push_back(node);
  18. }
  19. visit[node] = true;//一般图访问点完了之后,都要标记的,防止其下一个点再返回去访问它。
  20. for(int i = ; i < v[node].size(); i++) {
  21. if(visit[v[node][i]] == false)
  22. dfs(v[node][i], height + );
  23. }
  24. }
  25. int main() {
  26. scanf("%d", &n);
  27. v.resize(n + );
  28. int a, b, cnt = , s1 = ;
  29. for(int i = ; i < n - ; i++) {
  30. scanf("%d%d", &a, &b);
  31. v[a].push_back(b);//使用二维向量来存储。
  32. v[b].push_back(a);
  33. }
  34. for(int i = ; i <= n; i++) {
  35. if(visit[i] == false) {
  36. dfs(i, );
  37. if(i == ) {//其实这里随便一个点均可。
  38. if (temp.size() != ) s1 = temp[];
  39. for(int j = ; j < temp.size(); j++)
  40. s.insert(temp[j]);//再使用集合存储,防止重复。
  41. }
  42. cnt++;
  43. }
  44. }
  45. if(cnt >= ) {
  46. printf("Error: %d components", cnt);
  47. } else {
  48. temp.clear();
  49. maxheight = ;
  50. fill(visit, visit + , false);
  51. dfs(s1, );
  52. for(int i = ; i < temp.size(); i++)
  53. s.insert(temp[i]);
  54. for(auto it = s.begin(); it != s.end(); it++)
  55. printf("%d\n", *it);
  56. }
  57. return ;
  58. }

//厉害,使用dfs,传递层数参数,并且有一个maxHeight来保存最大的,厉害。

PAT 1021 Deepest Root[并查集、dfs][难]的更多相关文章

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

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

  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)(25 分)

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

  4. PAT 1021 Deepest Root

    #include <cstdio> #include <cstdlib> #include <vector> using namespace std; class ...

  5. PAT甲级1021. Deepest Root

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

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

  7. PAT甲题题解-1021. Deepest Root (25)-dfs+并查集

    dfs求最大层数并查集求连通个数 #include <iostream> #include <cstdio> #include <algorithm> #inclu ...

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

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

  9. 1021. Deepest Root (25) -并查集判树 -BFS求深度

    题目如下: A graph which is connected and acyclic can be considered a tree. The height of the tree depend ...

随机推荐

  1. sencha touch 在安卓中横屏、竖屏切换 应用崩溃问题

    答案来至于 Sencha Touch 交流 @周旭 这是由于横竖屏转换导致activity重跑onCreate方法导致的,有两种解决方案:1.横竖屏转换的时候不要重新跑onCreate方法,这个可以在 ...

  2. ELK系列三:Elasticsearch的简单使用和配置文件简介

    1.定义模板创建索引: 首先定义好一个模板的例子 { "order":14, "template":"ids-1", "state ...

  3. for,for-each,for-in,for-of,map的比较

    参考: 全面解析JavaScript里的循环方法之forEach,for-in,for-of Iterator 和 for...of 循环 JavaScript Array 对象 常规for for循 ...

  4. C# 未能加载文件或程序集“mysql.data”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)

    报错信息: 在web.config中已经加了以下代码. <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-co ...

  5. PCB常见的拓扑结构 (转)

    常见的拓扑结构有: 1.点对点拓扑 point-to-point scheduling     该拓扑结构简单,整个网络的阻抗特性容易控制,时序关系也容易控制,常见于高速双向传输信号线:常在源端加串行 ...

  6. Memcached概念、作用、运行原理、特性、不足简单梳理(1)

    大家可能对memcached这种产品早有了解,或者已经应用在自己的网站中了,但是也有一些朋友从来都没有听说过或者使用过.这都没什么关系,本文旨在从各个角度综合的介绍这种产品,尽量深入浅出,如果能对您现 ...

  7. dhroid - eventbus 事件总线

    你听过onClick 事件,onItemClick 事件,事件总线不一定听过吧, eventbus 事件总线也是一个编程思想,为什么要设计EventBus了,因为他是领域驱动设计中比不可少的模块,它承 ...

  8. Unity性能优化之Draw Call(转)

    Unity(或者说基本所有图形引擎)生成一帧画面的处理过程大致可以这样简化描述:引擎首先经过简单的可见性测试,确定摄像机可以看到的物体,然后把这些物体的顶点(包括本地位置.法线.UV等),索引(顶点如 ...

  9. Spark2 broadcast广播变量

    A broadcast variable. Broadcast variables allow the programmer to keep a read-only variable cached o ...

  10. windows乱码

    对于支持 UNICODE的应用程序,Windows 会默认使用 Unicode编码.对于不支持Unicode的应用程序Windows 会采用 ANSI编码 (也就是各个国家自己制定的标准编码方式,如对 ...