PAT_A1021#Deepest Root
Source:
Description:
A graph which is connected and acyclic can be considered a tree. The height 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−1lines 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
whereK
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
Keys:
- set(C++ STL)
- vector(C++ STL)
- 深度优先搜索(Depth First Search)
- 图的存储和遍历
Attention:
- 邻接矩阵存储图的规模要小于1e3,否则用邻接表存储
Code:
/*
Data: 2019-05-18 19:50:30
Problem: PAT_A1021#Deepest Root
AC: 45:38 题目大意:
寻找无环图中深度最大的生成树
输入:
第一行给出结点数N<1e4(结点从1~N)
接下来N-1行给出存在边的两结点V1和V2
输出:
给出最大深度生成树的根结点,不唯一从小到大依次输出根结点。
若图不连通则给出连通分量个数 基本思路:
从任意顶点开始遍历全图,root记录所能达到的最大深度及其顶点;
再从root中任选一顶点,再次遍历全图,leaf记录所能达到的最大深度及顶点;
如果图连通的话,
生成树的最大深度就是第二次遍历全图所能达到的最大深度;
the deepest root就是root+leaf的并集
*/ #include<cstdio>
#include<algorithm>
#include<set>
#include<vector>
using namespace std;
const int M=1e4;
int vis[M],n,depth,f;
set<int> root,leaf;
vector<int> grap[M]; void DFS(int u, int deep)
{
vis[u]=;
if(deep > depth)
{
depth=deep;
if(f)
{
root.clear();
root.insert(u);
}
else
{
leaf.clear();
leaf.insert(u);
}
}
else if(deep == depth)
if(f) root.insert(u);
else leaf.insert(u); for(int i=; i<grap[u].size(); i++)
{
int v = grap[u][i];
if(vis[v]==)
DFS(v,deep+);
}
} int Travel()
{
int block=;
fill(vis,vis+M,);
for(int v=; v<=n; v++)
{
if(vis[v]==)
{
DFS(v,);
block++;
}
}
return block;
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE scanf("%d", &n);
for(int i=; i<n; i++)
{
int v1,v2;
scanf("%d%d", &v1,&v2);
grap[v1].push_back(v2);
grap[v2].push_back(v1);
}
depth=;f=;
int block = Travel();
if(block > )
printf("Error: %d components\n", block);
else
{
fill(vis,vis+M,);f=;
DFS(*root.begin(),);
for(auto it=leaf.begin(); it!=leaf.end(); it++)
root.insert(*it);
for(auto it=root.begin(); it!=root.end(); it++)
printf("%d\n", *it);
} return ;
}
PAT_A1021#Deepest Root的更多相关文章
- 1021.Deepest Root (并查集+DFS树的深度)
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
- 1021. Deepest Root (25) -并查集判树 -BFS求深度
题目如下: A graph which is connected and acyclic can be considered a tree. The height of the tree depend ...
- PAT1021:Deepest Root
1021. Deepest Root (25) 时间限制 1500 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A graph ...
- A1021. Deepest Root
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
- PAT A1021 Deepest Root (25 分)——图的BFS,DFS
A graph which is connected and acyclic can be considered a tree. The hight of the tree depends on th ...
- 1021. Deepest Root (25)
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
- PAT 甲级 1021 Deepest Root
https://pintia.cn/problem-sets/994805342720868352/problems/994805482919673856 A graph which is conne ...
- PAT 1021 Deepest Root[并查集、dfs][难]
1021 Deepest Root (25)(25 分) A graph which is connected and acyclic can be considered a tree. The he ...
- PTA (Advanced Level) 1021 Deepest Root
Deepest Root A graph which is connected and acyclic can be considered a tree. The hight of the tree ...
随机推荐
- N天学习一个linux命令之xargs
用途 标准输入流读取参数(空格或者换行符分隔),传递给需要执行的命令 用法 xargs [options] [command [initial-arguments]] 常用选项 --arg-file= ...
- 微信 创建自定义菜单 向微信发起的post请求
微信 创建自定义菜单 向微信发起的post请求 Map<String, Object> res = new HashMap<String, Object>(); try { S ...
- HDOJ 4857 逃生
BestHack.....真乱..... 逃生 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- [PWA] Check Online Status by using the NavigatorOnLine API
Even if you have your application fully cached, you couldn’t perform any external request without in ...
- win7下code::blocks开发环境
一.环境配置步骤: 下载安装code::blocks; 下载安装MinGW; 在complier settings中, Toolchain executables选择MinGW的安装路径. 完成安装. ...
- html5--视频播放器实例
html5--视频播放器实例 总结: 1.相对定位和绝对定位的区别,两者都是浮起来了 2.属性和方法都是有对象的,搞清楚对象之后,属性和方法就很好用了,我们一般可以用document.getEleme ...
- 2017 Multi-University Training Contest - Team 2&&hdu 6047 Maximum Sequence
Maximum Sequence Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- treap平衡树
今天集训讲平衡树,就瞎搞了一下.直接下代码. #include<iostream> #include<cstdio> #include<cmath> #includ ...
- openStack 主机流量计运行状态 随笔记录
root@ruiy-controller:~# ifconfigeth0 Link encap:Ethernet HWaddr 0c:c4:7a:0d:97:2c ine ...
- Appium + python - swipe滑屏操作实例
方法一: from appium import webdriverfrom time import sleep descred_caps = { "platformName":&q ...