Deepest Root

  A graph which is connected and acyclic can be considered a tree. The hight 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 (≤10​^4​​) which is the number of nodes, and hence the nodes are numbered from 1 to N. Then N−1 lines 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 where 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

题目解析
  本题给出一个无向图的信息,包括一个数字n,代表无向图有n个结点,之后跟随n-1行分别为n-1条边连接的两个结点。要求判断该无向图是否是一颗树,若是一棵树,由小到大输出以其为根结点时深度最大的点,若不是树则输出连通块数量。

  在解题之前首先看一下题目的限制

  时间限制: 2000 ms
  内存限制: 64 MB
  

  本题结点数量最多1e4个若用二维数组存储邻接矩阵,虽然数组没有超限但是当节点数取到最大1e4个结点时,需要的内存空间超过382MB,所以在这里使用邻接表来存储无向图。对于判断无向图是否为树,由于无向图有n个结点n-1条边,那么只要这个图是连通图,它便肯定是一颗树。我们只需要维护一个并查集便可以达到判断数与输出连通块数量,由于本题时间限制非常松,时间宽松使人暴力,所以在寻找深度最大的根结点时,这里以所有点为根结点暴力深搜获取深度,用set保存所有深度最大的点,之后将其输出即可。

 #include<bits/stdc++.h>
using namespace std;
const int MAX =1e4 + ;
vector<int> G[MAX];
int f[MAX];
int n;
void init(){ //初始化并查集
for(int i = ; i <= n; i++)
f[i] = i;
}
int getF(int x) //并查集找爹函数
{
if(f[x] == x)
return x;
else
return f[x] = getF(f[x]);
}
int maxH = ;
void DFS_getH(int u, int height, int preNode){ //深搜获取深度
maxH = max(maxH, height);
for(int i = ; i < G[u].size(); i++)
if(G[u][i] != preNode)
DFS_getH(G[u][i], height + , u);
}
int main(){
scanf("%d", &n); //输入结点数量
init(); //初始化并查集
memset(G, , sizeof(G));
for(int i = ; i < n - ; i++){ //输入n-1条边
int u, v;
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
int fu = getF(u);
int fv = getF(v);
if(fu != fv) //合并一条的两个端点
f[fu] = fv;
}
int cnt = ; //记录连通块个数
for(int i = ; i <= n; i++){ //获取连通块个数
if(f[i] == i)
cnt++;
};
if(cnt != ) //若连通块个数不等于1证明给出的图不是一颗树
printf("Error: %d components\n", cnt); //输出连通块个数
else{
set<int> ans;
int ansH = ;
for(int i = ; i <= n; i++){
maxH = ;
DFS_getH(i, , ); //获取每一个点为根时对应的树高(用maxH记录)
if(maxH > ansH){ //ansH记录当前最高深度
ansH = maxH; //若找到深度更大的点便清空集合重新记录
ans.clear();
ans.insert(i);
}else if(maxH == ansH){ //找到深度与当前最大深度相同的点便加入集合
ans.insert(i);
}
}
for(auto i : ans){
printf("%d\n", i);
}
}
return ;
}

PTA (Advanced Level) 1021 Deepest Root的更多相关文章

  1. PAT (Advanced Level) 1021. Deepest Root (25)

    先并查集判断连通性,然后暴力每个点作为根节点判即可. #include<iostream> #include<cstring> #include<cmath> #i ...

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

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

  3. PAT甲级1021. Deepest Root

    PAT甲级1021. Deepest Root 题意: 连接和非循环的图可以被认为是一棵树.树的高度取决于所选的根.现在你应该找到导致最高树的根.这样的根称为最深根. 输入规格: 每个输入文件包含一个 ...

  4. PAT 甲级 1021 Deepest Root (并查集,树的遍历)

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

  5. [PAT] 1021 Deepest Root (25)(25 分)

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

  6. PAT 甲级 1021 Deepest Root (25 分)(bfs求树高,又可能存在part数part>2的情况)

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

  7. 1021. Deepest Root (25)——DFS+并查集

    http://pat.zju.edu.cn/contests/pat-a-practise/1021 无环连通图也可以视为一棵树,选定图中任意一点作为根,如果这时候整个树的深度最大,则称其为 deep ...

  8. PAT 1021 Deepest Root

    #include <cstdio> #include <cstdlib> #include <vector> using namespace std; class ...

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

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

随机推荐

  1. SSH框架中配置log4j的方法

    SSH框架中使用log4j的方便之处 1. 动态的改变记录级别和策略,即修改log4j.properties,不需要重启Web应用,这需要在web.xml中设置一下.2. 把log文件定在 /WEB- ...

  2. 使用Markdown语法画流程图

    Markdown流程图   鉴于本人使用的是马克飞象,所以一下以马克飞象为例: 语法   流程图的画法和代码块类似,流程图也是写在两个```之间的.格式用flow表示,例如: st=>start ...

  3. ListView的另一种可读性更强的ViewHolder模式写法

    常见的写法是这样的: @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHol ...

  4. 微软研发流程(ALM)管理培训会议(比亚迪汽车)

    主要讨论和演示整体研发流程,包括需求管理.项目计划.开发管理.生成和发布.测试管理等. Figure 1 - 客户现场培训 Figure 2 - 客户现场培训 Figure 3 - 客户现场培训

  5. bash基本命令速查表

    来源:https://github.com/skywind3000/awesome-cheatsheets/blob/master/languages/bash.sh ################ ...

  6. Android实现自带横线的EditText

    (一)问题 怎样实现带有横栏的EditText(像记事本的编辑界面那样)? (二)初步思路 1.通过修改EditText背景来实现(系统背景是一个框形图片,内部透明,替换为一个带有横栏的图片即可) 2 ...

  7. Struts2-057/CVE-2018-11776两个版本RCE漏洞分析(含EXP)

    0x01 前言 2018年8月22日,Apache Strust2发布最新安全公告,Apache Struts2存在远程代码执行的高危漏洞(S2-057/CVE-2018-11776),该漏洞由Sem ...

  8. List泛型集合对象排序

    本文的重点主要是解决:List<T>对象集合的排序功能. 一.List<T>.Sort 方法 () MSDN对这个无参Sort()方法的介绍:使用默认比较器对整个List< ...

  9. BitAdminCore框架应用篇:(五)核心套件querySuite列的定义

    索引 NET Core应用框架之BitAdminCore框架应用篇系列 框架演示:http://bit.bitdao.cn 框架源码:https://github.com/chenyinxin/coo ...

  10. Spring+Ehcache

    这里记录一下Spring+Ehcache的结合使用 1.添加依赖 <dependency> <groupId>org.springframework</groupId&g ...