PAT A1021 Deepest Root (25 分)——图的BFS,DFS
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 (≤104) 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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <queue>
#include <string>
#include <set>
#include <map>
using namespace std;
const int maxn = ;
const int inf = ;
int n;
int depth[maxn] = { };
bool vis[maxn] = { false };
struct node {
int id;
int depth;
}nodes[maxn];
vector<int> adj[maxn];
void bfs(int v) {
queue<node> q;
q.push(nodes[v]);
vis[v] = true;
while (!q.empty()) {
node u = q.front();
q.pop();
for (int i = ; i < adj[u.id].size(); i++) {
if (vis[adj[u.id][i]] == false) {
nodes[adj[u.id][i]].depth = u.depth + ;
q.push(nodes[adj[u.id][i]]);
vis[adj[u.id][i]] = true;
if (nodes[adj[u.id][i]].depth > depth[v]) {
depth[v] = nodes[adj[u.id][i]].depth;
}
}
}
}
}
bool bfs_c(int v) {
fill(vis, vis + maxn, false);
queue<int> q;
q.push(v);
vis[v] = true;
int count = ;
while (!q.empty()) {
int u = q.front();
q.pop();
vis[u] = true;
for (int i = ; i <adj[u].size(); i++) {
if (vis[adj[u][i]] == false) {
q.push(adj[u][i]);
count++;
if (count > n)return false;
}
}
}
return true;
}
int bfsTrave() {
fill(vis, vis + maxn, false);
int count = ;
for (int i = ; i <= n; i++) {
if (vis[i] == false) {
bfs(i);
count++;
}
}
return count;
}
int main() {
cin >> n;
for (int i = ; i < n; i++) {
int c1, c2;
cin >> c1 >> c2;
adj[c1].push_back(c2);
adj[c2].push_back(c1);
}
for(int i=;i<=n;i++){
nodes[i].id = i;
nodes[i].depth = ;
}
int k = bfsTrave();
if (k > )printf("Error: %d components", k);
else {
if (!bfs_c())printf("Error: %d components", k);
else {
for (int i = ; i <= n; i++) {
fill(vis, vis + maxn, false);
for (int i = ; i <= n; i++) {
nodes[i].depth = ;
}
bfs(i);
}
int max_d = ;
vector<int> maxi;
for (int i = ; i <= n; i++) {
if (depth[i] > max_d) {
max_d = depth[i];
maxi.clear();
maxi.push_back(i);
}
else if (depth[i] == max_d) {
maxi.push_back(i);
}
}
for (int i = ; i < maxi.size(); i++) {
printf("%d\n", maxi[i]);
}
}
}
system("pause");
}
注意点:考察整个图的遍历以及有环无环图的判断。这里判断有没有环我是通过bfs的加入队列个数超过n来判断的。每个节点遍历一遍,找到最大深度再输出。
ps:看了别人的思路,发现自己想多了,n个节点n-1条边,若只有1个联通块就不会有环,所以那个都是白判断的。
ps2:随便找一个节点dfs找到最深的那些节点,再从那些节点里挑一个dfs找到最深的节点,并集就是所有最深的节点,不需要每个节点都做一次搜索。
PAT A1021 Deepest Root (25 分)——图的BFS,DFS的更多相关文章
- 【PAT甲级】1021 Deepest Root (25 分)(暴力,DFS)
题意: 输入一个正整数N(N<=10000),然后输入N-1条边,求使得这棵树深度最大的根节点,递增序输出.如果不是一棵树,输出这张图有几个部分. trick: 时间比较充裕数据可能也不是很极限 ...
- 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 ...
- PAT Advanced A1021 Deepest Root (25) [图的遍历,DFS,计算连通分量的个数,BFS,并查集]
题目 A graph which is connected and acyclic can be considered a tree. The height of the tree depends o ...
- [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 分) 并查集判断成环和联通+求树的深度
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
- 1021 Deepest Root (25 分)
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
- [PAT] A1021 Deepest Root
[题目大意] 给出n个结点和n-1条边,问它们能否形成一棵n个结点的树,如果能,从中选出结点作为树根,使整棵树的高度最大.输出所有满足要求的可以作为树根的结点. [思路] 方法一:模拟. 1 连通.边 ...
- 1013 Battle Over Cities (25分) 图的连通分量+DFS
题目 It is vitally important to have all the cities connected by highways in a war. If a city is occup ...
- PAT 1021 Deepest Root[并查集、dfs][难]
1021 Deepest Root (25)(25 分) A graph which is connected and acyclic can be considered a tree. The he ...
随机推荐
- hive的行列转换
行转列(把多个行合并) 比如把: id tag 1 12 1 23 2 67 2 78 2 76 行转列之后: id tag 1 12,23 2 67,78,76 使用函数为:concat_w ...
- linux下vscode的c++工程配置
准备 安装vscode,可直接下载deb包进行安装,完成后安装C/C++ for Visual Studio Code插件,安装后重启(最新1.3版本以后不需要重启). 生成目录和文件 新建文件夹[t ...
- POM、STS、IOC、DI、AOP
POM:全称:poject object model 说明:项目对象模型.maven用来管理项目的依赖.编译.文档等信息 STS: 全称:spring tool suite 说明:spring 基于e ...
- 【读书笔记】iOS-自定义 URL Scheme 完全指南
iPhone / iOS SDK 最酷的特性之一就是应用将其自身”绑定”到一个自定义 URL scheme 上,该 scheme 用于从浏览器或其他应用中启动本应用. 注册自定义 URL Sche ...
- 启动MySQL报错
安装完MySQL,启动MySQL报错,报错信息如下:Starting MySQL....The server quit without updating PID file (/data/mysqlda ...
- CentOS7.4 系统下 Tomcat 启动慢解决方法
CentOS7.4 系统下 Tomcat 启动慢解决的方法 首先查看日志信息,查看因为什么而启动慢 在CentOS7启动Tomcat时,启动过程很慢,需要几分钟,经过查看日志,发现耗时在这里:是s ...
- loadrunner 脚本开发-int型变量和字符串的相互转换
脚本开发-int型变量和字符串的相互转换 by:授客 QQ:1033553122 字符串转化为int型变量 Action2() { int j = 0; j = atoi("12345&qu ...
- iOS动画-从UIView到Core Animation
首先,介绍一下UIView相关的动画. UIView普通动画: [UIView beginAnimations: context:]; [UIView commitAnimations]; 动画属性设 ...
- maven(一):是否有必要使用maven
以下是普通项目和maven项目 分别引入spring core模块的区别 1,假设我们有十个项目,都需要引入spring core模块,那么需要十份重复的Spring core.jar和commons ...
- SQL alwayson 辅助接点查询统计信息“丢失”导致查询失败
ALWAYSON 出现以下情况已经2次了,记录下: DBCC 执行完毕.如果 DBCC 输出了错误信息,请与系统管理员联系. 消息 2767,级别 16,状态 1,过程 sp_table_statis ...