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:

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个节点(1~n),并给出n-1个边,求图的连通分量。如果连通分量为1,则求图对应的树的最大深度对应的root,如果不唯一则按升序排列输出。

思路:
柳婼 の blog
1.先通过DFS判断图的连通分量个数。
2.任取一个顶点,DFS求得最高高度的结点们,然后从中任取一个结点,再做一个DFS求得最高高度的结点们,做一个并集得到最终结果。

题解:

 #include<cstdio>
 #include<vector>
 #include<set>
 using namespace std;
 vector<vector<int>> mp;
 ];
 ;
 vector<int> temp;
 set<int> s;
 void dfs(int node, int height) {
     if (height > maxHeight) {
         temp.clear();
         temp.push_back(node);
         maxHeight = height;
     }
     else if (height == maxHeight) {
         temp.push_back(node);
     }
     isVisit[node] = true;
     ; i < mp[node].size(); i++) {
         if (isVisit[mp[node][i]] == false) {
             dfs(mp[node][i], height + );
         }
     }
 }
 int main() {
     int n;
     scanf("%d", &n);
     mp.resize(n + );
     int a, b;
     ; i < n; i++) {
         scanf("%d %d", &a, &b);
         mp[a].push_back(b);
         mp[b].push_back(a);
     }
     ;
     int s1;
     ; i <= n; i++) {
         if (isVisit[i] == false) {
             dfs(i, );
             //假设cnt为1,将temp中的值放入s中。
             //如果cnt>1,这些值虽然不对,但也没用上。
             ) {
                 ) s1 = temp[];
                 ; j < temp.size(); j++) {
                     s.insert(temp[j]);
                 }
             }
             cnt++;
         }
     }
     ) {
         printf("Error: %d components\n", cnt);
     }
     else {
         temp.clear();
         fill(isVisit, isVisit + , false);
         dfs(s1, );
         ; i < temp.size(); i++) {
             s.insert(temp[i]);
         }
         for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
             printf("%d\n", *it);
         }
     }
     ;
 }

[PAT] 1021 Deepest Root (25)(25 分)的更多相关文章

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

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

  2. PAT A1021 Deepest Root (25 分)——图的BFS,DFS

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

  3. PAT 1021 Deepest Root

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

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

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

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

  6. PAT甲级1021. Deepest Root

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

  7. 1021 Deepest Root (25 分)

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

  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. [Leetcode] Binary tree postorder traversal二叉树后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...

  2. 洛谷 P3644 [APIO2015]八邻旁之桥 解题报告

    P3644 [APIO2015]八邻旁之桥 题目描述 一条东西走向的穆西河将巴邻旁市一分为二,分割成了区域\(A\)和区域\(B\). 每一块区域沿着河岸都建了恰好\(1000000001\)栋的建筑 ...

  3. YBT 1.1 贪心算法

    本人因为过于懒所以以后就将题解放进原文件中,存入百度网盘,自行下载,里面包含题目网站,源文件,与相应题解(这次没有写) 链接: https://pan.baidu.com/s/1eSoQ_LFWMxF ...

  4. 如何将javascript对象转换成字符串

    将后台程序(如php)发送过来的json数据转化为javascript的数组或者对象的方法十分简单,代码如下: 1 // 假设后台发送的json数据为 '{a:2,b:1}' 存储于str中 2 va ...

  5. 基于MeanShift的目标跟踪算法及实现

    这次将介绍基于MeanShift的目标跟踪算法,首先谈谈简介,然后给出算法实现流程,最后实现了一个单目标跟踪的MeanShift算法[matlab/c两个版本] csdn贴公式比较烦,原谅我直接截图了 ...

  6. iostat和iowait详细解说

    简单的说,sar -u看出来的cpu利用率iowait 不实用,iostat -x 中的 svctm   和util 参数 命令形式: iostat -x 1 每隔一秒输出下 其中的svctm参数代表 ...

  7. SpringMVC 用注解Annotation驱动的IoC功能@Autowired @Component

    转载自:http://blog.csdn.net/lufeng20/article/details/7598564 本文分为三个部分:概述.使用注解进行属性注入.使用注解进行Bean的自动定义. 一, ...

  8. RabbitMQ与Spring集成

    RabbitMQ服务端安装: https://blog.csdn.net/hzw19920329/article/details/53156015 与Spring集成 https://www.cnbl ...

  9. Mysql优化小记1

    在项目开发中,需要写个windows服务从sqlserver复制数据到mysql(5.6.13 Win64(x86_64)),然后对这些数据进行计算分析.每15分钟复制一次,每次复制大概200条数据, ...

  10. 【BZOJ4864】神秘物质 [Splay]

    神秘物质 Time Limit: 10 Sec  Memory Limit: 256 MB Description Input Output Sample Input Sample Output 1 ...