PAT甲级真题 1013 Battle Over Cities (25分) C++实现(基于Kruskal算法思想,分支标记法)

题目

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

思路

对于一个包含n个节点的图,将其连通且无回路,最少需要n-1条边。题目求剩下n-1个城市需要添加几条边能连通,只需计算出它们之间已有的无回路边数count,剩下的就是需要添加的。

基于Kruskal算法思想,逐个考察添加每条边后(不考虑被围困城市的),是否会形成回路。利用数组find标记分支的代表元素,find[i]就是节点i的连通分支标记。这里统一将节点号最小的最为代表元素。若不形成回路,则修改两个节点的分支标记,并计入已有边里。

用边表存储图,使用内置结构体pair

用了边表记录图。用到了内置结构体pair记录边,(位于utility.h中,已被vector.h包含)。pair定义如下:

template<class T1,class T2> struct pair

即包含两个值的结构体,分别可用first和second访问。

注意一个小坑:测试点4输入量很大,直接用cin会超时。可换成scanf或添加以下代码后再用cin

    ios::sync_with_stdio(false);
    cin.tie(0);

柳神方法

柳神使用了dfs(深度优先遍历)的方法求强连通分支个数, “求解需添加多少边”即转化为 “求需多少次 dfs 才能遍历完全图”。链接:

1013. Battle Over Cities (25)-PAT甲级真题(图的遍历,统计强连通分量的个数,dfs)

代码

#include <iostream>
#include <vector>
using namespace std;

int main(){
    //提高cin、cout效率,否则测试点4超时
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n, m, k;
    cin >> n >> m >> k;
    vector<pair<int, int> > edge(m); //边表,用pair记录
    for (int i=0; i<m; i++){
        cin >> edge[i].first >> edge[i].second;
    }
    for (int i=0; i<k; i++){
        int c;
        cin >> c;
        vector<int> find(n+1); //连通分支标记,取分支中最小的节点号
        for (int j=1; j<n+1; j++){
            find[j] = j;
        }
        int count = 0; //无回路的边数
        for (int j=0; j<m; j++){
            int a = edge[j].first;
            int b = edge[j].second;
            if (a!=c && b!=c){  //排除掉被围城市c
                if (find[a] != find[b]){  //不再同一个连通分支里
                    find[a] = min(find[a], find[b]);
                    find[b] = find[a];
                    count++;
                }
            }
        }
        int repair = n - 2 - count; //剩余n-1个点,无回路时全部练上共需n-2条边
        cout << repair << endl;
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值