1021 Deepest Root (25 分) 【难度: 中 / 知识点: 树的直径 连通块】
https://pintia.cn/problem-sets/994805342720868352/problems/994805482919673856
方法一:
- 数组模拟邻接表
- 第一步: 爆搜dfs求连通块
- 第二步: 暴力枚举每一个点求其最远的距离
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int h[N],e[N],ne[N],idx,n;
int st[N],cnt;
void add(int a,int b)
{e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
void dfs(int u)//求连通块
{st[u]=1;for(int i=h[u];i!=-1;i=ne[i]){int j=e[i]; if(!st[j]) dfs(j);}
}
int dfs1(int u,int father)// u当前的点 father从哪里来的
{int depth=0;for(int i=h[u];i!=-1;i=ne[i]){int j=e[i];if(j==father) continue;//走回头路了depth=max(depth,dfs1(j,u)+1); }return depth;//该点的最大深度
}
int main(void)
{cin>>n;memset(h,-1,sizeof h);for(int i=0;i<n-1;i++){int a,b; cin>>a>>b;add(a,b),add(b,a);}for(int i=1;i<=n;i++) if(!st[i]) dfs(i),cnt++;//求联通块的个数if(cnt==1){vector<int>ve;int max_len=0;//最深的深度for(int i=1;i<=n;i++){int len=dfs1(i,-1);if(len>max_len)//有更深的{max_len=len;ve.clear();ve.push_back(i);}else if(len==max_len) ve.push_back(i);//并列}for(int i=0;i<ve.size();i++) cout<<ve[i]<<endl;}else printf("Error: %d components",cnt);return 0;
}
方法二:
- vector存邻接表
- 第一步: 并查集求连通块
- 第二步: 暴力枚举每一个点求其最远的距离
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
vector<int>ve[N];
int p[N],n;
int find(int x)
{if(x!=p[x]) p[x]=find(p[x]);return p[x];
}
int dfs(int u,int father)
{int depth=0;for(int i=0;i<ve[u].size();i++){if(ve[u][i]==father) continue;depth=max(depth,dfs(ve[u][i],u)+1);}return depth;
}
int main(void)
{cin>>n; int cnt=n;//初始每一个点都是一个连通块。for(int i=1;i<=n;i++) p[i]=i;for(int i=0;i<n-1;i++){int a,b; scanf("%d%d",&a,&b);ve[a].push_back(b);ve[b].push_back(a);if(find(a)!=find(b))//合并{p[find(a)]=find(b);cnt--;//合并之后连通块的数量减一}}if(cnt>1) printf("Error: %d components",cnt);else {vector<int>ans;int max_len=0;for(int i=1;i<=n;i++){int len=dfs(i,-1);if(len>max_len){max_len=len;ans.clear();ans.push_back(i);}else if(len==max_len) ans.push_back(i);}for(int i=0;i<ans.size();i++) printf("%d\n",ans[i]);}return 0;
}
时间上的比较: 第一种比第二种快了[100,300]ms
如果第一种也用并查集时间会更快。第二种的vector的时间花费有点大
1021 Deepest Root (25 分) 【难度: 中 / 知识点: 树的直径 连通块】相关推荐
- 【分析】1021 Deepest Root (25 分)【DFS解法】
立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 A graph which is connected and acyclic can be considered a tree. ...
- 【PAT甲级】1021 Deepest Root (25 分)(暴力,DFS)
题意: 输入一个正整数N(N<=10000),然后输入N-1条边,求使得这棵树深度最大的根节点,递增序输出.如果不是一棵树,输出这张图有几个部分. trick: 时间比较充裕数据可能也不是很极限 ...
- 1021. Deepest Root (25)
题目链接:http://www.patest.cn/contests/pat-a-practise/1021 题目: 1021. Deepest Root (25) 时间限制 1500 ms 内存限制 ...
- 浙大PAT 1021. Deepest Root (25)
1021. Deepest Root (25) 时间限制 1500 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A graph ...
- 【PAT - 甲级1021】Deepest Root (25分)(并查集,暴力枚举)
题干: A graph which is connected and acyclic can be considered a tree. The height of the tree depends ...
- PTA Deepest Root (25分)
释放无限光明的是人心,制造无边黑暗的也是人心,光明和黑暗交织着,厮杀着,这就是我们为之眷恋又万般无奈的人世间. A graph which is connected and acyclic can b ...
- 1057 Stack (30 分)【难度: 中 / 知识点: 树状数组 STL】
https://pintia.cn/problem-sets/994805342720868352/problems/994805417945710592 本题的一个最大的难点,就是如何给一个动态的区 ...
- 1004 Counting Leaves (30 分)【难度: 中 / 知识点: 树的遍历】
题目意思: 求每一层的叶子结点数 方法一: 深搜 数组模拟存储邻接表 #include<bits/stdc++.h> using namespace std; const int N=1e ...
- PAT甲级-1021 Deepest Root(25分)
题目:1021 Deepest Root(25分) 分析:找出以某个节点为根时,最深的根,这题可能会超时要用vector来表示二维数组 疑问:代码一是第二次写的超时了,代码二是第一次写的AC了,找不出 ...
最新文章
- python代码写完怎么运行-教你如何编写、保存与运行 Python 程序
- 视觉与图像系列 几何光学I 近轴光学1 Fermat原理
- python的xlutils模块_xlutils模块使用
- 如何将10元店,做到月2000万流水?
- Django环境安装和创建工程
- uvm_reg_defines——寄存器模型(四)
- Starling粒子系统工具
- 独家!币安被盗原因找到了!7074 枚比特币竟是这样丢掉的
- Linux高性能server规划——多线程编程(在)
- [渝粤教育] 西南科技大学 经济法概论 在线考试复习资料2021版
- 安装 composer 并启动 yii2 项目
- Queue接口分析:add和offer区别,remove和poll方法到底啥区别
- 七参数/六参数坐标转换(小角度)-- 公共点间接平差
- 网易云信技术创新,助力网易云音乐社交玩法升级
- 利用XML制作UGUI登陆界面
- 刀片服务器的机箱显示器,思科 UCS 5100系列刀片服务器机箱
- Windows11打开任务管理器的方式
- Ffmpeg中的文件格式
- 静态成员和静态成员函数的总结
- 苹果商城怎么调成中文_苹果手机没声音?苹果专业维修帮你