【并查集/C++】1013 Battle Over Cities (25分)

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 city​1​​-city​2​​ and city​1​​-city​3​​. Then if city​1​​ is occupied by the enemy, we must have 1 highway repaired, that is the highway city​2​​-city​3​​.

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

本题大意为,城市之间有道路相连,去掉某一城市之后,判断其余城市的连通性,如果城市之间不连通,要增加多少道路。

要知道需要增加多少条道路,我们只需要知道,去掉某座城市之后,剩下这些城市会变成多少座“孤岛”。

此时适合使用并查集。原理在此不多解释,可以观看其他教程。

需要注意的是,在算“孤岛”总数(即集合数量)时,需要排除掉被“占领”的城市,因为被占领的城市和其他城市均不连通。

另外要注意一下:只有一座城市时,那么不管有没有被占领,都不需要额外修路。

即:

当集合数量≥2时,需要修的路数量是(集合数量-1)

当集合数量<2时,需要修的路数量是0

并查集模板不要写错!!!不要像我一样调试半天没发现问题,结果发现合并集合的函数写错[捂脸]

//1013
#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <vector>
using namespace std;
int find(vector<int>& set, int idx) {
    if (idx == set[idx])return idx;
    return set[idx] = find(set,set[idx]);
}
void bind(vector<int>& set, int i, int j) {
    int a = find(set, i);
    int b = find(set, j);
    set[a] = set[b];
}
int main()
{
    int n, m, k;
    cin >> n >> m >> k;
    vector<pair<int, int>>paths;
    for (int i = 0; i < m; ++i) {
        int s, t;
        cin >> s >> t;
        paths.push_back(make_pair(s, t));
    }
    for (int i = 0; i < k; ++i) {
        int city;
        cin >> city;
        vector<int>uniSet(n + 5);
        for (int j = 1; j <= n; ++j)uniSet[j] = j;
        
        for (auto&& path : paths) {
            if (path.first == city || path.second == city)continue;
            bind(uniSet, path.first, path.second);
        }
        unordered_set<int>set;
        for (int j = 1; j <= n; ++j) {
            if (j == city)continue;
            set.insert(find(uniSet, j));
        }
        if (set.size() >= 2)
            cout << set.size() - 1 << endl;
        else cout << 0 << endl;
    }
    return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值