PAT 甲级 1021 Deepest Root
https://pintia.cn/problem-sets/994805342720868352/problems/994805482919673856
A graph which is connected and acyclic can be considered a tree. The hight 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 (≤) 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
代码:
- #include <bits/stdc++.h>
- using namespace std;
- const int maxn = 1e5 + 10;
- int N;
- vector<int> v[maxn];
- int vis[maxn], mp[maxn];
- int cnt = 0;
- int depth = INT_MIN;
- vector<int> ans;
- void dfs(int st) {
- vis[st] = 1;
- for(int i = 0; i < v[st].size(); i ++) {
- if(vis[v[st][i]] == 0)
- dfs(v[st][i]);
- }
- }
- void helper(int st, int step) {
- if(step > depth) {
- ans.clear();
- ans.push_back(st);
- depth = step;
- } else if(step == depth) ans.push_back(st);
- mp[st] = 1;
- for(int i = 0; i < v[st].size(); i ++) {
- if(mp[v[st][i]] == 0)
- helper(v[st][i], step + 1);
- }
- }
- int main() {
- scanf("%d", &N);
- memset(vis, 0, sizeof(vis));
- for(int i = 0; i < N - 1; i ++) {
- int a, b;
- scanf("%d%d", &a, &b);
- v[a].push_back(b);
- v[b].push_back(a);
- }
- for(int i = 1; i <= N; i ++) {
- if(vis[i] == 0) {
- dfs(i);
- cnt ++;
- }
- else continue;
- }
- set<int> s;
- int beginn = 0;
- helper(1, 1);
- if(ans.size() != 0) beginn = ans[0];
- for(int i = 0; i < ans.size(); i ++)
- s.insert(ans[i]);
- if(cnt >= 2)
- printf("Error: %d components\n", cnt);
- else {
- ans.clear();
- depth = INT_MIN;
- memset(mp, 0, sizeof(mp));
- helper(beginn, 1);
- for(int i = 0; i < ans.size(); i ++)
- s.insert(ans[i]);
- for(set<int>::iterator it = s.begin(); it != s.end(); it ++)
- printf("%d\n", *it);
- }
- return 0;
- }
第一个 dfs 搜索有多少个连通块 helper 来找树的直径的一个头 已知树的直径 树上任意一点到的最大距离的另一端一定是树的直径的一个端点 两次深搜
PAT 甲级 1021 Deepest Root的更多相关文章
- PAT甲级1021. Deepest Root
PAT甲级1021. Deepest Root 题意: 连接和非循环的图可以被认为是一棵树.树的高度取决于所选的根.现在你应该找到导致最高树的根.这样的根称为最深根. 输入规格: 每个输入文件包含一个 ...
- PAT 甲级 1021 Deepest Root (并查集,树的遍历)
1021. Deepest Root (25) 时间限制 1500 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A graph ...
- 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甲级——A1021 Deepest Root
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
- 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] 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甲级:1066 Root of AVL Tree (25分)
PAT甲级:1066 Root of AVL Tree (25分) 题干 An AVL tree is a self-balancing binary search tree. In an AVL t ...
- 1021. Deepest Root (25)——DFS+并查集
http://pat.zju.edu.cn/contests/pat-a-practise/1021 无环连通图也可以视为一棵树,选定图中任意一点作为根,如果这时候整个树的深度最大,则称其为 deep ...
- 1021.Deepest Root (并查集+DFS树的深度)
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
随机推荐
- 不要以为字段以transient修饰的话就一定不会被序列化
1: 先阅读这边文章:http://www.importnew.com/21517.html 2:被transient修饰真的会被序列化吗? 反例:java.util.ArrayList中底层存储数组 ...
- AES块加密与解密
AES块加密与解密 解密目标 在CBC和CTR两种模式下分别给出十篇加密的样例密文,求解密一篇特定的密文 解密前提 全部密文及其加密使用的key都已给出 加密的方法遵循AES的标准 解密过程分析 实验 ...
- $Simpson$积分入门
\(\rm{0x01}\) 前言 首先阐明一点,自适应辛普森算法(\(\rm{Adaptive ~Simpson's~ rule}\) )是一类近似算法(\(\rm{Approximation ~al ...
- Android处理ListView中的Item中的Button按钮不能点击的问题
问题描述:ListView列表中的Button按钮按钮不能点击 解决办法:在ListView中的Item项的布局文件中加上:android:descendantFocusability="b ...
- day 26
今日内容 classmethod 让这个类中的方法绑定自己类,这样就可以直接用类调用该方法. staticmethod 让类中的方法编程非绑定方法,也就是是这个类中的方法编程普通函数. ####### ...
- Oracle substr() instr() 用法
转载:oracle中substr() instr() 用法 substr(字符串,截取开始位置,截取长度) = 返回截取的字符串instr(源字符串,目标字符串,起始字符串,匹配字符串) = 返回要截 ...
- 半导体热阻问题深度解析(Tc,Ta,Tj,Pc)
半导体热阻问题深度解析(Tc,Ta,Tj,Pc) 本文是将我以前的<有关热阻问题>的文章重新梳理,按更严密的逻辑来讲解. 晶体管(或半导体)的热阻与温度.功耗之间的关系为: Ta=Tj-* ...
- Newtonsoft.Json.Linq对象读取DataSet数据
Newtonsoft.Json.Linq对象读取DataSet数据: private void button4_Click(object sender, EventArgs e) { ...
- SVD(奇异值分解)小结
注:奇异值分解在数据降维中有较多的应用,这里把它的原理简单总结一下,并且举一个图片压缩的例子,最后做一个简单的分析,希望能够给大家带来帮助. 1.特征值分解(EVD) 实对称矩阵 在理角奇异值分解之前 ...
- VS编程,WPF单独更改TextBlock中部分文字格式的一种方法
原文:VS编程,WPF单独更改TextBlock中部分文字格式的一种方法 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_43307934/articl ...