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. BZOJ 1016 Windy 数 | 数位DP

    题目: http://www.lydsy.com/JudgeOnline/problem.php?id=1026 题解: f[i][j][1/0]表示枚举到第i位,这位开头是j,当前的数大于(1)或小 ...

  2. [Leetcode] valid palindrome 验证回文

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  3. 洛谷 P2617 Dynamic Rankings 解题报告

    P2617 Dynamic Rankings 题目描述 给定一个含有\(n\)个数的序列\(a[1],a[2],a[3],\dots,a[n]\),程序必须回答这样的询问:对于给定的\(i,j,k\) ...

  4. 浅谈Hibernate框架(一)——.hbm.xml中的配置

    Hibernate一枚“全自动”的ORM框架: 用IDE工具集成Hibernate会自动生成: 以.hbm.xml为后缀结尾的配置文件+ POJO类 + Dao类 主键查询: Session.load ...

  5. 使用Android Studio调试UiAutomator过程中遇到的问题

    声明: 这里纪录了个人学习和使用Android Studio调试UiAutomator过程中遇到遇到的问题,不定时进行更新,欢迎一起交流学习 1.Excution faild for task ‘:a ...

  6. web开发环境和要求配置

    对于eclipse,有很多版本,但要开发WEB程序,需要用到j2ee版本,如果是winform或android 用不带ee的版本就行,两者的明显区别是在看帮助->关于->Eclipse J ...

  7. Ubuntu修改系统语言为英文可支持中文

    简单来说,就行修改/etc/default/locale文件,设置语言位UTF-8,如果没有这个语言,就执行命令locale-gen en_US.UTF-8进行安装,没有即时生效的话就重启. 查看当前 ...

  8. MFC中用正则表达式进行有效性验证

    转载自:http://blog.csdn.net/jinhill/article/details/5928993 正则表达式最实用的一个地方是验证用户输入.它可以轻松验证邮编.电话号码.信用卡号码-- ...

  9. python 面试题(1)

    好用简洁的大数据技术:python.hadoop.R 慢慢学习,随时分享 1.什么是Python?使用Python有什么好处? Python是一种编程语言,它有对象.模块.线程.异常处理和自动内存管理 ...

  10. asyncio结合线程池

    #使用多线程:在协程中集成阻塞io import asyncio from concurrent.futures import ThreadPoolExecutor import socket fro ...