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

输入

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.

输出

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

样例输入 

3 2 3
1 2
1 3
1 2 3

样例输出 

1
0
0

题意理解

给你n个城市点 m条边 注意是无向边 最后k个询问 

每个询问是如果我们少了这个点,那么我们要重新连接多少条边才能保持图的连通性

那么我们可以想到了一个暴力并查集的一个做法

在没有这个点的情况下把所有的其他剩余的点都放入一个连通块里面

将其他剩余的点全部连起来

然后遍历一遍,只要并查集根节点是自身的,说明是一个连通块,找到一共有多少个独立连通块

然后独立连通块的数量-2就是我们要建立的边数

为什么-2呢

一个独立连通块就是我们没有的这个点,我们并不用把它加入我们的连通块里面,还有就是我们这个剩余的连通块中以这个点为根节点的根,除了被占领城市的这个点,其他独立连通快都要并入这个根节点。所有我们要连的边数就是sum-2

代码 

#include<bits/stdc++.h>
using namespace std;
const int N=500010;
int ae[N];
int n,m,k;
vector<int>g[N];
void init(){
    for(int i=0;i<=n;i++){
        ae[i]=i;
    }
}
int find(int x){
    if(ae[x]!=x)ae[x]=find(ae[x]);
    return ae[x];
}
void merge(int a,int b){
    a=find(a),b=find(b);
    if(a!=b){
        ae[a]=b;
    }
}
int get_sum(int x){
    int sum=0;
    for(int i=1;i<=n;i++){
        for(int j=0;j<g[i].size();j++){
            int k=g[i][j];
            if(i==x||k==x)continue;
            merge(i,k);
        }
    }
    for(int i=1;i<=n;i++){
        if(ae[i]==i)sum++;
    }
    return max(sum-2,0);
}
void solve(){
        scanf("%d%d%d",&n,&m,&k);
        while(m--){
            int a,b;
            scanf("%d%d",&a,&b);
            g[a].push_back(b);
            g[b].push_back(a);
        }
        while(k--){
            int x;
            scanf("%d",&x);
            init();
            int res=get_sum(x);
            printf("%d\n",res);
        }
}
int main(){
    int T=1;
    //scanf("%d",&T);
    while(T--){
       solve();    
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值