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 ...
随机推荐
- Spring基于注解和XML混合方式的使用
首先要明白,基于注解和XML两种方式的实现功能是一样的,只是两种不同的配置方式. 一.IoC配置 1.配置xml 在使用注解与xml结合的方式配置IoC之前,首先要引入context标签: xmlns ...
- HTML5 template元素
前言 转自http://www.zhangxinxu.com/wordpress/2014/07/hello-html5-template-tag/ 在单页面应用,我们对页面的无刷新有了更高的要求,H ...
- 如何判断页面是pc端还是移动端,进入不同的页面
vue判断是pc端还是移动端分别进入不同的页面 判断移动端代码如下: function IsPC(){ var userAgentInfo = navigator.userAgent; var Age ...
- blfs(systemv版本)学习笔记-编译安装sudo并创建普通用户配置sudo权限
我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! blfs书中sudo的安装配置章节:http://www.linuxfromscratch.org/blfs/view/8.3/ ...
- 洛谷P3987 我永远喜欢珂朵莉~(set 树状数组)
题意 题目链接 Sol 不会卡常,自愧不如.下面的代码只有66分.我实在懒得手写平衡树了.. 思路比较直观:拿个set维护每个数出现的位置,再写个线段树维护区间和 #include<bits/s ...
- 【读书笔记】iOS-微定位技术
在大型商场,医院或是大楼里,你是否曾经有过找不到想去的地方的经历呢?这种情况下采用传统的定位方法就有些力不从心了.首先这些地方不能采用GPS定们,而Wifi和蜂窝式移动电话基站定位误差比较大.这种情况 ...
- 安卓开发_WebView如何在Fragment中使用
之前学习了如何在activity中使用WebView控件来显示网页. 在我的实际开发中,有需要在Fragment中用到WebView控件的,那么就百度学习了一下 其实很简单,但是当然不是和在Activ ...
- Android事件总线(二)EventBus3.0源码解析
1.构造函数 当我们要调用EventBus的功能时,比如注册或者发送事件,总会调用EventBus.getDefault()来获取EventBus实例: public static EventBus ...
- android recovery 升级UI显示之资源文件
Recovery只有在升级的时候才会呈现给用户,所以界面一般都很简单,没有android上层那么绚丽,所以recovery下面对图片的支持很有限,仅支持png图片显示,所以我们可以看到,recover ...
- 测试中Android与IOS分别关注的点
主要从本身系统的不同点.系统造成的不同点.和注意的测试点做总结 1.自身不同点 研发商:Adroid是google公司做的手机系统,IOS是苹果公司做的手机系统 开源程度:Android是开源的,IO ...