【PAT】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 FORMAT

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.

翻译:每个输入文件包括一组测试数据,每组测试数据第一行为3个数字N(<1000),M,K,分别为城市的总数,剩下的城市数和需要检查的城市数。接下来的M行每行包括2个数字描述一条高速公路连接的两端城市。这些城市用1-N来标记。最后一行包括K个数字,代表我们关注的城市。

OUTPUT FORMAT

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

翻译:对于K个城市,分别输出一行,为如果该城市被攻占所需修建的高速公路数。


Sample Input

3 2 3
1 2
1 3
1 2 3


Sample Output

1
0
0


解题思路:

对于每个需要检查的城市,对其他城市进行广度优先搜索,如果已搜索到就跳过,否则ccount(需要修建的高速公路数)加一,再搜索该城市。具体细节见代码。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
#define INF 99999999
using namespace std;
int N,M,K,d[1010],judge[1010]; 
vector<int>G[1010];
queue<int> q;
void bfs(int a,int except){
    d[a]=1;
    q.push(a);
    while(!q.empty()){
        int temp=q.front();q.pop();
        for(int i=0;i<G[temp].size();i++){
            if(G[temp][i]!=except&&!d[G[temp][i]]){
                q.push(G[temp][i]);
                d[G[temp][i]]=1;
            }
        }
    }    
}
int main(){
    scanf("%d%d%d",&N,&M,&K);
    int a,b;
    for(int i=0;i<M;i++){
        scanf("%d%d",&a,&b);
        G[a].push_back(b);
        G[b].push_back(a);
    }
    for(int i=0;i<K;i++)scanf("%d",&judge[i]);
    for(int i=0;i<K;i++){
        a=judge[i];
        memset(d,0,sizeof(d));
        int ccount=-1;
        for(int j=1;j<=N;j++){
            if(!d[j]&&j!=a)ccount++,bfs(j,a);
        }
        printf("%d\n",ccount);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值