PAT 甲级 1021 Deepest Root (25 分)(bfs求树高,又可能存在part数part>2的情况)
A graph which is connected and acyclic can be considered a tree. The height 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 (≤) 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
题意:
给出n个结点和n-1条边,问它们能否形成一棵n个结点的树,如果能,从中选出结点作为树根,使整棵树的高度最大。按升序输出所有满足要求的可以作为树根的结点。 如果不是一棵树,则输出cout<<"Error: "<<part<<" components";
思路:
bfs求高度,如果有多个部分,再bfs数有几个部分,part可能大于2。一开始没考虑,测试点2过不了。
Error: components
AC代码:
#include<bits/stdc++.h>
using namespace std;
int n;
vector<int>v[];
struct node{
int k;//节点的值
int h;//在第几层
};
queue<node>q;
queue<int>ans;
int in[];//在不在队列里
int main(){
cin>>n;
for(int i=;i<=n-;i++){
v[i].clear();
}
while(!q.empty()) q.pop();
while(!ans.empty()) ans.pop();
for(int i=;i<=n-;i++){
int x,y;
cin>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
}
int maxH=;
memset(in,,sizeof(in));
int part=;
for(int i=;i<=n;i++)
{
//以i为树根
int height=;
memset(in,,sizeof(in));
node x;
x.k=i;
x.h=;
q.push(x);
in[i]=;//标记已访问
while(!q.empty())
{
node x=q.front();
q.pop();
height=max(height,x.h);//更新高度
for(int j=;j<v[x.k].size();j++)//遍历与 x.k相连的节点
{
int k1=v[x.k].at(j);
if(in[k1])//被访问过了
{
continue;
}
node y;
y.k=k1;
y.h=x.h+;//新的一层高度+1再放进队列
q.push(y);
in[k1]=;
}
}
//先检查是不是一个块的
for(int j=;j<=n;j++)
{
if(in[j]!=)
{
part=;
break;
}
}
if(!part)
{
break;
}
//cout<<i<<" "<<height<<endl;
//更新高度
if(height>maxH)
{
maxH=height;
while(!ans.empty()) ans.pop();//更新了就清空
ans.push(i);
}else if(height==maxH)
{
ans.push(i);
}
}
if(!part){
part=;
for(int j=;j<=n;j++)
{//bfs数数有多少块
if(in[j]!=)
{
part++;//块数+1
node x;
x.k=j;
x.h=;
q.push(x);
in[j]=;
while(!q.empty())
{
node x=q.front();
q.pop();
for(int p=;p<v[x.k].size();p++)
{
int k1=v[x.k].at(p);
if(in[k1])
{
continue;
}
node y;
y.k=k1;
y.h=x.h+;
q.push(y);
in[k1]=;
}
}
}
}
cout<<"Error: "<<part<<" components";
}else{
while(!ans.empty()){
cout<<ans.front()<<endl;
ans.pop();
}
}
return ;
}
PAT 甲级 1021 Deepest Root (25 分)(bfs求树高,又可能存在part数part>2的情况)的更多相关文章
- PAT 甲级 1021 Deepest Root (并查集,树的遍历)
1021. Deepest Root (25) 时间限制 1500 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A graph ...
- PAT甲级1021. Deepest Root
PAT甲级1021. Deepest Root 题意: 连接和非循环的图可以被认为是一棵树.树的高度取决于所选的根.现在你应该找到导致最高树的根.这样的根称为最深根. 输入规格: 每个输入文件包含一个 ...
- PAT 甲级 1021 Deepest Root
https://pintia.cn/problem-sets/994805342720868352/problems/994805482919673856 A graph which is conne ...
- 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甲级】1021 Deepest Root (25 分)(暴力,DFS)
题意: 输入一个正整数N(N<=10000),然后输入N-1条边,求使得这棵树深度最大的根节点,递增序输出.如果不是一棵树,输出这张图有几个部分. trick: 时间比较充裕数据可能也不是很极限 ...
- [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 ...
- 1021. Deepest Root (25) -并查集判树 -BFS求深度
题目如下: A graph which is connected and acyclic can be considered a tree. The height of the tree depend ...
- 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 ...
- 1021. Deepest Root (25)——DFS+并查集
http://pat.zju.edu.cn/contests/pat-a-practise/1021 无环连通图也可以视为一棵树,选定图中任意一点作为根,如果这时候整个树的深度最大,则称其为 deep ...
随机推荐
- Hosts 长期更新【已停】
修改hosts篇 [2018.1.3] 由于google的对应的hosts更新过于频繁,再加上上次(18+1)大之后,国家政策原因,网上hosts更新基本上都停了,github的项目也陆续挂掉了. 还 ...
- 网页报警提示 This page includes a password or credit card input in a non-secure context. A warning has been added to the URL bar. For more information, see https://goo.gl/zmWq3m.
This page includes a password or credit card input in a non-secure context. A warning has been added ...
- Netty搭建WebSocket服务端
Netty服务端 1.引入依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=& ...
- Java锁--公平锁
转载请注明出处:http://www.cnblogs.com/skywang12345/p/3496147.html 基本概念 本章,我们会讲解“线程获取公平锁”的原理:在讲解之前,需要了解几个基本概 ...
- c语言1博客作业11
一.本周作业头 这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 http://edu.cnblogs.com/campus/zswxy/SE2019-4/homework/10125 我 ...
- spark job分析
spark job spark job提交 三级调度框架, DagSch,计算stage,提交阶段,将stage映射成taskset,提交taskset给tasksch. TaskSch Backen ...
- Nginx location规则匹配
^~ 标识符匹配后面跟-一个字符串.匹配字符串后将停止对后续的正则表达式进行匹配,如location ^~ /images/ , 在匹配了/images/这个字符串后就停止对后续的正则匹配 = 精 ...
- Codeforces Round #584 A. Paint the Numbers
链接: https://codeforces.com/contest/1209/problem/A 题意: You are given a sequence of integers a1,a2,-,a ...
- C语言学习系列(一)开门首篇
一.特辑 此次是我自己的学习之路,和大家一起分享(我现在是做Java),途中遇到什么问题大家也可以提出来一起讨论一起进步: 主要参考教程是菜鸟教程上面的C语言教程,以及大学课本C语言教程-第四版(谭浩 ...
- CodeForces 840A - Leha and Function | Codeforces Round #429 (Div. 1)
/* CodeForces 840A - Leha and Function [ 贪心 ] | Codeforces Round #429 (Div. 1) A越大,B越小,越好 */ #includ ...