A1013 Battle Over Cities (25 分)PAT甲级真题(C++)题目详解【图论+DFS】

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.

For example, if we have 3 cities and 2 highways connecting city1​-city2​ and city1​-city3​. Then if city1​ is occupied by the enemy, we must have 1 highway repaired, that is the highway city2​-city3​.

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

题意分析:

这道题简单来说就是,本来城市之间存在高速公路使得各个城市通过各种路线都可以连通,即是一张连通图,现在去掉几个城市,这张图有可能就变为非连通了,我们要做的就是计算最少需要加上几条路(边)可以使得这张图再次成为连通图。

需要了解的有关连通图的知识点:

  • 百科上的定义:

  • 显然这里的地图是一个非连通图,我们可以把每一个城市看做一个点,一个连通分量

  • 一个n个顶点的连通图,至少需要n-1条边,所以我们要求的最短路线就 = 连通分量数-1

  • 当m个互相独立的连通分量需要成为连通图的时候,只需要添加m-1个边,所以题目就转换为求去掉某个点之后剩下的图所拥有的连通分量数

p.s.   emmm,可以想象成去掉的那个点本来与其他点连接的线都没有了,只剩下它本身还有若干个受它影响也落单的单个的点,再加上若干块受它影响被迫拆分开的连通图分支(我们把它看成一个“大点”),就求以这些单点大点组成一个连通图需要的边数即可~

基本思路方法:

  • 使用邻接矩阵 v[1010][1001] 存储初始时的连通地图,有路为1,否则为0
  • 使用深度优先遍历整张图的所有点,同时计算连通分量
  • 对于每一个被攻占的城市,visit[1010]数组记录访问情况,去除这个城市结点,标记为已访问(true)
  • 对所有未访问(false)的结点进行深度优先遍历,就能求到所有的连通分量(单点和大点)的个数~
  • 每一次输入被占领的城市之前要把visit数组置为false,因为对于每一个被占领的城市都要重新计算

代码如下:

#include<iostream>
using namespace std;
int v[1010][1010];
bool visit[1010]={false};
int n,m,k;
void dfs(int city)
{
    visit[city]=true;//标为访问过了
    for(int i=1;i<=n;i++)
    {
        if(visit[i]==false&&v[city][i]==1){//相连的城市中如果有还没有被访问的,继续访问,串联成一个大的连通图,直到断开
            dfs(i);//继续深度优先,直到该连通分支断开
        }
    }
}
int main()
{
    scanf("%d %d %d",&n,&m,&k);
    int c1,c2;
    for(int j=1;j<=m;j++){
        scanf("%d %d",&c1,&c2);
        v[c1][c2]=v[c2][c1]=1;//邻接矩阵,初始连通地图
    }
    int checkC;//待攻占的城市
    int count=0;//存放连通分量的个数
    for(int i=0;i<k;i++){
        count=0;//每一次重新计算
        fill(visit,visit+1010,false);//每一次重新计算
        scanf("%d",&checkC);
        visit[checkC]=true;//被攻占的城市记为已访问,不参与接下来的dfs
        for(int j=1;j<=n;j++)
        {
            if(visit[j]==false)//访问所有还没有被访问的点,经过多次遍历后,剩下连通分支组成的若干个“大点”,以及若干个单点
            {
                dfs(j);//深度优先遍历这个点所连接的所有点,串成一个连通分支
                count++;//算作一个“大点”,即为连通分量
            }
        }
        printf("%d\n",count-1);//根据上文的分析,最后的最短路线数就是连通分量数-1,即需要增加的边数
    }
    return 0;
}

运行结果如下:

(本来对DFS和连通图的概念不太清晰,写完思路分析再用大白话整理后感觉豁然开朗~,虽然很基本的一道题,不过希望掌握好基本的之后慢慢会做更复杂的,哈哈哈继续加油啊~~)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值