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 ...
随机推荐
- C# 开发的windows服务 不能调试——讨论整理
CSDN的标题:C# 开发的windows服务 不能调试 System.Diagnostics.Debugger.Launch();在想加断点的地方加入这行,是进入断点的,可以进行调试,我的是xp系统 ...
- Android.mk走读与Cmake配置
Android.mk认识: 在上一次[https://www.cnblogs.com/webor2006/p/9946061.html]中学会了用NDK提供的交叉编译工程编译成Android能运行的可 ...
- PHP返回JSON数据及中文编码问题的解决方案
在处理app接口的时候 ,中文在经过json_encode之后 变成\ \格式 想在返回接口的时候 中文不被转换 解决办法 第一种解决办法 exit(json_encode($result,JSON ...
- 0010Springboot整合thymeleaf
1.pom.xml中添加thymeleaf的起步依赖 2.编写html文件并放在classpath:/templates/路径下 3.编写controller并返回字符串,会到classpath:/t ...
- 再论i++ ++i
#include <stdio.h> int main(void) { char acData[5] ={'A','B','C','D','E'}; char *pcData = NULL ...
- Mysql批量更新的一个坑-&allowMultiQueries=true允许批量更新(转)
实际上,我们经常会遇到这样的需求,那就是利用Mybatis批量更新或者批量插入,但是,实际上即使Mybatis完美支持你的sql,你也得看看你说操作的数据库是否支持,而阿福,最近就遇到这样的一个坑. ...
- winfrom 窗体首次加载
#region Override Functions /// <summary> /// OnLoad /// </summary> /// <param name=&q ...
- P3474 [POI2008]KUP-Plot purchase
思路:单调栈 提交:>5次 错因:单调栈写法有问题+前缀和写错 题解: 若有\(>=k\ \&\&\ <=2\times k\)的点,显然直接选他就行了. 否则,我们 ...
- jenkins 邮件发送错误
jenkins 在创建新的 Build 的时候希望邮件进行通知. 但是邮件通知的时候出现错误: Unable to Send Mail - javax.net.ssl.SSLException: Un ...
- [Luogu] 八数码难题
https://www.luogu.org/problemnew/show/P1379 long long ago 暴力bfs #include <iostream> #include & ...