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. [BZOJ2717]迷路的兔子[构造]

    构造题…当然需要推(看)一推(看)规(题)律(解)啦... 其实是在Discuss那个CA的一句话题解里面翻到这个东西的... 用奇怪的姿势枚举一下...先贴代码 #include<bits/s ...

  2. Minimum Path Sum,最短路径问题,动态规划

    问题描述:Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right ...

  3. thinkphp5新特性

    1.惰性加载(需要什么就加载什么,不需要什么就不加载),显著提高了应用性能: 2.支持composer: 3.遵守PSR-2.PSR-4规范: 4.支持单元测试: 5.安全机制,详细的日志能帮你轻轻松 ...

  4. HTML5标签学习

    <abbr> 表示一个缩写形式,比如 "Inc."."etc.".通过对缩写词语进行标记,您就能够为浏览器.拼写检查程序.翻译系统以及搜索引擎分度器 ...

  5. hdu 5978 To begin or not to begin(概率,找规律)

    To begin or not to begin Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java ...

  6. MacOS Docker安装

    Docker简介: Docker 是一个开源的应用容器引擎 Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化. ...

  7. opencv:图像模糊处理

    接口: blur(sourceImage,dstImage,Size(,)); // 图像模糊处理 示例代码: #include <opencv.hpp> #include <img ...

  8. python 在头文件添加 #include \"stdafx.h\"\r\n

    import osimport shutil#-*- coding:cp936 -*-import codecsfrom sys import argv def replace_all_files(p ...

  9. New Concept English three (40)

    23w/m 48 errors It has never been explained why university students seem to enjoy practical jokes mo ...

  10. 【tensorflow:Google】三、tensorflow入门

    [一]计算图模型 节点是计算,边是数据流, a = tf.constant( [1., 2.] )定义的是节点,节点有属性 a.graph 取得默认计算图 g1 = tf.get_default_gr ...