1021. Deepest Root (25)——DFS+并查集
http://pat.zju.edu.cn/contests/pat-a-practise/1021
无环连通图也可以视为一棵树,选定图中任意一点作为根,如果这时候整个树的深度最大,则称其为 deepest root。 给定一个图,按升序输出所有 deepest root。如果给定的图有多个连通分量,则输出连通分量的数量。
1.使用并查集判断图是否为连通的。
2.任意选取一点,做 dfs 搜索,选取其中一个最远距离的点 A,再做一次 dfs,找到的所有距离最远的点以及点 A 都是 deepest root。
考虑到为稀疏图,则使用动态链表
#include<stdio.h>
#include<stack>
#include<iostream>
#include<vector>
#include<set>
#include<queue>
using namespace std; vector<int> map[];
int f[];
int find(int k){
if(f[k]==-)return k;
else
return f[k]=find(f[k]);
} int um(int a,int b){
int fa,fb;
fa=find(a);
fb=find(b); if(fa==fb)return ;//表示有环 if(fa!=fb)
f[fa]=fb;
return ;
} int step[]; void dfs(int x,int STEP){ int i;
for(i=;i<map[x].size();i++){
if(step[map[x][i]]!=)continue;
step[map[x][i]]=STEP;
dfs(map[x][i],STEP+);
}
} int main()
{
int n;
while(scanf("%d",&n)!=EOF){
int i,a,b; for(i=;i<=n;i++){
f[i]=-;
step[i]=;
} int huan=;
for(i=;i<n;i++){
scanf("%d%d",&a,&b);
if(um(a,b)==)huan=; map[a].push_back(b);
map[b].push_back(a);
}
int connectAdd=;
set<int>set1;
int ttemp;
for(i=;i<=n;i++){
ttemp=find(i);
set1.insert(ttemp);
} if(set1.size()>=||huan==){
printf("Error: %d components\n",set1.size());continue;
}
step[]=;
dfs(,); int max=,ri;
for(i=;i<=n;i++){
if(max<step[i]){
max=step[i];
ri=i;
}
} for(i=;i<=n;i++){
step[i]=;
}
step[ri]=;
dfs(ri,); max=;
for(i=;i<=n;i++){
if(max<step[i]){
max=step[i];
}
} for(i=;i<=n;i++){
if(step[i]==max||step[i]==){
printf("%d\n",i);
}
}
} return ;
}
其实上面的算法还有点问题,虽然AC了
考虑
5
1 2
1 3
2 4
2 5
应该是输出
3
4
5
算法改进 以(第2次dfs最深的点)为起点,再做DFS得到的最深的点,这些点才是所有最深的点
#include<stdio.h>
#include<stack>
#include<iostream>
#include<vector>
#include<set>
#include<queue>
using namespace std; vector<int> map[];
int f[];
int find(int k){
if(f[k]==-)return k;
else
return f[k]=find(f[k]);
} int um(int a,int b){
int fa,fb;
fa=find(a);
fb=find(b); if(fa==fb)return ;//表示有环 if(fa!=fb)
f[fa]=fb;
return ;
} int step[];
int deepF[];//第2次dfs最深的点
int deepR[];//以(第2次dfs最深的点)为起点,做DFS得到的最深的点 void dfs(int x,int STEP){ int i;
for(i=;i<map[x].size();i++){
if(step[map[x][i]]!=)continue;
step[map[x][i]]=STEP;
dfs(map[x][i],STEP+);
}
} int main()
{
int n;
while(scanf("%d",&n)!=EOF){
int i,a,b,j; for(i=;i<=n;i++){
f[i]=-;
step[i]=;
deepF[i]=;
deepR[i]=;
} int huan=;
for(i=;i<n;i++){
scanf("%d%d",&a,&b);
if(um(a,b)==)huan=; map[a].push_back(b);
map[b].push_back(a);
}
int connectAdd=;
set<int>set1;
int ttemp;
for(i=;i<=n;i++){
ttemp=find(i);
set1.insert(ttemp);
} if(set1.size()>=||huan==){
printf("Error: %d components\n",set1.size());continue;
}
step[]=;
dfs(,); int max=,ri;
for(i=;i<=n;i++){
if(max<step[i]){
max=step[i];
ri=i;
}
} for(i=;i<=n;i++){
step[i]=;
}
step[ri]=;
dfs(ri,); max=;
for(i=;i<=n;i++){
if(max<step[i]){
max=step[i];
}
} for(i=;i<=n;i++){
if(step[i]==max||step[i]==){
//printf("%d\n",i);
deepF[i]=;
deepR[i]=;
}
} for(i=;i<=n;i++){
if(deepF[i]==)continue;
for(j=;j<=n;j++)step[j]=; dfs(i,);
for(j=;j<=n;j++){
if(step[j]==max)deepR[j]=;
}
} for(i=;i<=n;i++){
if(deepR[i]==)printf("%d\n",i);
}
} return ;
}
1021. Deepest Root (25)——DFS+并查集的更多相关文章
- PAT甲题题解-1021. Deepest Root (25)-dfs+并查集
dfs求最大层数并查集求连通个数 #include <iostream> #include <cstdio> #include <algorithm> #inclu ...
- 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 ...
- [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 ...
- PAT (Advanced Level) 1021. Deepest Root (25)
先并查集判断连通性,然后暴力每个点作为根节点判即可. #include<iostream> #include<cstring> #include<cmath> #i ...
- 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 分)
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
随机推荐
- ActiveMQ JMS实现消息发送
一.创建配置消息发送接收目的地. ActiveMQ中间件地址 JMS_BROKER_URL=failover://(tcp://192.168.1.231:61616) QUEUE_BUSP_TP_S ...
- kafka和canal设置为开机启动
1.切换到初始化目录 cd /etc/init.d/ 2.新建一个文件 如 touch autoupdate 3.vim autoupdate #!/bin/bash export JAVA_HOME ...
- apache kafka配置中request.required.acks含义
Kafka producer的ack有3中机制,初始化producer时的producerconfig可以通过配置request.required.acks不同的值来实现. 0:这意味着生产者prod ...
- BZOJ3669/UOJ3 魔法森林(LCT)
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- 全方位解读Java反射(reflection)
JAVA提供了一种反射机制,反射也称为反省. java程序运行以后内存中就是一堆对象,除了对象什么都没有. 找对象 拉关系 瞎折腾 对象在运行过程中能否有一种机制查看自身的状态,属性和行为.这就是反射 ...
- BZOJ 4445 [Scoi2015]小凸想跑步:半平面交
传送门 题意 小凸晚上喜欢到操场跑步,今天他跑完两圈之后,他玩起了这样一个游戏. 操场是个凸 $ n $ 边形,$ n $ 个顶点 $ P_i $ 按照逆时针从 $ 0 $ 至 $ n-1 $ 编号. ...
- 报错org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [bean.xml]
报这种错的原因基本上是applicationContext.xml文件中bean配置错误,错误如下: org.springframework.beans.factory.BeanCreationExc ...
- go语言的排序、结构体排序
原文:https://studygolang.com/articles/1598 晚上准备动手写点 go 的程序的时候,想起 go 如何排序的问题.排序 sort 是个基本的操作,当然搜索 searc ...
- C++(二十一) — 引用概念及本质
1.引用概念 引用是别名,必须在声明的时候初始化.即:是指一个已定义变量的别名.(一个内存空间,有两个名字都可以操作) 引用:在函数调用时,是变量的别名,不可以单独存在,使用时必须要初始化: 指针: ...
- Windows Desktop Optimization.bat
界面: github: https://github.com/m2nlight/WindowsDesktopOptimization Config Service [Disabled] Windows ...