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 ...
随机推荐
- 开源仓库Harbor搭建及配置过程
1.Harbor介绍 Harbor是Vmvare中国团队开发的开源registry仓库,相比docker官方拥有更丰富的权限权利和完善的架构设计,适用大规模docker集群部署提供仓库服务. 2.安装 ...
- JAVA框架Struts2(二)
一:Struts2执行流程: 1)编写页面,点击超链接,请求提交到服务器端. 2)请求先经过Struts2核心过滤器(StrutsprepareAndexectuterfilter). 3)过滤器的功 ...
- Java 中long类型转换成为int类型时可能会出错的地方
那计算两个日期之间间隔的天数为例来说明这个问题. 下面是计算日期间隔天数的简单算法(主要出错的地方为红色标注的地方): public int getDay(String startDate, Stri ...
- Android Studio 设置代码提示和代码自动补全快捷键--Eclipse 风格 - 转
首先本文转自http://blog.csdn.net/csdnzouqi/article/details/50454703,是为了方便以后查看这些设置,最后在这里感谢原博主. 为了能跟上技术发展的脚步 ...
- Exp6 20155218 信息搜集与漏洞扫描
Exp6 信息搜集与漏洞扫描 1.DNS IP注册信息的查询 1.进行whois查询时,要去掉www,ftp等前缀,否则可能在whois服务器中查询不到: 2.使用whois查询ip的地理位置: 2. ...
- 20155338课程设计个人报告——基于ARM实验箱的Android交友软件的设计与实现
课程设计个人报告--基于ARM实验箱的Android交友软件的设计与实现 个人贡献 实验环境的搭建 代码调试 在电脑上成功运行 研究程序代码撰写小组报告 一.实验环境 1.Eclipse软件开发环境: ...
- adb连接手机的两种方式
adb连接手机进行调试有两种方式,一种使用USB线,一种使用无线WiFi. 第一种 使用USB线连接 1. 在手机上启用USB调试 2. CMD窗口输入adb devices,此时可以看到自己的设备 ...
- SQLAlchemy 关联表删除实验
本实验所用代码来源于官网文档 from sqlalchemy import Table, Column, Integer, String, ForeignKey from sqlalchemy.orm ...
- flask使用sqlit3的两种方式
方式一:raw_sql import sqlite3 from flask import Flask, request, jsonify app = Flask(__name__) DATABASE_ ...
- 牛客OI周赛4-提高组-C-战争[并查集]
题意 一个长度为 \(n\) 的序列,每个权值互不相同,给出形如 \(l,r,p\) 的信息表示 \([l,r]\) 区间中最小的数是 \(p\) ,问第几个信息开始出现矛盾. \(n\leq 5 \ ...