PAT1021. Deepest Root (25)
之前不知道怎么判断是不是树,参考了
http://blog.csdn.net/eli850934234/article/details/8926263
但是最后有一个测试点有超时,在bfs里我用了数组拷贝,所以简化成直接访问超时就没有了。
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
int bing[10005];
int n;
int bfs(int node);
vector<int> nodeList[10005];
vector<int> maxDis;
int vis[10005];
int unionFind(int a){
if(bing[a]==0) return a;
else return unionFind(bing[a]);
}
void unionUnion(int a,int b){
int ia=unionFind(a);
int ib=unionFind(b);
if(ia!=ib) bing[ia]=ib;
}
int bfs(int node){
memset(vis,0,sizeof(vis));
queue<int> q;
q.push(node);
int co=1;
int co2=0;
int depth=0;
while(!q.empty()){
int fro=q.front();
vis[fro]=1;
q.pop();
for(int k=0;k<nodeList[fro].size();k++){
if(vis[nodeList[fro][k]]==0){
q.push(nodeList[fro][k]);
co2++;
}
}
co--;
if(co==0) {
co=co2;
co2=0;
depth++;
}
}
return depth;
} int main(){
cin>>n;
int i,j;
for(int m=0;m<n-1;m++){
cin>>i>>j;
unionUnion(i,j);
nodeList[i].push_back(j);
nodeList[j].push_back(i);
}
int count=0;
for(int m=1;m<=n;m++){
if(bing[m]==0) count++;
}
if(count!=1){
printf("Error: %d components",count);
return 0;
}
int max=0;
for(int m=1;m<=n;m++){
int dis=bfs(m);
if(max<dis){
maxDis.clear();
max=dis;
maxDis.push_back(m);
}else if(max==dis){
maxDis.push_back(m);
}
}
for(int m=0;m<(maxDis.size());m++){
cout<<maxDis[m]<<endl;
}
return 0;
}
PAT1021. Deepest Root (25)的更多相关文章
- PAT-1021 Deepest Root (25 分) 并查集判断成环和联通+求树的深度
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
- PAT1021:Deepest Root
1021. Deepest Root (25) 时间限制 1500 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A graph ...
- [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 ...
- PAT 甲级 1021 Deepest Root (25 分)(bfs求树高,又可能存在part数part>2的情况)
1021 Deepest Root (25 分) A graph which is connected and acyclic can be considered a tree. The heig ...
- 1021. Deepest Root (25) -并查集判树 -BFS求深度
题目如下: A graph which is connected and acyclic can be considered a tree. The height of the tree depend ...
- 1021. Deepest Root (25)
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
- 1021 Deepest Root (25)(25 point(s))
problem A graph which is connected and acyclic can be considered a tree. The height of the tree depe ...
- 1021. Deepest Root (25)——DFS+并查集
http://pat.zju.edu.cn/contests/pat-a-practise/1021 无环连通图也可以视为一棵树,选定图中任意一点作为根,如果这时候整个树的深度最大,则称其为 deep ...
- 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 ...
随机推荐
- 如何导入和导出应用数据通过电子邮件应用程序在你的IOS
本文转载至 http://blog.csdn.net/zaitianaoxiang/article/details/6657887 http://yueding920.blog.163.com/bl ...
- CodeIgniter框架——知识要点汇总
NO1.学习要点: 一.CodeIgniter 框架的简介 二.CodeIgniter 框架的安装 三.CodeIgniter 框架的目录结构分析 四.CodeIgniter 框架是如何工作的? 五. ...
- 《从零开始学Swift》学习笔记(Day 15)——请注意数字类型之间的转换
原创文章,欢迎转载.转载请注明:关东升的博客 在C.Objective-C和Java等其他语言中,整型之间有两种转换方法: 从小范围数到大范围数转换是自动的: 从大范围数到小范围数需要强制类型转换,有 ...
- JQuery处理DOM元素
现有一个id为txtMyTest的元素 获取属性值 $('#id').attr('属性名'); 设置属性值 $('#id').attr('属性名','需要设置的值'); 设置多个属性 如下同时设置va ...
- 初步认识dubbo--小案例
Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按照分层的方式来架构,使用这种方式可以使各个层之间解耦合(或者最大限度地松耦合).从服务模型的角度来看,Dubbo采用的是一种非常简单的模 ...
- php-fpm 启动 关闭 进程逃逸 pid
正常关闭失败 [root@d personas]# /etc/init.d/php-fpm stopGracefully shutting down php-fpm /etc/init.d/php-f ...
- Python星号*与**用法分析 What does ** (double star/asterisk) and * (star/asterisk) do for parameters? 必选参数 默认参数 可变参数 关键字参数
python中*号**的区别 - CSDN博客 https://blog.csdn.net/qq_26815677/article/details/78091452 定义可变参数和定义 list 或 ...
- mapdb与Redis区别:
Redis也能完全相同的工作,但必竟其独立于JVM之外通过Socket交互,能达到10万次/秒就很不了不起了 mapdb可直接嵌入到JVM运行空间,运行效率是Redis没法比的,单线程能达到 30万次 ...
- HackerRank - fibonacci-modified 【大数】
思路 用PYTHON 或 JAVA 干掉 AC代码 a, b, n = map(int, input().split()) for i in range (2, n, 1) : temp = b b ...
- Python学习进程(10)字典
本节介绍Python中的字典:是另一种可变容器模型,且可存储任意类型对象. (1)字典简介: 字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割 ...