1021 Deepest Root (25 分)
 

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的情况)的更多相关文章

  1. PAT 甲级 1021 Deepest Root (并查集,树的遍历)

    1021. Deepest Root (25) 时间限制 1500 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A graph ...

  2. PAT甲级1021. Deepest Root

    PAT甲级1021. Deepest Root 题意: 连接和非循环的图可以被认为是一棵树.树的高度取决于所选的根.现在你应该找到导致最高树的根.这样的根称为最深根. 输入规格: 每个输入文件包含一个 ...

  3. PAT 甲级 1021 Deepest Root

    https://pintia.cn/problem-sets/994805342720868352/problems/994805482919673856 A graph which is conne ...

  4. 1021 Deepest Root (25 分)

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

  5. 【PAT甲级】1021 Deepest Root (25 分)(暴力,DFS)

    题意: 输入一个正整数N(N<=10000),然后输入N-1条边,求使得这棵树深度最大的根节点,递增序输出.如果不是一棵树,输出这张图有几个部分. trick: 时间比较充裕数据可能也不是很极限 ...

  6. [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 ...

  7. 1021. Deepest Root (25) -并查集判树 -BFS求深度

    题目如下: A graph which is connected and acyclic can be considered a tree. The height of the tree depend ...

  8. 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 ...

  9. 1021. Deepest Root (25)——DFS+并查集

    http://pat.zju.edu.cn/contests/pat-a-practise/1021 无环连通图也可以视为一棵树,选定图中任意一点作为根,如果这时候整个树的深度最大,则称其为 deep ...

随机推荐

  1. CrawlSpider ---> 通用爬虫 项目流程

    通用爬虫 通用网络爬虫 从互联网中搜集网页,采集信息,这些网页信息用于为搜索引擎建立索引从而提供支持,它决定着整个引擎系统的内容是否丰富,信息是否即时,因此其性能的优劣直接影响着搜索引擎的效果. 不扯 ...

  2. Ubuntu系统---系统驱动丢失、Kernel内核卸载、禁止更新

    Ubuntu系统---系统驱动丢失.Kernel内核卸载.禁止更新 一早开机发现,ubuntu字体异常,字体很大,直接反应是驱动坏了.一查,确实丢失英伟达驱动,为什么呢?莫名的消失.想知道:1.英伟达 ...

  3. hbase实践之数据读取详解

    hbase基本存储组织结构与数据读取组织结构对比 Segment是Hbase2.0的概念,MemStore由一个可写的Segment,以及一个或多个不可写的Segments构成.故hbase 1.*版 ...

  4. PageHelper使用中出现的问题

    PageHelper在分页查询的时候功能强大,内部使用拦截器实现.这边文章做了详细的介绍. https://www.cnblogs.com/ljdblog/p/6725094.html https:/ ...

  5. 在多语句事务内不允许使用 CREATE DATABASE 语句。

    方法一:create database [ 项目名称] 方法二:update-database -verbose

  6. 2019-2020 ICPC, NERC, Southern and Volga Russian Regional Contest (Online Mirror, ICPC Rules, Teams Preferred)【A题 类型好题】

    A. Berstagram Polycarp recently signed up to a new social network Berstagram. He immediately publish ...

  7. 【算法进阶-康托展开】-C++

    目录 引入 这位老爷子就是康托 基本概念 康托展开是一个全排列到一个自然数的双射,常用于构建hash表时的空间压缩.设有n个数(1,2,3,4,-,n),可以有组成不同(n!种)的排列组合,康托展开表 ...

  8. Flutter布局3-----Center

    Center 居中的布局 只能有一个chlid,但是可以用container包含好多子child,继承自Align. 用于将其子项与其自身对齐,并根据子级的大小自行调整大小. 如果它的尺寸受到约束且[ ...

  9. springboot项目:以run as-->spring boot app方式启动,配置热部署(亲测可用!!!)

    1.在pom.xml中添加热部署依赖 <!-- 热部署 --> <!-- devtools可以实现页面热部署(即页面修改后会立即生效, 这个可以直接在application.prop ...

  10. 牛客 17439:Endless Pallet

    题目传送门 算法:min-max 容斥.树上背包.NTT. 题意简述 有一棵 \(n\) 个点的树.一开始所有点都是白色,每次操作会随机选择 \(\frac{n \times (n + 1)}{2}\ ...