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
作者

#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的更多相关文章

  1. 【PAT甲级】1021 Deepest Root (25 分)(暴力,DFS)

    题意: 输入一个正整数N(N<=10000),然后输入N-1条边,求使得这棵树深度最大的根节点,递增序输出.如果不是一棵树,输出这张图有几个部分. trick: 时间比较充裕数据可能也不是很极限 ...

  2. 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 ...

  3. 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 ...

  4. [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 ...

  5. 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 ...

  6. 1021 Deepest Root (25 分)

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

  7. [PAT] A1021 Deepest Root

    [题目大意] 给出n个结点和n-1条边,问它们能否形成一棵n个结点的树,如果能,从中选出结点作为树根,使整棵树的高度最大.输出所有满足要求的可以作为树根的结点. [思路] 方法一:模拟. 1 连通.边 ...

  8. 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 ...

  9. PAT 1021 Deepest Root[并查集、dfs][难]

    1021 Deepest Root (25)(25 分) A graph which is connected and acyclic can be considered a tree. The he ...

随机推荐

  1. 【Mysql】MySQL event 计划任务

    一.查看event是否开启 show variables like '%sche%'; set global event_scheduler =1; 二. -- 设置时区并设置计划事件调度器开启,也可 ...

  2. javascript如何处理很多数据,类似分页切换

    需求:一个用户列表数据,如果对应列表数据大于10个,就每10个保存到二维数组,后面不足10个的依然放在二维数组尾部 用处:模拟分页,或者局部刷新 在线DEMO:戳这里 var obj=[ { &quo ...

  3. Codeforces672D(SummerTrainingDay01-I)

    D. Robin Hood time limit per test:1 second memory limit per test:256 megabytes input:standard input ...

  4. Python全栈学习_day001作业

    Day1作业及默写 1.简述变量命名规范 1. 必须以字母.数字.下划线命名,且不能以数字开头 2. 不能是python的关键字 3. 不能以中文或者拼音作为变量名 4. 命名格式推荐以驼峰式或者下划 ...

  5. 【工具相关】Web-XAMPP的安装

    一,在查度中搜索XAMPP,会如下所示,下载软件 ,依次进行安装. 二,安装完成之后,会生成如下的图标. 三,打开上图的图标,如下图所示. 四,选择Apache Web Server---->S ...

  6. RNN,LSTM中如何使用TimeDistributed包装层,代码示例

    本文介绍了LSTM网络中的TimeDistributed包装层,代码演示了具有TimeDistributed层的LSTM网络配置方法. 演示了一对一,多对一,多对多,三种不同的预测方法如何配置. 在对 ...

  7. WebLogic 11g的安装与配置详谈配置详谈

     之前以weblogic8.1为例介绍了其具体安装,但是由于现在weblogic 11g毕竟使用越来越广泛 ,因此,下面将介绍weblogic 11g的具体安装以及配置: 一.安装步骤(基本跟之前we ...

  8. Android RecycleView 自定义Item的使用

    自定义布局的RecycleView需要自己实现Adapter,ViewHolder和布局: 自定义Adapter继承RecycleView.Adapter,重写getItemCount(),onBin ...

  9. EF 排序扩展

    public static class LinqOrderEx { private static IOrderedQueryable<T> OrderingHelper<T>( ...

  10. zabbix-Get value from agent failed: cannot connect to [[127.0.0.1]:10050]: [111] Connection refused

    监控zabbix服务端这台服务器,然后显示Get value from agent failed: cannot connect to [[127.0.0.1]:10050]: [111] Conne ...