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

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

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 的边我方才能正常在城市间通行。


思路

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


具体思路如下:

  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;
}
目录
相关文章
|
10月前
|
C++
【PAT甲级 - C++题解】1040 Longest Symmetric String
【PAT甲级 - C++题解】1040 Longest Symmetric String
35 0
|
10月前
|
算法 C++
【PAT甲级 - C++题解】1044 Shopping in Mars
【PAT甲级 - C++题解】1044 Shopping in Mars
54 0
|
10月前
|
C++
【PAT甲级 - C++题解】1117 Eddington Number
【PAT甲级 - C++题解】1117 Eddington Number
49 0
|
10月前
|
存储 C++ 容器
【PAT甲级 - C++题解】1057 Stack
【PAT甲级 - C++题解】1057 Stack
48 0
|
10月前
|
存储 C++
【PAT甲级 - C++题解】1055 The World‘s Richest
【PAT甲级 - C++题解】1055 The World‘s Richest
58 0
|
10月前
|
C++
【PAT甲级 - C++题解】1051 Pop Sequence
【PAT甲级 - C++题解】1051 Pop Sequence
50 0
|
10月前
|
人工智能 BI C++
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
99 0
|
10月前
|
存储 定位技术 C++
【PAT甲级 - C++题解】1091 Acute Stroke
【PAT甲级 - C++题解】1091 Acute Stroke
35 0
|
10月前
|
存储 人工智能 C++
【PAT甲级 - C++题解】1085 Perfect Sequence
【PAT甲级 - C++题解】1085 Perfect Sequence
48 0
|
10月前
|
C++
【PAT甲级 - C++题解】1046 Shortest Distance
【PAT甲级 - C++题解】1046 Shortest Distance
48 0