[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 ...
随机推荐
- Windows系统中Xshell与Linux连接时遇到的问题
前提条件:在Windows系统中已经安装了Xshell,并且安装了虚拟机软件和Linux系统 步骤1.在Linux系统中root用户下,使用ifconfig命令查看虚拟系统Linux的IP地址.如图1 ...
- 实验三 Java敏捷开发与XP实践
北京电子科技学院(BESTI) 实 验 报 告 课程:Java程序设计 班级:1353 姓名:陈巧然 ...
- ES6 Set,WeakSet,Map,WeakMap
1. Set Set是一个集合,里面的值都是唯一的,没有重复的.Set中可以是任何数据类型,并且添加数据时会进行严格比较,重复数据无法加入. 2. WeakSet 弱引用Set.只能存储对象,不能存储 ...
- bzoj2083: [Poi2010]Intelligence test(二分+vector)
只是记录一下vector的用法 v.push_back(x)加入x v.pop_back()弹出最后一个元素 v[x]=v.back(),v.pop_back()删除x,但是会打乱vector顺序 v ...
- 项目压力测试软件 -- LoadRunner 11.0 的安装、汉化和破解
重要说明: LoadRunner 11.0 只支持Win7,32位系统:不支持Win7,64位系统[ Win7,64位 我反复安装都没有成功!] 一.下载安装.汉化.破解文件: 我的下 ...
- selenium测试-open chrome
通过selenium来打开浏览器测试之前,需要确认本地已安装相应的webdriver,本例以chrome为例. 1. 查看本地chrome版本,以此确认需要安装的webdriver版本 查看chrom ...
- HDU4305:Lightning(生成树计数+判断点是否在线段上)
Lightning Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- POJ 3254 状态压缩 DP
B - Corn Fields Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:65536KB ...
- 如何更有效使用 Rational AppScan 扫描大型网站,第 2 部分: 案例分析
使用 AppScan 进行扫描 针对大型网站的扫描,我们按照戴明环 PDCA 的方法论来进行规划和讨论,建议 AppScan 使用步骤:计划(Plan).执行(Do).检查(check).分析(Ana ...
- C语言函数的变参实用与分析
实现变参传递的关键是: 传入参数在内存中是连续分布的. #define va_list void* #define va_arg(arg, type) *(type*)arg; arg = (char ...