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 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条边连接的两个结点。要求判断该无向图是否是一颗树,若是一棵树,由小到大输出以其为根结点时深度最大的点,若不是树则输出连通块数量。
在解题之前首先看一下题目的限制
本题结点数量最多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的更多相关文章
- PAT (Advanced Level) 1021. Deepest Root (25)
先并查集判断连通性,然后暴力每个点作为根节点判即可. #include<iostream> #include<cstring> #include<cmath> #i ...
- PAT 1021 Deepest Root[并查集、dfs][难]
1021 Deepest Root (25)(25 分) A graph which is connected and acyclic can be considered a tree. The he ...
- PAT甲级1021. Deepest Root
PAT甲级1021. Deepest Root 题意: 连接和非循环的图可以被认为是一棵树.树的高度取决于所选的根.现在你应该找到导致最高树的根.这样的根称为最深根. 输入规格: 每个输入文件包含一个 ...
- PAT 甲级 1021 Deepest Root (并查集,树的遍历)
1021. Deepest Root (25) 时间限制 1500 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A graph ...
- [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 ...
- 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 ...
- 1021. Deepest Root (25)——DFS+并查集
http://pat.zju.edu.cn/contests/pat-a-practise/1021 无环连通图也可以视为一棵树,选定图中任意一点作为根,如果这时候整个树的深度最大,则称其为 deep ...
- PAT 1021 Deepest Root
#include <cstdio> #include <cstdlib> #include <vector> using namespace std; class ...
- 1021.Deepest Root (并查集+DFS树的深度)
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
随机推荐
- Android-JVM中的多线程&垃圾回收
Java语言是为数不多支持多线程技术的编程语言,而这多线程就不得不提到JVM虚拟机 先看代码案例:(JVM收垃圾) package android.java.thread; class Demo { ...
- WinForm如何去掉右边和下边的白边
系统给的窗体样式都缺乏美感,想要漂亮的UI只能自己做,很容易实现 1.新建窗体,设置FormBorder为None 这时的窗体就只有一个Panel(Form自带的默认Panel),没有边框,没有标题栏 ...
- ASP.NET网页VS利用文件系统发布
1.点击发布 2.选择发布方式,这里选择文件系统,并选择发布的路径 3.配置相关参数 4.点击发布按钮 5.发布成功后文件夹下生成的文件 ..
- leetcode 杨辉三角
给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2,1], [ ...
- BitAdminCore框架应用篇:(五)核心套件querySuite列的定义
索引 NET Core应用框架之BitAdminCore框架应用篇系列 框架演示:http://bit.bitdao.cn 框架源码:https://github.com/chenyinxin/coo ...
- 第五章 JVM垃圾收集器(1)
说明:垃圾回收算法是理论,垃圾收集器是回收算法的实现,关于回收算法,见<第四章 JVM垃圾回收算法> 1.七种垃圾收集器 Serial(串行GC)-- 复制 ParNew(并行GC)-- ...
- 廖雪峰Python学习笔记——类和实例
Class MyList(list): __metaclass__ = ListMetaclass #它表示在创建MyList这个类时,必须通过 ListMetaclass这个元类的LIstMetac ...
- C++引用和指针的区别
一.引用和指针的定义 引用:它是给另一个变量取一个别名,不会再次分配空间(可以带来程序的优化) 指针:它是一个实体,需要分配空间 引用在定义的时候必须进行初始化,并且空间不能够改变. 指针在定义的 ...
- day 44 django 学习入门
前情提要: 终于学到了Django ...古川小姐姐好流b .....7天学完.....脑壳疼..为了出了这个小火箭.. 详细参考官网. https://www.django.cn/ 中文网站 一: ...
- UITableView 的常用可复制代码
UITableView是使用中最常用的工具,下面列举一个常用的tableview类,以后直接复制代码,稍作修改,就能用了. #import "ViewController.h" @ ...