【PAT甲级 - C++题解】1013 Battle Over Cities

✍个人博客:https://blog.csdn.net/Newin2020?spm=1011.2415.3001.5343
📚专栏地址:PAT题解集合
📝原题地址:题目详情 - 1013 Battle Over Cities (pintia.cn)
🔑中文翻译:战争中的城市
📣专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!
❤️如果有收获的话,欢迎点赞👍收藏📁,您的支持就是我创作的最大动力💪

1013 Battle Over Cities

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
题意

题目给定 m 条边,以及 n 个城市和 k 个重点关注城市。

需要我们计算当重点关注城市中的一个城市被敌人占领后,需要再连多少条边才能使所有城市都能相连。

例如,现在有两条边,三个城市,分别是城市 1 和城市 2 相连,以及城市 1 和城市 3 相连,那么当城市 1 作为重点关注城市后被敌人占领,与城市 1 相关的两条边都会失效,需要再连一条城市 2 到城市 3 的边我方才能正常在城市间通行。

思路

这道题用到了并查集方法,在之前的文章中我有详细讲解,不了解的小伙伴可以跳转到之前的文章中(包含并查集模板),传送门如下:

(248条消息) C++实现树 - 08 并查集_Pandaconda的博客-CSDN博客

具体思路如下:

  1. 先用一个数组将所有边保存起来。
  2. 遍历所有重点关注城市。
    1. 初始化并查集。
    2. 计算去掉该重点关注城市后,所有城市所在的不同集合数,需要相连的高速公路数就等于集合数减 1
    3. 输出去除当前重点关注城市后,需要连接的高速公路数。
代码
#include<bits/stdc++.h>
using namespace std;

const int N = 1010, M = 500010;
int n, m, k;
int p[N];

struct Edge
{
    int a, b;
}e[M];

//并查集模板
int find(int x)
{
    if (p[x] != x) p[x] = find(p[x]);
    return p[x];
}

int main()
{
    cin >> n >> m >> k;

    //输入每条边
    for (int i = 0; i < m; i++)    cin >> e[i].a >> e[i].b;

    //计算需要维修的最少高速公路数
    for (int i = 0; i < k; i++)
    {
        int x;
        cin >> x;

        //初始化并查集
        for (int i = 1; i <= n; i++)   p[i] = i;

        //计算集合数量
        int cnt = n - 1;
        for (int i = 0; i < m; i++)
        {
            int a = e[i].a, b = e[i].b;
            //去掉与x相关的边再计算
            if (a != x && b != x)
            {
                int pa = find(a), pb = find(b);
                //如果不在一个集合中,就将两者合并到一个集合
                if (pa != pb)
                {
                    p[pa] = pb;
                    cnt--;
                }
            }
        }

        //输出答案,需要连接的路数就等于集合数减1
        printf("%d\n", cnt - 1);
    }

    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值