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. jquery全选或不全选时,不操作已经禁用的checkbox

    $("#selectAll").click(function(){ if(this.checked ){ $(":checkbox[name='equid']" ...

  2. elasticsearch安装之各种坑

    我用的是centos6.5,安装elasticsearch5.2.0 首先不说了,安装JDK1.8,下载elasticsearch5.2.0 https://www.elastic.co/downlo ...

  3. 【代码笔记】Web-HTML-框架

    一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  4. [VUE ERROR] Invalid options in vue.config.js: "publicPath" is not allowed

    项目在build的时候报的这个错误: 具体原因是因为版本支持的问题,publicPath 属性到 vue-cli 3.2.0 之后才支持,所以将 publicPaht 改成 baseUrl 即可,或者 ...

  5. Linux 学习笔记之超详细基础linux命令 Part 14

    Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 13---------------- ...

  6. MagicApp说明

    title: MagicApp说明 date: 2017-12-06 05:41:00 tags: IT 技术 MagicApp是日常处理的程序,协助进行日常工作处理 批量重命名模块 说明 该模块是根 ...

  7. CSS| 框模型-border

    CSS 边框属性

  8. Flutter在Windows平台下的安装配置

    目录 1. 安装 Flutter SDK2. 设置环境变量3. Flutter doctor4. 安装 Android Studio5. 启动 Android Studio, 安装 Android S ...

  9. Shell脚本应用(if语句的结构)

    1.测试:检测表达式是否成立,成立则返回值为0,否则为非0 方法: 1)test  表达式 2)[ 表达式 ] 2.文件测试: -d:是否为目录 -f:是否为文件 -e:是否存在 -r:是否有读取权限 ...

  10. tkinter学习系列之(五)Checkbutton控件

    目录 目录 前言 (一)基本属性 (二)案例 1.简单的复选框 2.组合复选框 目录 前言 复选框:可以同时多选的一组框,其只有两种状态,选中与未选中. (一)基本属性 (1)说明: tkinter里 ...