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 ...
随机推荐
- 如何利用while语句打印“九九乘法口诀表”
需求:输出九九乘法表 plus.py代码如下: i=1 j=1 while i<=9: j=1 while j<=i: print(j,'*',i,'=',str(i*j)+' ',end ...
- PAT Advanced 1108 Finding Average (20 分)
The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...
- SQL Join 理解
对各种连接的理解,可以参照文章. 下面是对连接结果表条数统计的思考:假设有主表Ta有5条记录,从表Tb有4条记录 Ta corss join Tb, 结果为2表做笛卡尔积,5*4=20条 /*下面其它 ...
- linux加载字体
将解压后的文件夹cp到/usr/share/fonts目录下,然后cd到/usr/share/fonts/ziti目录下执行:mkfontscalemkfontdirfc-cache 在linux,把 ...
- Python与数据库 sqlalchemy 建立声明层表对象的两种方式
在对表对象进行建立的时候,通常有两种方式可以完成,以下是两种方式的建立过程对比 首先导入需要的模块,获取一个声明层 1 from sqlalchemy.sql.schema import Table, ...
- Java中String为什么是不可变
什么是不可变对象? 众所周知, 在Java中, String类是不可变的.那么到底什么是不可变的对象呢? 可以这样认为:如果一个对象,在它创建完成之后,不能再改变它的状态,那么这个对象就是不可变的.不 ...
- FastDFS+Nginx+Module
1.安装libevent wget https://cloud.github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.g ...
- C# 通过浏览器打开指定url
using System.Diagnostics; var resultsg = Process.Start(@"C:\Users\Administrator\AppData\Local\S ...
- HDU 6129 - Just do it | 2017 Multi-University Training Contest 7
比赛时脑子一直想着按位卷积... 按题解的思路: /* HDU 6129 - Just do it [ 规律,组合数 ] | 2017 Multi-University Training Contes ...
- Girls and Boys POJ - 1466 【(二分图最大独立集)】
Problem DescriptionIn the second year of the university somebody started a study on the romantic rel ...