知识讲解:

图的遍历分为两种,深度遍历与广度遍历。这里讨论深度遍历。

以上图为例讨论图(图片来自《算法笔记》)的深度遍历:

设图形的顶点数为n。

先从顶点v0开始,用一个数组vis[n]来表示该顶点是否被访问,如果未被访问,vis[顶点编号]=0,否则为1.从v0开始访问,则vis[0]=1,v0可以连通v1与v2,即下一个访问的顶点是v1(遍历二维数组时1比2先访问到),再更新vis数组,然后再从v1访问v3,到v3时发现没有办法继续访问了,再退回上一个顶点v1(v1仍存在未被访问的岔道),从v1访问没有被访问过的v4,再从v4出发访问v5,到v5时无法再访问其余的顶点,回退至上一个存在未被访问岔道的顶点v0,访问未被访问的v2,至此所有顶点均被访问,遍历结束。

遍历要用递归实现;

代码实现:

int vis[1005],n;

int a[1005][1005];

void DFS(int v){

vis[v]=1;//该顶点被访问

for(int i=1;i<=n;i++)

     {

if(a[v][i]==1&&vis[i]==0)//与之有连接的点是否被访问过

{

DFS(i);

         }

     }

}

void DFSG()

{

for(int i=0;i<n;i++){

if(vis[i]==0){

DFS(i);}

}

}

例题研究:

1013 Battle Over Cities (25 分)

It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any other highways to keep the rest of the cities connected. Given the map of cities which have all the remaining highways marked, you are supposed to tell the number of highways need to be repaired, quickly.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 3 numbers N (<1000), M and K, which are the total number of cities, the number of remaining highways, and the number of cities to be checked, respectively. Then M lines follow, each describes a highway by 2 integers, which are the numbers of the cities the highway connects. The cities are numbered from 1 to N. Finally there is a line containing K numbers, which represent the cities we concern.

Output Specification:

For each of the K cities, output in a line the number of highways need to be repaired if that city is lost.

Sample Input:
3 2 3
1 2
1 3
1 2 3
Sample Output:
1
0
0

来源网站:https://pintia.cn/problem-sets/994805342720868352/problems/994805500414115840

思路分析:

这道题的题意是,一个图如果删除一个顶点,添加几条边能保证其他点的联通。

先介绍概念连通体:连通体就是一个最大连通的子图(可以是一个点,两个相连的点)。

根据题意,我们需要讨论图要形成连通图的条件:假如某个图有n个连通体,v0,v1……vn,如果它们要连通,至少需要n-1条边相连,这里特别注意将整个连通体视作一个整体,也可以将其视为一整个顶点。

这道题可以先通过深度遍历找到连通体(深度遍历是在不断形成最大连通体),判断连通体的数量(即每次遍历到底后又回到有未访问岔口的顶点的时候计数器+1);需要添加的边就是连通体数量-1.这里需特别注意,删除一个点不能在图上真的删除它,只能在遍历到被删除的点时就返回。

代码实现:

#include <stdio.h>
#include <string.h>
int vis[1005],n,m,k,delet;
int a[1005][1005];
void DFS(int v){
if(v==delet) return;
vis[v]=1;
for(int i=1;i<=n;i++)
{
if(a[v][i]==1&&vis[i]==0){
DFS(i);
}
}
}
int main()
{
scanf("%d %d %d",&n,&m,&k);
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
a[i][j]=-1;
a[j][i]=-1;
}
}
for(int i=0;i<m;i++){
int x,y;
scanf("%d %d",&x,&y);
a[x][y]=1;
a[y][x]=1;
}
while(k--){ scanf("%d",&delet);
int block=0;
//vis[delet]=1;
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++){
if(vis[i]==0&&i!=delet){
DFS(i);
block++;
}
}
printf("%d\n",block-1);
}
return 0;
}

  

最新文章

  1. from表单如果未指定action,submit提交时候会执行当前url
  2. &lt;三&gt;JDBC_面向对象思想的体现
  3. JavaScript---function、this关键字相关习题
  4. -bash: msgunfmt: command not found
  5. swift 同步加载图片
  6. $(function(){})和jQuery(function(){})
  7. jQuery对象和dom对象的辨析和相互转化
  8. JQuery的$命名冲突详细解析
  9. web api简单验证实现办法
  10. linux下搭建SVN服务器完全手册-很强大!!!!!
  11. ubuntu解压乱码
  12. POJ - 1733 Parity game 种类并查集+离散化
  13. Java中常见数据结构Map之LinkedHashMap
  14. 你能选择出,前几个元素吗?使用纯css
  15. java.lang.UnsatisfiedLinkError解决方法汇集(转载)
  16. 100-days: twenty-two
  17. CMMI摘要
  18. JAVA的基本数据类型和类型转换
  19. NATS_07:NATS之top工具监控以及测量调优工具
  20. 表格 滚动条 (tbody部分滚动)

热门文章

  1. Zookeeper绍二(分布式锁介)
  2. java基础04-数据类型扩展及面试题
  3. thanos receiver压测结果分享
  4. unity3d发布安卓出错plese set the package name
  5. Using Swap
  6. Android开发 定时任务清理数据
  7. String类(获取,转换,判断,比较)
  8. docker 传入变量
  9. 命令行传参是否只能针对main方法
  10. react直接使用bootstrap失效的原因