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. 【协议】4、http状态码

    10.4 客户错误 4xx 状态代码4xx类是专门使用在客户看上去错误的情形下的.除非当应答一个HEAD请求时,服务器因该有一个包括错误情形的解释的实体,以及它是否一个临时或者终了的条件.这些状态编码 ...

  2. Flask wtforms实现简单的登录注册

    目录结构 视图 # -*- coding:utf-8 -*- # Author : Niuli # Data : 2019-02-27 19:26 from flask import render_t ...

  3. JS 中的 __proto__ 、prototype、constructor

    首先 先解释这三个属性: (1) prototype  : 它是函数独有的,从一个函数指向一个对象(函数的原型),含义是函数的原型对象,也就是这个函数所创建的实例的原型对象.(普通函数的该属性没有作用 ...

  4. cf1130E. Wrong Answer(构造)

    题意 题目链接 Sol 对构造一无所知... 题解的方法比较神仙,,设第一个位置为\(-1\),\(S = \sum_{i=1}^n a_i\) 那么我们要让\(N * S - (N - 1) * ( ...

  5. npm 全局执行 update 、 outdated 出现 npm-debug.log 404 错误的问题

    想要执行一次全局更新,发现屡次报错: # npm update -g 提示的错误信息包含如下内容: npm ERR! code E404 npm ERR! 404 Registry returned ...

  6. 【读书笔记】iOS-工作区的使用

    一,打开Xcode--->File-->New-->Workspace--->AllProject. 二,打开桌面上的AllProject--->File--->N ...

  7. 两年AI研究经验(教训)总结,进来看看吧!

    https://www.jianshu.com/p/2149c9fee166 摘要: 本文以过来人的身份将自身2年的研究经验做了一下分享,希望本文对于即将开始从事人工智能研究的朋友有所帮助. 人工智能 ...

  8. Linux 操作系统主机名变成bogon怎么解决?

    主机名变成bogon怎么解决? by:授客 QQ:1033553122   一:使用hostname命令 [laiyu@localhost ~]$ hostname localhost.localdo ...

  9. python中get pass用法

    python中getpass 模块的作用是输入密码不可见 运行到这脚本不继续运行下去, 打开pycharm中的terminal 如上图显示,password中有输入密码,但不显示

  10. 常用的Git命令整理

    之前一直忙于项目苦于没有时间总结,今天刚好有时间特来总结一下在工作中常用到的代码版本管理器Git.至于为什么要用Git?Git相比SVN有哪些好处?我就不多说了,前人已经总结的很好.今天主要介绍的是常 ...