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的更多相关文章

  1. PAT甲级1021. Deepest Root

    PAT甲级1021. Deepest Root 题意: 连接和非循环的图可以被认为是一棵树.树的高度取决于所选的根.现在你应该找到导致最高树的根.这样的根称为最深根. 输入规格: 每个输入文件包含一个 ...

  2. PAT 甲级 1021 Deepest Root (并查集,树的遍历)

    1021. Deepest Root (25) 时间限制 1500 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A graph ...

  3. 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 ...

  4. PAT甲级——A1021 Deepest Root

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

  5. PAT 1021 Deepest Root[并查集、dfs][难]

    1021 Deepest Root (25)(25 分) A graph which is connected and acyclic can be considered a tree. The he ...

  6. [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 ...

  7. 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 ...

  8. 1021. Deepest Root (25)——DFS+并查集

    http://pat.zju.edu.cn/contests/pat-a-practise/1021 无环连通图也可以视为一棵树,选定图中任意一点作为根,如果这时候整个树的深度最大,则称其为 deep ...

  9. 1021.Deepest Root (并查集+DFS树的深度)

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

随机推荐

  1. python面试题库——3数据库和缓存

    第三部分 数据库和缓存(46题) 列举常见的关系型数据库和非关系型都有那些? 关系型数据库: Oracle.DB2.Microsoft SQL Server.Microsoft Access.MySQ ...

  2. 本地模拟服务器CDN(静态HTML,CSS,JS)开发

    本地模拟服务器CDN(静态HTML,CSS,JS)开发 所谓本地开发环境就是和线上cdn(a.longencdn.cn)一样的目录结构和功能,提供了一个本地镜像,开发者直接在本地镜像的对应目录中作开发 ...

  3. C++之数据类型

    C++语言是广泛使用的程序设计语言之一,因其特有的优势在计算机应用领域占有重要一席. C++中的数据类型 C++中的数据类型分为两大类:基本数据类型和非基本数据类型,如图1.1所示. 图1.1 C++ ...

  4. Jquery的Ajax中contentType和dataType的区别(转载)

    上代码 $.ajax({ type: httpMethod, cache: false, contentType: "application/json; charset=utf-8" ...

  5. 2.1《想成为黑客,不知道这些命令行可不行》(Learn Enough Command Line to Be Dangerous)——重定向文件和添加文件

    回忆第一章节的内容,我们用echo命令输出莎士比亚的第一首十四行诗的第一行(Listing 6): $ echo "From fairest creatures we desire incr ...

  6. MAC下配置ssh让SourceTree通过秘钥访问远程仓库

    问题描述 由于TortoiseGit没有MAC版本,我们使用了SourceTree来替代. 在帮同事解决Mac下的Git的时候,碰到一个问题:SourceTree无法使用ssh方式提交代码,这是由于没 ...

  7. sql——inner join,where,left join的区别

    1.select a.name,a.sex,a.subject,a.age from TableA a, TableB b where a.name = b.name 2.select a.name, ...

  8. MyBatis的一级缓存和二级缓存简介笔记

    关于mybatis中一级缓存和二级缓存的简单介绍 mybatis的一级缓存: MyBatis会在表示会话的SqlSession对象中建立一个简单的缓存,将每次查询到的结果结果缓存起来,当下次查询的时候 ...

  9. SPA程序加载首界面eclipse卡顿解决笔记

    最近在开发SPA程序项目时遇到一个问题,因为是在开发阶段,所以直接就在eclipse中启动项目. 每次进入首界面时,eclipse就会长时间卡顿,前端界面也加载不出来,很影响开发效率. 在查找问题的时 ...

  10. stl源码剖析 详细学习笔记 算法(3)

    //---------------------------15/03/30---------------------------- //min_element template<class Fo ...