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 ...
随机推荐
- sql server使用杂记
SqlServer导出数据库 navcat for sql server中打开连接,打开数据库,右键--数据传输,常规选项卡--模式选择dbo,目标选择连接(选择你新建的库)或者文件(导出你要的sql ...
- maven打包需要设置main-class的插件写法
maven打包需要设置main-class的插件写法 <build> <plugins> <plugin> <groupId>org.apache.ma ...
- 2-2 第二天 利用 QQ 浏览器代理调试端口
在没有域名服务器的情况下微信代理的方案 echo "api_key: N3DYn5356kYDvcd67fRxoecKxQV7fTE0" > ~/.ultrahook gem ...
- (Go)08.time示例
package main import ( "fmt" "time" ) func test() { ) } func main() { now := time ...
- codevs1060 搞笑世界杯(概率dp)
1060 搞笑世界杯 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 随着世界杯小组赛的结束,法国,阿根廷等世界 ...
- selenium3 + python - table定位
前言 在web页面中经常会遇到table表格,特别是后台操作页面比较常见.本篇详细讲解table表格如何定位. 一.认识table 1.首先看下table长什么样,如下图,这种网状表格的都是table ...
- Python 37 进程池与线程池 、 协程
一:进程池与线程池 提交任务的两种方式: 1.同步调用:提交完一个任务之后,就在原地等待,等任务完完整整地运行完毕拿到结果后,再执行下一行代码,会导致任务是串行执行 2.异步调用:提交完一个任务之后, ...
- 模拟Queue(wait/notify)
BlockingQueue:顾名思义,首先它是一个队列,并且支持阻塞的机制,阻塞的放入和得到数据.我们要实现LinkedBlockingQueue下面的两个方法put和take. put(anObje ...
- RabbitMQ 官方NET教程(六)【RPC】
在第二个教程中,我们学习了如何使用Work Queues在多个工作者之间分配耗时的任务. 但是如果我们需要在远程计算机上运行功能并等待结果怎么办? 那是一个不同的模式. 此模式通常称为远程过程调用或R ...
- mvc action 缓存的清楚更新办法
https://www.cnblogs.com/waynechan/p/3232672.html