Source:

PAT A1021 Deepest Root (25 分)

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

Keys:

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

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

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

  2. 1021. Deepest Root (25) -并查集判树 -BFS求深度

    题目如下: A graph which is connected and acyclic can be considered a tree. The height of the tree depend ...

  3. PAT1021:Deepest Root

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

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

  6. 1021. Deepest Root (25)

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

  7. PAT 甲级 1021 Deepest Root

    https://pintia.cn/problem-sets/994805342720868352/problems/994805482919673856 A graph which is conne ...

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

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

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

随机推荐

  1. wps h5制作软件

    相当好用这个

  2. ORA-12547错误

    http://www.linuxidc.com/Linux/2013-03/81330p3.htm << good http://www.verydemo.com/demo_c158_i6 ...

  3. CF #321 (Div. 2) D

    不说了,爆内存好几次,后来醒起状态有重复... 状压+TSP #include <iostream> #include <cstdio> #include <cstrin ...

  4. 计算几何 二维凸包问题 Andrew算法

    凸包:把给定点包围在内部的.面积最小的凸多边形. Andrew算法是Graham算法的变种,速度更快稳定性也更好. 首先把全部点排序.依照第一keywordx第二keywordy从小到大排序,删除反复 ...

  5. CSS3:box-sizing:不再为盒子模型而烦恼

    题外话: W3C奉行的标准,就是content-box,就是须要计算边框,填充还有内容的;可是就我个人而言, 比較喜欢的是传统IE6时候的怪异模式,不用考虑容器是否会被撑开(打乱布局); 盒子模型差异 ...

  6. mysql-5.5 for linux源码安装

    mysql-5.5 for linux源码安装 1.使用Yum安装依赖软件包 # yum install -y gcc gcc-c++ gcc-g77 autoconf automake bison  ...

  7. Codeforces--630E--A rectangle(规律)

     E - A rectangle Crawling in process... Crawling failed Time Limit:500MS     Memory Limit:65536KB  ...

  8. css 浮动问题详解

    浮动(float),一个我们即爱又恨的属性.爱,因为通过浮动,我们能很方便地布局: 恨,浮动之后遗留下来太多的问题需要解决,特别是IE6-7(以下无特殊说明均指 windows 平台的 IE浏览器). ...

  9. Gym - 101981A The 2018 ICPC Asia Nanjing Regional Contest A.Adrien and Austin 简单博弈

    题面 题意:一堆有n个石子,编号从1⋯N排成一列,两个人Adrien 和Austin玩游戏,每次可以取1⋯K个连续编号的石子,Adrien先手,谁不能取了则输 题解:k==1时,显然和n奇偶相关,当k ...

  10. springboot配置过滤器和拦截器

    import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.Http ...