pat1013. 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 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
判断图的联通分量个数。并查集。
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <iostream>
using namespace std;
int f[];
int getf(int i){
if(f[i]==-){
return i;
}
return f[i]=getf(f[i]);
}
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int n,m,k;
scanf("%d %d %d",&n,&m,&k);
int *u=new int[m];
int *v=new int[m];
int i,j;
for(i=;i<m;i++){
scanf("%d %d",&u[i],&v[i]);
}
int pnum;
for(i=;i<k;i++){
memset(f,-,sizeof(f));
scanf("%d",&pnum);
for(j=;j<m;j++){
if(u[j]!=pnum&&v[j]!=pnum){
int fu=getf(u[j]);
int fv=getf(v[j]);
if(fu!=fv){
f[fv]=fu;
}
}
}
int count=;
for(j=;j<=n;j++){
if(f[j]==-){
count++;
}
}
count-=;
cout<<count<<endl;
}
delete []u;
delete []v;
return ;
}
最新文章
- GPS 气压计高度测量
- 【hive】——Hive基本操作
- 在wex5平台grid里面的gridselect下拉不能显示汉字问题
- ilspy导致c# dll代码被窃取
- HTML5手机APP开发入(5)
- [git]fork+pull提交模式
- unity3d 赛车游戏——复位点检测优化、反向检测、圈数检测、赛道长度计算
- RequestContextListener有什么用
- Perl中的匹配(六)
- 1:CSS中一些@规则的用法小结 2: @media用法详解
- LA 4329(树状数组)
- Linux批量替换文本,文件夹内所有文本内容
- JS面向对象,创建,继承
- 在centos6,7 上编译安装内核
- bootstrap模态对话框
- Leetcode_122_Best Time to Buy and Sell Stock II
- 第四课 VMP壳内爆破
- VMware搭建虚拟机服务器
- C# 连接Oracle时报错的问题
- Ubuntu Eclipse C++运行问题:launch failed.Binary not found