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 ...
随机推荐
- Firemonkey里触发home按键被按下的事件
吾八哥我最近在使用Delphi里的Firemonkey平台写一个叫“由由密码管家”的APP工具,是跨多平台的,如ios/android/windows/macOs.由于是用于密码管理的,那么在手机里操 ...
- 使用Markdown语法画流程图
Markdown流程图 鉴于本人使用的是马克飞象,所以一下以马克飞象为例: 语法 流程图的画法和代码块类似,流程图也是写在两个```之间的.格式用flow表示,例如: st=>start ...
- python - http请求带Authorization
# 背景 接入公司的一个数据统计平台,该平台的接口是带上了Authorization验证方式来保证验签计算安全 # 方法 其实很简单,就是在header中加入key=Authorization,val ...
- Java计算手机九宫格锁屏图案连接9个点的方案总数
(一)问题 九宫格图案解锁连接9个点共有多少种方案? (二)初步思考 可以把问题抽象为求满足一定条件的1-9的排列数(类似于“八皇后问题”),例如123456789和987654321都是合法的(按照 ...
- C#一些代码小结--文件对话框
C# 一些代码小结--文件对话框 查看文件完整路径 try { Config cfg = new Config(); var file = ""; if (saveFileDial ...
- winform中控件的简单数据绑定
是因为在学习组件开发过程中有个Bindable的属性,不明白意义,然后才接触到winform的数据绑定,想着先把数据绑定这块看一下,然后去测试下是否Bindable属性设为false,就不能绑定该属性 ...
- .net core Swagger
Startup中的 ConfigureServices //注册 Swagger services.AddSwaggerGen(sg => { sg.SwaggerDoc("refu ...
- UE4随笔(一)准备过程
19号,也就是中国时间20日凌晨,虚幻4放出了"订阅制"这个重磅炸弹,估计出乎大多数人的想象,已经不止一个同事表示"自己的引擎这下没用了". 笔者前天搞定了付款 ...
- 第五章 企业项目开发--mybatis注解与xml并用
本章的代码建立在第四章<Java框架整合--切分配置文件>的项目代码之上,链接如下: http://www.cnblogs.com/java-zhao/p/5118184.html 在实际 ...
- Android 创建自定义 View 的属性 (attrs) 时需要注意的问题
自定义 View 的属性并不难,可以参照官方的文档 https://developer.android.com/training/custom-views/create-view.html 但是需要注 ...