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. Spring的InitializingBean与DisposableBean方法

    在bean初始化的时候,将所有显示提供的属性设置完毕后调用这个方法 org.springframework.beans.factory.InitializingBean#afterProperties ...

  2. 2018-2019-2 20165302 《网络对抗技术》Exp4 恶意代码分析

    实验要求 1.监控你自己系统的运行状态,看有没有可疑的程序在运行 2.分析一个恶意软件,就分析Exp2或Exp3中生成后门软件:分析工具尽量使用原生指令或sysinternals,systracer套 ...

  3. QGis C++ 开发之创建临时图层并添加要素

            开发环境:Win10 + VS2010 + Qt 4.8.6 + QGis 2.14.4 其实本文实现的功能类似于QGis中“添加文本数据图层”的一个简化版,本文不会涉及到对话框的使用 ...

  4. Android soundpool初探

    内容:本编播客主要讲解一下“即时音效”: 特点:快,短. 在播放这类时间短但是要求反应迅速的的音效,就不能够用不能够使用播放时间较长的音乐播放技术了,而应该采取soundpool技术来播放. soun ...

  5. Fiddler抓包调试前端脚本代码

    0.写在前面的话 之前看了阮一峰老师关于互联网协议入门的博客,受益匪浅,接着再去体会了下HTTP协议,就想着看实际网络访问中的那些HTTP请求头和响应是什么样的.Chrome的调试工具的Network ...

  6. 图文列表,关于Simpleadapter

    main.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xm ...

  7. Apple Watch应用开发之PM2.5查询

    最近脚伤,停止了跑步,看来又要胖了,不过最近倒是对Swift语言很感兴趣,然后就开始了Apple Watch应用的开发,今天是6月8日,苹果的发布会就会在今天过后的凌晨一点开始,今天还是有点小激动的, ...

  8. Tomcat端口被占用解决方案

    Tomcat端口被占用解决方法 1.在dos下,输入 netstat -ano|findstr 8080 //说明:查看占用8080端口的进程,显示占用端口的进程 2.taskkill /pid 19 ...

  9. 2017-2018-1 20155232 嵌入式C语言——时钟

    2017-2018-1 20155232 嵌入式C语言--时钟 任务: 在作业本上完成附图作业,要认真看题目要求. 提交作业截图 作弊本学期成绩清零(有雷同的,不管是给别人传答案,还是找别人要答案都清 ...

  10. jdbc获取blob类型乱码

    一.使用场景: mysql数据库字段类型为longblob,在数据库里看中文字符正常,java读取字串的时候发现中文乱码 使用到了activeMq 二.排查: (1)修改eclipse的环境编码为ut ...