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 ...
随机推荐
- Delphi Dll 动态调用例子(3)-仔细看一下
http://blog.163.com/bxf_0011/blog/static/35420330200952075114318/ Delphi 动态链接库的动态和静态调用 为了让人能快速的理解 静态 ...
- MAC将 /etc/sudoers文件修改错后的几种解决方法
文件修改错误后难以再次修改的原因: 1.修改此文件必须是root权限 2.此文件出现问题时sudo命令不可用 3.默认情况下MAC系统不启用root用户 解决的方法: 一.启用root用户,使用roo ...
- Checkpoint--实现步骤
Checkpoint 实现步骤: 1.将CheckPoint标记写入日志(标记中包含当前数据库中活动的事务信息),并将Log Block写入持久化存储 2.将Buffer Pool中所有的脏页写入磁盘 ...
- 浏览器报ScriptResource.axd异常
新拷贝的一份管理后台代码,部署在另一台服务器上时,查看浏览器调控制台,发现有几个红色报错.这些错误导致网站的部分功能无法使用. 主要错误有: 1.“Sys”未定义 2.asp.net ajax 客户端 ...
- ASP.NET MVC 全局异常
先新建一个过滤器ExceptionHandleErrorAttribute.cs 内容如下: using System; using System.Net; using System.Web; usi ...
- 安装docker ce版
可参考 菜鸟教程:http://www.runoob.com/docker/centos-docker-install.html 官网教程:https://docs.docker.com/instal ...
- Linux(Debian)网卡设置
debian IP地址配置 vim /etc/network/interface 配置网卡eth0的IP地址 auto eth0 表示网卡随系统自动请 iface eth0 inet static ...
- BitAdminCore框架更新日志20180523
20180523更新内容 本次更新两个内容,一是增加视频处理功能,二是增加定时服务功能. 视频处理 定时服务 BitAdminCore框架,用最少的代码,实现最多的功能 本次新暂未发布,后续有空发布 ...
- Ruby for Sketchup 贪吃蛇演示源码(naive_snake)
sketchup是非常简单易用的三维建模软件,可以利用ruby 做二次开发, api文档 http://www.rbc321.cn/api 今天在su中做了一款小游戏 贪吃蛇,说一下步骤 展示 主要思 ...
- silverlight chart 折线图 的线颜色如何修改???
silverlight chart 折线图 的线颜色如何修改??? 我做出来都是这些偏黄色,请问如何修改线的颜色,以及线的宽度?谢谢