zoukankan      html  css  js  c++  java
  • PAT 1013 Battle Over Cities 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

    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

    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个城市之间有m条道路,当其中的一个城市被敌人占领了,那么该城市通往其他城市的道路都将关闭,问剩下的几个城市至少需要再添加多少条道路,才能重新让这些城市连通起来。

    解题思路:如果将这道题映射到图论上,就可以看成原来的一个连通图,由于某一点的缺失,其关联边缺失而将连通图拆分成几个连通分量,问需要再添加多少条边才能重新变为连通图。其实只需要统计连通分量的个数就行了,因为将a连通分量抽象成点,仅需要a-1条边就可以使其连通。而统计连通分量的个数则直接DFS就可以了,利用邻接矩阵存图,对于每一个被占领的城市,去除这个城市结点,就是把它标记为已经访问过,这样在DFS的时候,对于所有未访问的结点进行遍历,就能将所有的连通分量的个数统计出来了。注意需要k次查询被占领的城市,因此每次都需要将vis数组置零。

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    int mp[1010][1010];
    int vis[1010];
    int n,m,k;
    void DFS(int x)
    {
        int i;
        vis[x]=1;
        for(i=1;i<=n;i++)
        {
            if(vis[i]==0&&mp[x][i]==1)
            {
                DFS(i);
            }
        }
    }
    int main()
    {
    
        int i,j, a,cnt;
        int v,w;
        scanf("%d%d%d",&n,&m,&k);
        for(i=0;i<m;i++)
        {
            scanf("%d%d",&v,&w);
            mp[v][w]=mp[w][v]=1;
        }
        for(i=0;i<k;i++)
        {
            memset(vis,0,sizeof(vis));
            scanf("%d",&a);
            cnt=0;
            vis[a]=1;
            for(j=1;j<=n;j++)
            {
                if(vis[j]==0)
                {
                    DFS(j);
                    cnt++;
                }
            }
            printf("%d
    ",cnt-1);
        }
       return 0;
    }
  • 相关阅读:
    JuiceSSH:安卓平台免费好用的 SSH 客户端
    git&github-本地库推送到远程库
    git&githib-给远程库取别名
    Git分支管理的本质
    MySQL学习笔记(一)--逻辑架构学习
    mysql-主从备份问题小结
    Docker--数据管理之Volumes
    初识OpenSSH--1
    一个最简单的Dockfile实践
    构词法2
  • 原文地址:https://www.cnblogs.com/wkfvawl/p/11390332.html
Copyright © 2011-2022 走看看