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. PHPer是草根吗

    以下文字并没有非常多的技术词汇,所以只要对PHP感兴趣的人都可以看看. PHPer是草根吗? 从PHP诞生之日起,PHP就开始在Web应用方面为广大的程序员服务.同时,作为针对Web开发量身定制的脚本 ...

  2. [转]VS2013+简单稀疏光束调整库SSBA配置(64位编译)

    有关SSBA库的资源比较少,我是在Github上搜索下载的,具体的GitHub官方下载地址为:SSBA 下载后在SSBA解压文件夹下新建文件夹build. 打开cmake gui,在source co ...

  3. 如何为一个类型为Color的属性设置默认值

    最近在研究GDI+的时候,用winform来写自定义控件遇到需要为控件的属性设置默认值,但这个属性的类型是System.Drawing.Color.本文只是总结一下各种设置的方法. Example [ ...

  4. 2018-2019-2 20175308实验一 《Java开发环境的熟悉》实验报告

    2018-2019-2-20175308 实验一 <Java开发环境的熟悉>实验报告 一.实验内容及步骤 (一)使用JDk编译.运行简单的Java程序 输入cd Code命令进入Code目 ...

  5. Jmeter—控件

    Jmeter有许多控件,可以在我们模拟测试请求时使用. Jmeter共有这8类控件: 配置元件—Http请求默认值 作用:仅设置一次目标URL服务器地址,之后不需要每次请求都写完整的,仅写相对地址就可 ...

  6. OpenGL笔记(二) 渲染管线

    GLSL着色器代码分为两个部分,即Vertex Shader(顶点着色器)与Fragment Shader(片元着色器). 上下文环境搭建 OGL不负责窗口管理与上下文环境管理,该职责由平台完成.在A ...

  7. NYOJ 35 表达式求值

    一个模板了 哈哈. 这题由于已经包括了整形.浮点形了,以后也不须要特别处理了. /* 这里主要是逆波兰式的实现,使用两个stack 这里用字符串来模拟一个stack,第一步,将中缀表达式转变为后缀表达 ...

  8. iOS Swift WisdomScanKit二维码扫码SDK,自定义全屏拍照SDK,系统相册图片浏览,编辑SDK

    iOS Swift WisdomScanKit 是一款强大的集二维码扫码,自定义全屏拍照,系统相册图片编辑多选和系统相册图片浏览功能于一身的 Framework SDK [1]前言:    今天给大家 ...

  9. 20155210 Exp7 网络欺诈防范

    Exp7 网络欺诈防范 SET工具建立冒名网站 首先利用lsof -i:80或者netstat -tupln |grep 80查询80端口的使用情况(我的电脑80端口没有被占用,如果被占用,则用kil ...

  10. 20155305乔磊《网络对抗》逆向及Bof基础

    20155305乔磊<网络对抗>逆向及Bof基础 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何 ...