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 ...
随机推荐
- M²的经典语录
1. If you failed, stop and think! You should work in the correct way. 2. If I can do all of it, why ...
- EF 批量更新删除(linq篇)
刚开始用EF很多东西都不会用,事后想想都很简单的东西总是用很麻烦的方式实现 1: EF的联合查询 inner join 很久很久以前我是这么写一个列表展示的,其中有两个字段Contractor和M ...
- I - Doing Homework again(贪心)
I - Doing Homework again Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & ...
- Vue中浏览器的的前进和后退
项目开发的时候,有时候可能需要我们来对页面后退和前进,这个东西跟浏览器自带的前进后退功能很像,下面来大致讲一下在vue中浏览器的前进和后退 一.后退功能 vue中的后退有好多种方法可以使用,使用这些方 ...
- [NOIP2018TG]旅行
[NOIP2018TG]旅行 树很简单,对每个点sort儿子,贪心走就行了 基环树呢? 如果是1e5可能不太好做 但是5000的话枚举断边就可以\(n^2\)了 #include<bits/st ...
- 阿里云短信验证解决方案(java版)(redis存储)
最近搞了一个互联网项目的注册,需要写一个手机号验证(由于之前没有轮子,只能自己摸索了); 1:基本思路: 1>购买了阿里云短信服务->下载阿里云短信发送demo(java版); 2> ...
- 开机提示memory size decreased怎么办?
ytkah的办公电脑实在是hold不住,win7用2G的内存,基本是在等待中度过的,没有新的只能考虑加格内存了,采购嘛,需走一大堆流程,跟领导请示从其他硬件需求不是很高的电脑那边拔一根内存条吧,小C的 ...
- Hexo+yilia添加helper-live2d插件宠物动画,很好玩的哦~~
个人主页:https://www.yuehan.online 现在博客:www.wangyurui.top 安装模块: 博客根目录选择cmd命令窗口或者git bash 输入以下代码,安装插件 操作: ...
- ACM解题之(ZOJ 2724)Windows Message Queue
题目来源: 点击打开链接 题目翻译: 消息队列是windows系统的基本基础.对于每个进程,系统都维护一个消息队列.如果这个过程发生某些事情,例如鼠标点击,文本改变,系统会向队列添加一条消息.同时,如 ...
- Guide to Spring @Autowired
Guide to Spring @Autowired Spring希望由@Autowired注解的依赖在某个依赖bean被构造时是可以访问的.如果框架不能解析这个用于wiring的bean,就会抛出异 ...