[PAT] 1021 Deepest Root (25)(25 分)
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 分)的更多相关文章
- PAT 1021 Deepest Root[并查集、dfs][难]
1021 Deepest Root (25)(25 分) A graph which is connected and acyclic can be considered a tree. The he ...
- 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 ...
- PAT 1021 Deepest Root
#include <cstdio> #include <cstdlib> #include <vector> using namespace std; class ...
- 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 (并查集,树的遍历)
1021. Deepest Root (25) 时间限制 1500 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A graph ...
- PAT甲级1021. Deepest Root
PAT甲级1021. Deepest Root 题意: 连接和非循环的图可以被认为是一棵树.树的高度取决于所选的根.现在你应该找到导致最高树的根.这样的根称为最深根. 输入规格: 每个输入文件包含一个 ...
- 1021 Deepest Root (25 分)
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
- 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 ...
随机推荐
- BZOJ4144 [AMPPZ2014]Petrol 【最短路 + 最小生成树】
题目链接 BZOJ4144 题解 这题好妙啊,,orz 假设我们在一个非加油站点,那么我们一定是从加油站过来的,我们剩余的油至少要减去这段距离 如果我们在一个非加油站点,如果我们到达不了任意加油站点, ...
- 用ByteArrayOutputStream解决IO流乱码问题
IO中用ByteArrayOutputStream解决乱码问题 --另一种解决乱码的方法 IO中另外一种防止乱码的方法:使用ByteArrayOutputStream在创建ByteArrayOutpu ...
- group by多字段分组
在平时的开发任务中我们经常会用到MYSQL的GROUP BY分组, 用来获取数据表中以分组字段为依据的统计数据.比如有一个学生选课表,表结构如下: Table: Subject_Selection S ...
- array_pop 剔除最后一个数组元素
<?php $a=array("red","green","blue"); print_r(array_pop($a)); //blu ...
- Hexo&Github-Pages搭建个人博客
some基础知识 hexo hexo是一款基于Node.js的静态博客框架 github-pages说明 github有两种主页,一种是github-page(个人主页),一种是项目主页,本教程针对个 ...
- 数据结构:二维ST表
POJ2019 我们其实是很有必要把ST算法拓展到二维的,因为二维的RMQ问题还是不少的 int N,B,K; ]; int val[maxn][maxn]; ][]; ][]; 这里的N是方阵的长宽 ...
- HDU 5533Dancing Stars on Me 基础几何
Problem Description The sky was brushed clean by the wind and the stars were cold in a black sky. Wh ...
- Codeforces Round #380 (Div. 2)/729E Subordinates 贪心
There are n workers in a company, each of them has a unique id from 1 to n. Exaclty one of them is a ...
- 通过java客户端连接hbase 注意事项
1.通过Java客户端连接Hbase,其中hbase通过zookeeper去管理,需要注意的是客户端端口. 通过在浏览器端输入地址查看:http://192.168.3.206:60010/maste ...
- base--AuditObject
//参考base-4.0.2.jarpublic class AuditObject extends HashMap<String, Object> implements TimeRefe ...