PAT 1021 Deepest Root[并查集、dfs][难]
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][难]的更多相关文章
- 1021.Deepest Root (并查集+DFS树的深度)
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
- PAT 甲级 1021 Deepest Root (并查集,树的遍历)
1021. Deepest Root (25) 时间限制 1500 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A graph ...
- [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 ...
- PAT 1021 Deepest Root
#include <cstdio> #include <cstdlib> #include <vector> using namespace std; class ...
- PAT甲级1021. Deepest Root
PAT甲级1021. Deepest Root 题意: 连接和非循环的图可以被认为是一棵树.树的高度取决于所选的根.现在你应该找到导致最高树的根.这样的根称为最深根. 输入规格: 每个输入文件包含一个 ...
- 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 ...
- PAT甲题题解-1021. Deepest Root (25)-dfs+并查集
dfs求最大层数并查集求连通个数 #include <iostream> #include <cstdio> #include <algorithm> #inclu ...
- 1021. Deepest Root (25)——DFS+并查集
http://pat.zju.edu.cn/contests/pat-a-practise/1021 无环连通图也可以视为一棵树,选定图中任意一点作为根,如果这时候整个树的深度最大,则称其为 deep ...
- 1021. Deepest Root (25) -并查集判树 -BFS求深度
题目如下: A graph which is connected and acyclic can be considered a tree. The height of the tree depend ...
随机推荐
- JS函数匿名替换
//匿名替换函数 function objFunc() { var obj = new Object(); obj.JsonData = [{ aa: "}], obj.FilterData ...
- ACER-4738ZG 拆机改散热
前言 武汉真是个很热的地方,我的笔记本于2011年3月份左右购买的,到现在已经两年多了,第一个暑假,我是在苏州的空调房使用,第二个暑假,我是在实验室的空调房使用,没有直接感受到夏天对笔记本的杀伤力,今 ...
- IOS设计模式第九篇之备忘录模式
版权声明:原创作品,谢绝转载!否则将追究法律责任. 备忘录模式捕获和具体化对象的内部状态.换句话说,它可以节省你的东西后来,这种外部状态可以恢复在不违反封装; 也就是说,私人数据是私有的. 怎么用备忘 ...
- Atom使用插件精选
小颖之前公司的大哥推荐小颖用的编辑器是atom,之前都是他给小颖了一个atom插件安装列表,小颖电脑出了点问题,所以后来小颖把那弄丢了,小颖重装atom后,就不知道要安装什么插件,所以也百度了很多,今 ...
- php 加密压缩
php 把文件打成压缩包 ,可以去搜下 pclzip 搜很好多地方没有找到对压缩包进行加密操作的. 如果服务器是linux 那么见代码: $filename="test.csv"; ...
- STS没有找到Dynamic Web Project
解决:安装JavaEE插件 help-> install new software-> 选择sts对应的eclipse版本站点,如eclipse版本4.09选择2018-09.4.10选择 ...
- 解决ios safari中按钮圆角问题【原创】
问题描述 使用html5编写页面在移动app中嵌套,总会涉及到按钮的使用,在android手机浏览器中显示正常,但在ios safari浏览器中会看到按钮显示为圆角样式,设置border-rad ...
- 170815、redis3.0安装配置
下载地址http://redis.io/download 安装步骤: 1 首先需要安装gcc,把下载好的redis-3.0.0-rc2.tar.gz 放到linux /usr/local文件夹下 2 ...
- TFS二次开发系列索引
TFS二次开发11——标签(Label) TFS二次开发10——分组(Group)和成员(Member) TFS二次开发09——查看文件历史(QueryHistory) TFS二次开发08——分支(B ...
- IIS7.5 配置虚拟目录的经历
好多网站为了不带上什么端口号所有就建立虚拟目录的试来使用80端口 iis6设置都没有问题可到了7.5碰到了点问题 原来7.5上有个:添加应用程序和添加虚拟目录.还有个转换为应用程序 直接把网站建成虚拟 ...