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.

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

题意:给出一个图,然后问去掉某个点以后有几个联通分量。路径的个数就是联通分量-1

如果求联通分量,一般用的是并查集,即把联通图表示为一个集合。只要求并查集的个数即是连通图的分量。

复习并查集: 并查集的Set相当于一个标签,如果是根元素,标签是0, 否则值是其所依附的元素。

并查集一共就两个操作:Find和Union,Find是找到某一个元素的根元素,Union是将两个元素放在同一个集合中。

一般来说,普通的Find过不了极大的测试点,必须进行路径压缩,就是把求得的父节点赋值给每一个路径上的元素。递归版本的Find:

ElementType Find(ElementType c, SetType Set){
	if(Set[c]==0) return c; //c是根元素
	else return Set[c]=Find(Set[c], Set); //把求得的根元素赋值给c元素
}

Union:

void Union(ElementType a, ElementType b, SetType Set){
	Set[Find(a, Set)]=Find(b,Set);//注意把b的根元素赋值给a根元素的标签,而不是a的标签
	//如果赋值给a的标签,那么a以上的元素没有加入到b集合中
}

代码, 由于每一次查询必须重新构建并查集,因此直接用一个二维数组存储了点对,而不是图。

#include<iostream>
using namespace std;
int Way[10000000][2];
int Find(int c, int Set[]){
    if(Set[c]==0) return c;
    return Set[c]=Find(Set[c], Set);
}
void Union(int a, int b, int Set[]){
    Set[Find(a,Set)]=Find(b, Set);
}
int main(){
    int N,K,M;
    cin>>N>>K>>M;
    for(int i=0;i<K;i++){
        cin>>Way[i][0]>>Way[i][1];
    }
    for(int i=0;i<M;i++){
        int Set[1000]={0};
        int Check;// 被检查的城市
        cin>>Check;
        for(int j=0;j<K;j++){
            if(Way[j][0]==Check||Way[j][1]==Check) continue;
            if(Find(Way[j][0],Set)!=Find(Way[j][1],Set)){
                Union(Way[j][0],Way[j][1],Set);
            }
        }
        int num=0;
        for(int j=1;j<=N;j++){
            if(!Set[j]) num++; 
        }
        cout<<num-2;
        if(i!=M-1) cout<<endl;
    }
}

另一个巧妙的方法是dfs,如果某个点没有被访问过,那么说明它不属于这个连通图,因此数量++

需要一个数组记录该点是否被访问过,然后便利所有点,当一个点没有被访问过时,num++

当一个点被断开时,将其设为已访问。(巧妙的方法)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值