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+并查集的更多相关文章

  1. PAT甲题题解-1021. Deepest Root (25)-dfs+并查集

    dfs求最大层数并查集求连通个数 #include <iostream> #include <cstdio> #include <algorithm> #inclu ...

  2. 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 ...

  3. [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 ...

  4. 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 ...

  5. 1021. Deepest Root (25) -并查集判树 -BFS求深度

    题目如下: A graph which is connected and acyclic can be considered a tree. The height of the tree depend ...

  6. 1021. Deepest Root (25)

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

  7. PAT (Advanced Level) 1021. Deepest Root (25)

    先并查集判断连通性,然后暴力每个点作为根节点判即可. #include<iostream> #include<cstring> #include<cmath> #i ...

  8. 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 ...

  9. 1021 Deepest Root (25 分)

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

随机推荐

  1. asp.net Core 部署到CentOs7上,使用Nginx做代理

    一.CentOs7部署Nginx 1.准备工作 Nginx的安装依赖于以下三个包,意思就是在安装Nginx之前首先必须安装一下的三个包,注意安装顺序如下: 1 SSL功能需要openssl库,直接通过 ...

  2. Android QRCodeReaderView 和Camera API冲突

    开发一款小功能,核心功能是二维码扫描,然后发送到远端服务器.App结构分为两个Activity,Activity A 负责二维码扫描,然后将参数存到本地,再启动Activity B,在Activity ...

  3. spring boot项目获取application配置文件参数的两种方式

    前言:了解过spring boot这个技术的,应该知道spring boot的核心配置文件application.properties,当然也可以通过注解自定义配置文件**.properties的信息 ...

  4. C#抽象类和接口

    抽象类和接口有什么区别?有了抽象类为什么还要接口? 接口和抽象类的相同点是都不能实例化,不同点是接口中的方法都没有方法体,而抽象类则不然,除了抽象方法没有方法体外,其他方法都有方法体. 原因是:在C# ...

  5. 十二道MR习题 – 1 – 排序

    题目: 一个文件,大小约为100G.文件的每一行都是一个数字,要求对文件中的所有数字进行排序. 对于这个题目,了解过Hadoop的同学可以笑而不语了.即使用spark实现也是非常简单的事情. 先说下如 ...

  6. js进阶---12-12、jquery事件委托怎么使用

    js进阶---12-12.jquery事件委托怎么使用 一.总结 一句话总结:通过on方法(事件委托),给要绑定事件的元素的祖先绑定事件,从而达到效果. 1.事件委托是什么? 通过事件冒泡,让子元素绑 ...

  7. 如何在 CentOS7 中安装 Nodejs

    一.安装Nodejs 安装版本:10.13.0 1.安装必要的编译软件包 yum -y install gcc gcc-c++ 2.从源码下载Nodejs cd /usr/local/src wget ...

  8. 用临时用户数据目录启动Chrome,关闭安全检查等(解决Cross origin requests are only supported for HTTP?)

    Cross origin requests are only supported for HTTP? 参考:https://www.zhihu.com/question/20948649 批处理: s ...

  9. notepad++去空格空行技巧

    选择视图显示所有字符,替换成空的就行

  10. New Concept English three (27)

    35w/m 67 It has been said that everyone lives by selling something. In the light of this statement, ...