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 ...
随机推荐
- 对比flash与ajax哪个好?
Ajax的优势: (1)可搜索性 普通的文本网页会更有利于SEO.文本内容是搜索引擎容易检索的,而繁琐的swf字节码却是搜索引擎不愿触及的.虽然Google等一些大型的搜索引擎可以检索SWF内部的内容 ...
- vue项目 使用nginx代理
nginx是一个高性能的HTTP和反向代理服务器.因此常用来做静态资源服务器和后端的反向代理服务器.本文主要记录使用nginx去部署使用vue搭建的前端项目,项目基于vue官方的脚手架vue-cli构 ...
- 多线程之CAS
在JDK 5之前Java语言是靠synchronized关键字保证同步的,这会导致有锁 锁机制存在以下问题: (1)在多线程竞争下,加锁.释放锁会导致比较多的上下文切换和调度延时,引起性能问题. (2 ...
- STlinkSWD模式连线方式
若使用SWD模式,则只需要连接4根线,7,9,20,1即SWDIO,SWDCLK,GND,VCC.VCC为3.3V
- jsp二(指令)
一.jsp动作标签: 1)<jsp:forward> 请求转发 相当于之前的request.getRequestDispatcher(..).forward(..); <!--jsp ...
- Android Frame动画demo
Android动画介绍:Android为我们提供了两种动画实现,Frame和Tween. 两者之间的区别: 1.Frame动画:就像放电影一样,是通过预先做好的图片进行连续播放从而形成动画效果 2.T ...
- 大数据入门第二十五天——logstash入门
一.概述 1.logstash是什么 根据官网介绍: Logstash 是开源的服务器端数据处理管道,能够同时 从多个来源采集数据.转换数据,然后将数据发送到您最喜欢的 “存储库” 中.(我们的存储库 ...
- 07-django项目
1.sql注入,xss攻击,csrf, sql注入 把sql命令插入到web表单,然后提交到所在页面请求,从而达到欺骗服务器执行恶意的sql命令 解决方法:不要使用动态拼接sql,把指令和数据分开,参 ...
- springboot打包成war后部署项目出现异常 LifecycleException: Failed to start component
完整异常:org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].Sta ...
- dubbo见解
调用关系说明 服务容器负责启动,加载,运行服务提供者. 服务提供者在启动时,向注册中心注册自己提供的服务. 服务消费者在启动时,向注册中心订阅自己所需的服务. 注册中心返回服务提供者地址列表给消费者, ...