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. OpenCV VideoCapture.get()参数详解

    转自https://blog.csdn.net/u011436429/article/details/80604590 方便查阅

  2. 对于vector中高效删除中间元素的技巧

    众所周知,vector是连续存储空间,只提供高效的尾部删除方法pop_back() ,在中间删除的效率很低,那么如果大家想快速删除中间元素该如何实现? 话不多说,看代码: //移除vector元素,最 ...

  3. 三种方法给Vmware虚拟机占用空间清理瘦身

    随着VMware虚拟机使用时间的增长,其所占用的空间也越来越大,本文来说说怎么给VMware虚拟机占用的空间进行瘦身. 方法一:VMware自带的清理磁盘这个方法是VMware自带,具有普适性,对快照 ...

  4. tomcat web的URL解析(web.xml)

    1.一个tomcat可以配置多个host: 2.一个host可以包含多个应用:context: 3.一个应用可以包含多个servlet:servlet-path; 4.一个servlet可以包含多个r ...

  5. Java并发包--LinkedBlockQueue

    转载请注明出处:http://www.cnblogs.com/skywang12345/p/3503458.html LinkedBlockingQueue介绍 LinkedBlockingQueue ...

  6. [Google Guava] 1.5-Throwables:简化异常和错误的传播与检查

    原文链接 译者: 沈义扬 异常传播 有时候,你会想把捕获到的异常再次抛出.这种情况通常发生在Error或RuntimeException被捕获的时候,你没想捕获它们,但是声明捕获Throwable和E ...

  7. HDU 6134 Battlestation Operational | 2017 Multi-University Training Contest 8

    破结论没听说过,上式推导到第三步的时候有了O(nlogn) 的做法(枚举倍数+1最后前缀和),并且这种做法可以直接应用到向上取整的计算中,详见forever97 但由于d(n)是积性函数,故可O(n) ...

  8. yii行为和过滤器

    行为是对类的功能进行了扩展,针对开闭原则,为了类的扩展而生,不去修改类原有的代码. yii的行为需要继承yii\base\Behavior,这就好比你要给人安装一个胳膊,这个胳膊得是人的,而不能是老虎 ...

  9. Linux文件删除的原理

    Linux文件iNode和block是否删除是通过  i_link  和 i_count 的计数值来判断的.只有i_count  和 I_link 同时为0 的 时候,文件的 iNode和block才 ...

  10. 顺序表Vector

    程序中会使用数据结构:例如:顺序表.链表.二叉树: 数据结构在底层中本质上只有两种:数据之间挨着和不挨着:   1.关于Vector