[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 ...
随机推荐
- BZOJ3343 & 洛谷2801:教主的魔法——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=3343 https://www.luogu.org/problemnew/show/2801 题目描述 ...
- HDU5115:Dire Wolf——题解+翻译
http://acm.hdu.edu.cn/showproblem.php?pid=5115 题目大意:给n匹狼,每一次攻击可以秒杀一匹狼,但同时会受到这匹狼的a攻击和它相邻两只狼的b攻击. 给定a, ...
- 征战jQuery
一:jQuery的用途 1>.访问 和 操作 DOM元素 2>.控制 页面样式 3>.对页面事件的处理 4>.方便的使用jQuery插件 5>.与Ajax技术的完美结合 ...
- YBT 5.1 区间类动态规划
题解在代码中 石子合并[loj 10147] /* dp[i][j]=max or min(dp[i][j],dp[i][k]+dp[k+1][j]+sum[j]-sum[i-1]) i<=k& ...
- 三大linux系统对比
概述: centos作为服务器部署是第一选择.CentOS去除很多与服务器功能无关的应用,系统简单但非常稳定,命令行操作可以方便管理系统和应用,丰富的帮助文档和社区的支持. ubuntu最佳的应用领域 ...
- bzoj 1564 [NOI2009]二叉查找树 区间DP
[NOI2009]二叉查找树 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 906 Solved: 630[Submit][Status][Discu ...
- 制定clone的用户名
git clone http://username:password@127.0.0.1/res/res.git指定用户名clone,有时需要切换clone 的用户名,不切换,会默认config us ...
- (转)如何在windows 2008 安装IIS
首先声明本文转自http://www.pc6.com/infoview/Article_54712.html ,作者为清晨 转载的原因有两个,一是怕原文挂了,而是打算写一下在阿里云部署django的文 ...
- WebDriver中如何处理Iframe 及 嵌套Iframe
最近在用webdriver进行爬虫的时候,遇到了网站存在iframe的情况,处理了好久没有解决,后来发现原来webdriver自带处理方法,汗颜.. 1.iFrame有ID 或者 name的情况 // ...
- git高级用法
1.git未保存的代码怎么切换分支? 2.两个分支的代码怎么合并?怎么解决冲突? 常见报错: 1.Merge failed : Some unreacked working tree files wo ...