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 ...
随机推荐
- ADODB
转自网友,看着挺全就转了,供大家学习研究. Recordset 对象的属性1.CursorType 属性AdOpenForwardOnly: 仅向前游标,默认值.除了只能在记录中向前滚动外,与静态游标 ...
- Linux 进程间通信(一)(经典IPC:消息队列、信号量、共享存储)
有3种称作XSI IPC的IPC:消息队列.信号量.共享存储.这种类型的IPC有如下共同的特性. 每个内核中的IPC都用一个非负整数标志.标识符是IPC对象的内部名称,为了使多个合作进程能够在同一IP ...
- Python菜鸟之路:DOM基础
前言 DOM 是 Document Object Model(文档对象模型)的缩写,定义了访问和操作 HTML 文档的标准方法.DOM把网页和脚本以及其他的编程语言联系了起来.DOM属于浏览器,而不是 ...
- CRM客户关系管理系统-需求概设和详设
大概设计 大概设计就是对需求进行一个整体性分析,把需要实现的功能都列出来,对于客户关系管理系统,我们需要从角色出发,从而确定有哪些需求,最好是画个思维导图 首先我们是为培训学校这么一个场景来开发的,所 ...
- 洛谷 P3393 逃离僵尸岛
洛谷 这道题目其实是最短路裸题. 首先看到题目,要求的到"被占点"距离不大于S的点,自然想到了以"被占点"为源点,求一遍最短路,处理出"危险点&quo ...
- Django的admin样式丢失【静态文件收集】
在部署完Django项目后,进行admin后台登录发现样式丢失,后台日志显示:js和css文件丢失 解决办法: 配置settings.py如下: #DEBUG打开时,app的静态文件默认从这里读取 S ...
- Linux查找命令find、locate、whereis、which、type
find:查找指定下目录的文件 -empty:查找空文件 -name:按名字查找 -type f(文件 or d:目录):按类型查找
- ORACLE中RECORD、VARRAY、TABLE的使用具体解释
1 说明 1.1 RECORD 定义记录数据类型. 它类似于C语言中的结构数据类型(STRUCTURE).PL/SQL提供了将几个相关的.分离的.基本数据类型的变量组成一个总 ...
- Ubuntu16.04安装Chrome浏览器及解决root不能打开的问题
1. 安装桌面(emmm,不知道是否只执行第二个命令就行) # apt-get install gonme# apt-get install ubuntu-desktop2. 安装Chrome浏览器 ...
- random模块(随机数库)
random random.random random.random()用于生成一个0到1的随机浮点数: 0 <= n < 1.0 random.uniform random.unifor ...