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. Activity-fragment-ListView展示

    在上一篇博客,Android-fragment简介-fragment的简单使用,介绍了简单的使用: 这篇博客主要讲解,在fragment做处理事情(Activity的事情) Activity pack ...

  2. Spring Boot 2 实践记录之 Redis 及 Session Redis 配置

    先说 Redis 的配置,在一些网上资料中,Spring Boot 的 Redis 除了添加依赖外,还要使用 XML 或 Java 配置文件做些配置,不过经过实践并不需要. 先在 pom 文件中添加 ...

  3. INNER JOIN与LEFT JOIN在SQL Server的性能

    我创建了INNER JOIN 9桌,反正需要很长的(超过五分钟).所以,我的民歌改变INNER JOIN来LEFT JOIN LEFT JOIN的性能较好,在首次尽管我所知道的.之后我变了,查询的速度 ...

  4. docker 多阶段构建

    构建镜像最具挑战性的一点是使镜像大小尽可能的小.Dockerfile中的每条指令都为图像添加了一个图层,您需要记住在移动到下一层之前清理任何不需要的工件.对于多阶段构建,您可以在Dockerfile中 ...

  5. .Net Core 跨平台应用使用串口、串口通信 ,可能出现的问题、更简洁的实现方法

    前些天在学习在 .NET Core下,跨平台使用串口通讯,有一篇文章说到在Linux/物联网下,实现通讯. 主要问题出现在以下两个类库 SerialPortStream flyfire.CustomS ...

  6. 简单了解 iTextSharp实现HTML to PDF

    查了下 转PDF的各种框架   发现大部分都是收费的. 发现一款免费的iTextSharp  就想玩一下 只是简单做个HTML 转PDF  没有过深的探究. 首先 我在项目中引入iTextSharp  ...

  7. 网易云安全DDoS高防全新上线 ,游戏防护实力领先

    本文由  网易云发布.       10月24日,网易云安全(易盾)正式上线DDoS高防解决方案[点击查看].基于网易20年网络安全防护经验,网易云安全(易盾)DDoS高防可提供1T超大防护带宽,拥有 ...

  8. PageAdmin CMS网站建设教程:如何创建及管理栏目?

    PageAdmin CMS网站制作教程:如何创建及管理栏目?1.登录管理后台后,在顶部导航中找到网站,并点击: 2.在左侧栏目中找到栏目管理,并点击: 3.进入到栏目管理页面,在顶部找到菜单,点击添加 ...

  9. MySQL数据库命令大全

    --数据库操作前的准备-- 创建数据库-- create database python_test_1 charset=utf8; -- 使用数据库-- use python_test_1; -- s ...

  10. Queue-621. Task Scheduler

    Given a char array representing tasks CPU need to do. It contains capital letters A to Z where diffe ...