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. codevs 1700 施工方案第二季

    1700 施工方案第二季 2012年市队选拔赛北京  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description c国边防 ...

  2. Javascript控制回车键进行表单(form)提交(转)

    一.采用钩子事件去捕获 键盘事件有3个: keydown,keypress,keyup分别是按下,按着没上抬,上抬键盘 . $(document).keyup(function(event){ if( ...

  3. F2BPM 开发Api与RESTfull应用服务Api 层次关系及示例

    目前越来越多的企业架构解决方案更加趋向于基于http协议“微服务”访问跨系统调用,而不使用统传的WebService调用,即通过RESTfull方式进行交互,更加轻量整合调用更加方便.本文档中所有F2 ...

  4. [转]十五天精通WCF——第一天 三种Binding让你KO80%的业务

    转眼wcf技术已经出现很多年了,也在.net界混的风生水起,同时.net也是一个高度封装的框架,作为在wcf食物链最顶端的我们所能做的任务已经简单的不能再简单了, 再简单的话马路上的大妈也能写wcf了 ...

  5. HDU 1561&HDU 3449 一类简单依赖背包问题

    HDU 1561.这道是树形DP了,所谓依赖背包,就是选A前必须选B,这样的问题.1561很明显是这样的题了.把0点当成ROOT就好,然后选子节点前必须先选根,所以初始化数组每一行为该根点的值.由于多 ...

  6. springmvc 解析xml数据

    springmvc 解析xml数据 http://blog.csdn.net/zhi_jun/article/details/37925475

  7. Ambari-部署常见问题

    重新启动ambari-server端后调用install.start API后返回200 导致该问题的解决办法是server在启动后没有收到agent的心跳即没有与agent建立连接,在此时进行API ...

  8. shell EOF注意点

    当sqlplus与shell交互的时候我们这么用 su - oracle -c "sqlplus / as sysdba<<EOF select * from gv($insta ...

  9. axis实现webservices分布式通信

    分布式通信原理 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2ZsMjAxMjEzMTQ=/font/5a6L5L2T/fontsize/400/fil ...

  10. Java数据结构2——深入JCF

    Java集合框架(JCF)参考C++的STL实现的在日常Java开发工作很常用的数据结构容器,有技术追求的人除了要会简单使用JCF之外,也要知道其底层的实现机制,知道它是如何实现的,为什么这样实现.就 ...