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. Jsp电子商务之七 订单篇2

    从View页面,点击超链接查询订单,进入到控制器 OrderlistServlet package com.cart.web; import java.io.IOException; import j ...

  2. 【初级算法】6. 两个数组的交集 II

    题目如下: 给定两个数组,写一个方法来计算它们的交集. 例如: 给定 nums1 = [, , , ], nums2 = [, ], 返回 [, ]. 注意: 输出结果中每个元素出现的次数,应与元素在 ...

  3. JavaScript SandBox沙箱设计模式

    沙箱模式常见于YUI3 core,它是一种采用同一构造器(Constructor)生成彼此独立且互不干扰(self-contained)的实例对象,而从避免污染全局对象的方法. 命名空间 JavaSc ...

  4. The Shortest Path in Nya Graph HDU - 4725

    Problem Description This is a very easy problem, your task is just calculate el camino mas corto en ...

  5. [samba]samba设置指定用户权限

    步骤: 1.在系统中添加用户 批量添加用户和密码的方法(因为samba用户要求必须在系统中存在): for name in a b c d;do useradd $name ; echo " ...

  6. 【转载】Innodb共享表空间VS独立表空间

    http://www.mysqlsupport.cn/innodb%E5%85%B1%E4%BA%AB%E8%A1%A8%E7%A9%BA%E9%97%B4vs%E7%8B%AC%E7%AB%8B%E ...

  7. 分析一个贴图社交app的失败原因:FORK(相机)

    FORK(相机)是一个通过分享图片来建立社交的app,它有着鲜明的配色,还算不错的贴图创新,细腻的产品设计,但是由于产品定位不清晰.设计亮点不多以及推广不利,从2014年5月第一版开始就没有火过.所以 ...

  8. intellij idea 破解补丁激活

    一.说明 idea激活可以用JetBrains account,Activation Code注册码或者填License server网址,使用注册码的方式可以参考lanyun提供的注册码,但是有效时 ...

  9. C11简洁之道:模板改进

    1.  右尖括号 我们在C++98/03中使用泛型编程的时候,经常遇到“>>”被当作右移操作符,而不是模板参数的结尾.假如我们有如下代码: template <typename T& ...

  10. jQuery简单的Ajax调用

    index.php 的代码如下: <!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"& ...