[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 ...
随机推荐
- 洛谷 P2747 [USACO5.4]周游加拿大Canada Tour 解题报告
P2747 [USACO5.4]周游加拿大Canada Tour 题目描述 你赢得了一场航空公司举办的比赛,奖品是一张加拿大环游机票.旅行在这家航空公司开放的最西边的城市开始,然后一直自西向东旅行,直 ...
- HDOJ(HDU).1035 Robot Motion (DFS)
HDOJ(HDU).1035 Robot Motion [从零开始DFS(4)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DF ...
- BZOJ2791 Rendezvous
Description给定一个n个顶点的有向图,每个顶点有且仅有一条出边.对于顶点i,记它的出边为(i, a[i]).再给出q组询问,每组询问由两个顶点a.b组成,要求输出满足下面条件的x.y:1. ...
- RGB向yuv的转化最优算法,快得让你吃惊!
朋友曾经给我推荐了一个有关代码优化的pdf文档<让你的软件飞起来>,看完之后,感受颇深.为了推广其,同时也为了自己加深印象,故将其总结为word文档.下面就是其的详细内容总结,希望能于己于 ...
- selenium - webdriver - ActionChains类(鼠标操作)
ActionChains 类提供了鼠标操作的常用方法: perform(): 执行所有 ActionChains 中存储的行为: context_click(): 右击: double_click() ...
- 关于AVPlayerItem对象的属性duration返回播放总时长的坑
最近在使用AVPlayer播放网络流媒体,发现一个坑: 就是playerItem.duration有可能不返回该网络多媒体资源的播放总时间长度,而是返回了一个奇怪的数据:nan, 因为我通过CMTim ...
- HDU1384 差分约束
Intervals Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- a标签nest问题,即a标签里面嵌套a标签
方法一:使用div模拟a,监听click事件 方法二:使用<object>标签包裹内部a标签 <div style="width: 200px;height: 200px; ...
- 使用 html2canvas 实现浏览器截图
基于上一篇<h5 本地上传图片预览 源码下载>,今天分享一个图片上传后, 根据所上传的图片颜值随机生成一个答案, 并且可以生成一张专属于自己的名片. 首先上传预览我们已经实现了, 所以接下 ...
- 浅谈移动端三大viewport
我们通常在写移动端页面时,往往都会在html页面中加入这样一段话 <meta name="viewport" content="width=device-width ...