1013 Battle Over Cities

 

目录

Input Specification:

Output Specification:

Sample Input:

Sample Output:

一、题目大意 

二、思路

三、代码 

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个城市和M条道路,每条道路只包含两端的城市

现给出K种城市被攻陷的情况,每种情况下只有一个城市被攻陷

要求给出每种情况下,让没有被攻陷的城市能够全连通最少需要再修建几条公路

二、思路

1.最好的情况,不需要新修建道路,如下图1(黑色线条代表已存在的道路,红色线条代表被攻陷后无法通行的道路,红色数字表示被攻陷的城市,下同

                                                                 图1

                                                           

2.中枢城市被攻陷,出现若干个分散的城市集合,如下图2

                                                                        图2

                                                             

 

分析:上图中,城市3被攻陷,此时端点含有3的道路全部断开,由此生成了4个分散的城市集合,如下图3

                                                                        图3

                                                               

显而易见,最少需要的道路数量就是 除被攻陷城市外分散的城市集合个数-1

那么解题流程如下:

1.读入原始数据,并生成原始图。

2.将端点含有被攻陷城市的道路切断。

3.统计切断后除被攻陷城市外分散的城市集合个数,减去1后输出

4.重新拷贝原始图后,循环执行2~3

三、代码

#include<bits/stdc++.h>
using namespace std;
#define MAXN 1001

bool vis[MAXN];

void dfs(vector<int> Adj[] , int x);

int main(){
    int N , M , K;
    cin >> N >> M >> K;
    vector<int> init[N + 1];
    for(int i = 0;i < M;i ++){
        int city1 , city2;
        cin >> city1 >> city2;
        //存入初始图
        init[city1].emplace_back(city2);
        init[city2].emplace_back(city1);
    }
    for(int i = 0;i < K;i ++){
        int lost; //被攻陷的城市
        cin >> lost;
        vector<int> Adj[N + 1]; //存放切断道路后的图
        for(int j = 1;j <= N;j ++){
            if(j != lost){  
                Adj[j] = vector<int>(init[j]); //不要放入被攻陷的城市
            }
        }
        for(int j = 1;j <= N;j ++){
            if(j != lost){
                auto it = find(Adj[j].begin() , Adj[j].end() , lost); 
                //如果当前城市与被攻陷的城市之间有道路
                if(it != Adj[j].end()){
                    //把和被攻陷城市连通的道路放到末尾并切断
                    swap(*it , *(Adj[j].end() - 1));
                    Adj[j].pop_back();
                }
            }
        }
        int connect_num = 0; //分散的城市集合的数量
        vector<int> isConnect; //用来存放已经记录过的城市,或是与已经记录过的城市连通的城市
        for(int j = 1;j <= N;j ++){
            if(j != lost){
                //若当前城市还没有被记录,且不是被攻陷的城市
                if(find(isConnect.begin() , isConnect.end() , j) == isConnect.end()){
                    int flag = 0; //判断是否全连通
                    memset(vis , false , N + 1); //初始化访问
                    dfs(Adj , j); //从当前城市开始访问
                    for(int k = 1;k <= N;k ++){
                        if(k != lost){
                            if(!vis[k]){
                                flag = 1;
                            }else{
                                isConnect.emplace_back(k); //将已经访问的城市记录。
                            }
                        }
                    }
                    if(flag == 1){
                        //若没有全连通,则说明从该城市出发是一个未被记录的分散城市集合
                        connect_num ++; 
                    }
                }
            }
        }
        printf("%d\n" , connect_num == 0 ? 0 : connect_num - 1);
    }
    
    return 0;
}

void dfs(vector<int> Adj[] , int x){
    vis[x] = true;
    for(int i = 0;i < Adj[x].size();i ++){
        int v = Adj[x][i];
        if(!vis[v]){
            dfs(Adj , v);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

R_1220

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值