• 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.

    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 (<), 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,m,k,分别代表有n个城市,后面有m行,每一行都是两个数值aa和bb,代表城市aa和bb之间有一条路,后面又有一行,k个数字,代表编号为k的城市被占领,那么所有和城市k相连接的道路都要断开。问你把断开道路后的所有的城市连接起来要修多少条道路?

    思路:

    通过深搜判断非连通图的个数n,则最少需要的边数就是n-1。

    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstdlib>
    #include<cmath>
    #include<cstring>
    #include<string.h>
    using namespace std;
    typedef long long ll;
    const int maxn=1010;
    int mp[maxn][maxn];//存储边 
    int n,m,k;
    bool vis[maxn];//标记是否访问过 
    void dfs(int v){
        vis[v]=true;
        for(int j=1;j<=n;j++){
            if(!vis[j]&&mp[v][j]==1){
                dfs(j);
            }
        }
    }
    int main(){
        
        int a,b,x;
        int sum;
        memset(mp,0,sizeof(mp));//初始化为0 
        memset(vis,false,sizeof(vis));//初始化为false 
        scanf("%d %d %d",&n,&m,&k);
        for(int i=0;i<m;i++){
            scanf("%d %d",&a,&b);
            mp[a][b]=1; 
            mp[b][a]=1;
        }
        for(int i=0;i<k;i++){
            memset(vis,false,sizeof(vis));//重新遍历图,需要重新标记 
            scanf("%d",&x);
            sum=0;
            vis[x]=true;
            for(int j=1;j<=n;j++){
                if(!vis[j]){ 
                    sum++;//判断有几个非连通图 
                    dfs(j);
                }
            }
            printf("%d
    ",sum-1);//若有n个非连通图,只需要n-1条边即可连通 
            
        }
        return 0;
    }
  • 相关阅读:
    设计模式代理模式
    设计模式建造者模式(生成器模式)
    SQL SERVER SA 无法登陆的解决方法
    [转]ASP.NET编程技巧10则
    键盘伪码
    MICROSOFT和ADOBE干上了?
    WINDOWS2003域控制器禁止U盘
    Dameware在Winxp下权限配置
    FTP操作
    WinForm 鼠标在页面无操作时页面关闭
  • 原文地址:https://www.cnblogs.com/dreamzj/p/14300518.html
Copyright © 2020-2023  润新知