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

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

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

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

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

#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
int n, maxheight = ;
vector<vector<int>> v;
bool visit[];
set<int> s;
vector<int> temp;
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);
}
visit[node] = true;//一般图访问点完了之后,都要标记的,防止其下一个点再返回去访问它。
for(int i = ; i < v[node].size(); i++) {
if(visit[v[node][i]] == false)
dfs(v[node][i], height + );
}
}
int main() {
scanf("%d", &n);
v.resize(n + );
int a, b, cnt = , s1 = ;
for(int i = ; i < n - ; i++) {
scanf("%d%d", &a, &b);
v[a].push_back(b);//使用二维向量来存储。
v[b].push_back(a);
}
for(int i = ; i <= n; i++) {
if(visit[i] == false) {
dfs(i, );
if(i == ) {//其实这里随便一个点均可。
if (temp.size() != ) s1 = temp[];
for(int j = ; j < temp.size(); j++)
s.insert(temp[j]);//再使用集合存储,防止重复。
}
cnt++;
}
}
if(cnt >= ) {
printf("Error: %d components", cnt);
} else {
temp.clear();
maxheight = ;
fill(visit, visit + , false);
dfs(s1, );
for(int i = ; i < temp.size(); i++)
s.insert(temp[i]);
for(auto it = s.begin(); it != s.end(); it++)
printf("%d\n", *it);
}
return ;
}

//厉害,使用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. 国内首款 FPGA 云服务器,性能是通用 CPU 服务器 30 倍以上

    版权声明:本文由薛梁原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/628340001485134638 来源:腾云阁 ht ...

  2. 小程序判断是否授权源码 auth.js

    一.auth.js const configGlobal = require('../config/config_global.js'); var util = require('function.j ...

  3. jQuery弹出层插件大全

    1.thickbox 目前用的比较多的,最新版本是thickbox3.1 下载地址:http://jquery.com/demo/thickbox/#examples 2.colorBox 官方网站: ...

  4. java (10) 集合类

    1.集合概述 集合按照存储结构可以分为两类,即单列集合 Collection 和双列集合 Map. * Collection 用于存储一系列符合某种规则的元素,它有两个重要的自接口,分别是List和S ...

  5. 九度OJ小结

    1. 高精度问题 可参考题目 题目1137:浮点数加法   http://ac.jobdu.com/problem.php?pid=1137 对于高精度问题可以考虑使用结构体.上述为浮点数加法,因此该 ...

  6. LeetCode 31 Next Permutation(下一个全排列)

    题目链接: https://leetcode.com/problems/next-permutation/?tab=Description   Problem :寻找给定int数组的下一个全排列(要求 ...

  7. 第二步 使用Cordova 3.0(及以上版本) 创建安卓项目(2014-6-25)

    参考资料: http://www.cnblogs.com/numtech/p/3233469.html http://blog.sina.com.cn/s/blog_9e245c690101jurr. ...

  8. openstack-networking-neutron(一)---端到端和点到点的理解

    本博客已经添加"打赏"功能,"打赏"位置位于右边栏红色框中,感谢您赞助的咖啡. ====本文目的===== 理解搞清楚两个概念: 1.端到端    2.点到点  ...

  9. jenkins或ansible启动应用不成功日志又不报错

    碰到ansible无法起停tomcat的时候,有3个点需要关注 1.环境变量,在startup.sh中添加source /etc/profile 2.后台运行,加上nohup...& 3.单独 ...

  10. 关于hp proliant sl210t服务器raid 1阵列配置

    hp proliant sl210t服务器,一般都会带有两个阵列卡 一个服务器自带的Dynamic Smart Array B120i RAID控制器,一个为Slot卡槽上的Smart Array P ...