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. Linux嵌入式 -- 内核简介(x86)

    0. 嵌入式系统 以应用为中心,软硬件可裁剪,对功耗.对体积.对成本等都有严格要求的专用计算机系统. 1.  linux体系结构 2. 为什么 划分为 用户空间 和 内核控件 ?  分两级,内核和应用 ...

  2. RabbitMQ 简单了解以及使用

    RabbitMQ 开发语言:Erlang – 面向并发的编程语言. AMQP:是消息队列的一个协议. mysql 是 java 写的吗?不是 那么 java 能不能访问?可以,则通过(驱动)协议;那么 ...

  3. scala学习手记37 - 容器的使用

    这次统一看一下scala中容器类的几个方法. Set filter()方法 filter()方法用来从Set中过滤获取含有指定特征的元素.示例代码如下: val colors1 = Set(" ...

  4. 51nod-1259-分块+dp

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1259 1259 整数划分 V2 基准时间限制:1 秒 空间限制:1310 ...

  5. Job for docker.service failed because the control process exited with error code. See "systemctl status do cker.service" and "journalctl -xe" for details.

    问题出现 :入手操作Docker时,安装启动后报了这个错 Job for docker.service failed because the control process exited with e ...

  6. 【scala】apply和update

    我们在使用scala的时候经常会用到对象的apply方法和update方法. 虽然我们表面没有察觉,但是实际上两个方法都会遵循相关约定被调用. apply apply方法的约定:用括号传递给变量(对象 ...

  7. shell编程实例1

    1.vim hello.sh 2. #!bin/bash echo "hello world!" 3.chmod +x  hello.sh 4.source hello.sh ls ...

  8. S5PV210启动过程详解1

    内存: SRAM  静态内存     特点就是容量小.价格高.优点是不需要软件初始化直接上电就能用 DRAM  动态内存    特点就是容量大.价格低.缺点就是上电后不能直接使用,需要软件初始化后才可 ...

  9. 释伴:Linux 上的 Shebang 符号(#!)

    使用Linux或者unix系统的同学可能都对#!这个符号并不陌生,但是你真的了解它吗? 本文了将给你简单介绍一下Shebang(”#!”)这个符号. 首先,这个符号(#!)的名称,叫做”Shebang ...

  10. 如何将桌面的路径定义到其它盘符,如d:\users\桌面

    首先要在“开始”——“运行”内输入“regedit”打开注册表编辑器,然后要在“文件”下拉菜单中的“导出”功能备份好注册表,以防万一,接着在左侧窗口依次打开: HKEY_CURRENT_USER\So ...