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 ...
随机推荐
- hdu1754(splay tree 单点更新,成段查询)
题意就是简单的点更新,成段查询. splay tree 果真是常数比较大的log(n)操作. 比线段树还慢了这么多. // // main.cpp // splay // // Created by ...
- [Spring MVC]学习笔记--form表单标签的使用
github例子地址: https://github.com/lemonbar/spring-mvc-jsp 效果图 关于spring mvc的标签的讲解, 有一篇blog已经讲的很细了. http: ...
- [Spring Data MongoDB]学习笔记--MongoTemplate查询操作
查询操作主要用到两个类:Query, Criteria 所有的find方法都需要一个query的object. 1. 直接通过json来查找,不过这种方式在代码中是不推荐的. BasicQuery q ...
- 关于jquery的取消阻止默认事件
最近帮朋友做个东西,整个屏幕有一个遮罩层,但是这个遮罩层能滑动,于是百度了一下,返现,用 $('body').bind("touchmove",function(e){ e.pre ...
- 初识Python、数据类型、基本语句
Python是一种面向对象的.直译式计算机程序设计语言,也是一种功能强大的且完善的通用型语言,是一种脚本语言. Python的主要特点:是面向过程的.面向对象的:模块丰富,功能强大:语法简单易学:扩展 ...
- ehcache.xml配置详解
一:配置文件案例 <ehcache> <!-- 磁盘存储:将缓存中暂时不使用的对象,转移到硬盘,类似于Windows系统的虚拟内存 path:指定在硬盘上存储对象的路径 --> ...
- css选择器的权重
权重会叠加!
- ffmpeg参数使用说明1
1. ffmpeg.exe -i F:闪客之家闪客之歌.mp3 -ab 56 -ar 22050 -b 500 -r 15 -s 320x240 f:11.flv 2. ffmpeg ...
- Android系统移植与调试之------->增加一个双击物理按键打开和关闭闪光灯并将闪光灯状态同步到下拉菜单中
最近有一个客户有这样的需求: 1.在[设置]--->[无障碍]中添加一个开关按钮. 如果打开开关的话,双击某个物理按键的时候,打开闪光灯,再双击该物理按键的时候,关闭闪光灯. 如果关闭开关的话, ...
- Java语言实现简单FTP软件------>FTP软件主界面的实现(四)
首先看一下该软件的整体代码框架 1.首先介绍程序的主入口FTPMain.java,采用了一个漂亮的外观风格 package com.oyp.ftp; im ...