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. Java 和JS Base64加密

    项目在登录.注册等场景实现时,经常会用到用户信息前端加密,然后项目后端二次解密,避免信息直接在浏览器上以明文显示. 本文主要介绍了base64加密的方式处理代码,不支持中文 源码如下: base64. ...

  2. SpringBoot多数据源改造(一)

    今天做一个需求,业务项目需要访问另一个项目的数据库. 常用两种方案: 1.另一个项目提供一个RestFul API,供调用方通过feign或其它httpClient等方式来访问. 2.项目中通过配置多 ...

  3. RDS for MySQL Mysqldump 常见问题和处理

    https://help.aliyun.com/knowledge_detail/41732.html?spm=5176.7841698.2.13.u67H3h

  4. UVa 642 - Word Amalgamation

    题目:给你一个单词列表.再给你一些新的单词.输出列表中又一次排列能得到此新单词的词. 分析:字符串.对每一个字符串的字母排序生成新的传f(str).总体排序,用二分来查找就可以. 说明:注意输出要满足 ...

  5. H3C交换机经常使用命令汇总

    H3C交换机经常使用命令 1.查看Linux下查看port状态 root@root:~# netstat -an|grep -E "6002|6003" 2.H3C交换机显示当前配 ...

  6. CentOS下安装使用phpMyAdmin. Set up phpMyAdmin on CentOS

    须要组件: Apache PHP Mysql phpMyAdmin Apache 0. yum install httpd 1. 确认版本号 $ httpd -v 2. 启动apache $ sudo ...

  7. jquery文件批量上传控件Uploadify3.2(java springMVC)

    人比較懒  有用为主 不怎么排版了 先放上Uploadify的官网链接:http://www.uploadify.com/  -->里面能够看到PHP的演示样例,属性说明,以及控件下载地址.分f ...

  8. luogu1991 无线通讯网

    题目大意 国防部计划用无线网络连接若干个边防哨所.2 种不同的通讯技术用来搭建无线网络:每个边防哨所都要配备无线电收发器:有一些哨所还可以增配卫星电话.任意两个配备了一条卫星电话线路的哨所(两边都ᤕ有 ...

  9. OTA升级中关于update.zip包的一些总结【转】

    本文转载自:http://429564140.iteye.com/blog/2337165 update.zip包整理 一. update.zip包的目录结构           |----boot. ...

  10. bzoj1898: [Zjoi2005]Swamp 沼泽鳄鱼

    一眼矩乘 把图分成12个,然后直接搞. #include<cstdio> #include<iostream> #include<cstring> #include ...