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 ;
}
pat1013. Battle Over Cities (25)的更多相关文章
- PAT-1013 Battle Over Cities (25 分) DFS求连通块
It is vitally important to have all the cities connected by highways in a war. If a city is occupied ...
- PAT---1013. Battle Over Cities (25)
这道题目的意思是:在战争时代,如果一个城市被敌人占领了,那么和该城市相连的道路都必须关闭,我们必须把剩下的城市(即不包括被敌人占领的城市)连接起来. 举个例子,我们有3个城市,C1,C2,C3,C1和 ...
- PAT 解题报告 1013. Battle Over Cities (25)
1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...
- PAT1013: Battle Over Cities
1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...
- PAT 甲级 1013 Battle Over Cities (25 分)(图的遍历,统计强连通分量个数,bfs,一遍就ac啦)
1013 Battle Over Cities (25 分) It is vitally important to have all the cities connected by highway ...
- 1013 Battle Over Cities (25分) DFS | 并查集
1013 Battle Over Cities (25分) It is vitally important to have all the cities connected by highways ...
- Battle Over Cities (25)(DFS、连通图)
It is vitally important to have all the cities connected by highways in a war. If a city is occupied ...
- 1013. Battle Over Cities (25)
题目如下: It is vitally important to have all the cities connected by highways in a war. If a city is oc ...
- 1013 Battle Over Cities (25)(25 point(s))
problem It is vitally important to have all the cities connected by highways in a war. If a city is ...
随机推荐
- C# 中使用using的三种方法
1.using指令 using+命名空间,这种方法基本学习过C#的都用过,好处在于,写代码的时候不需要指定详细的命名空间 using System.Windows.Media; using Syste ...
- Socket 简易静态服务器 WPF MVVM模式(二)
command类 标准来说,command会有三种模式,委托命令 准备命令 附加命令 1.DelegateCommand 2.RelayCommand 3.AttachbehaviorCommand ...
- Atcoder Grand Contest 031C(构造,思维,异或,DFS)
#include<bits/stdc++.h>using namespace std;int n,a,b,sum;void dfs(int x,int y,int ban){ if( ...
- HSSFWorkbook导出表格使用
HSSFWorkbook excel的文档对象 HSSFSheet excel的表单 HSSFRow ...
- C# Winform 加载窗体/对象时的等待页面设计
在设计应用程序过程中,有时候加载对象需时较长,我们可以显示一个Loading等待页面,对用户来说就比较友好了. 这个还是涉及到多线程,下面是步骤. 一.创建好Loading窗体: 一个Panel用于显 ...
- 用HTML,css完成的百叶窗效果,新手必看
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title> ...
- Centos7.4 更换国内yum源
1.进入yum源配置文件夹.(配置之前先看看有没有安装wget命令呢,没的话可以先用当前的yum源安装一下再说.yum -y install wget) 2.转到配置文件目录 cd /etc/yum ...
- 6、C++共用体
6.共用体 共用体(union)是一种数据格式,他能够存储不同的数据类型,但只能同时存储其中的一种类型.也就是说,结构可以同时存储int.long和double,共用体只能存储ing.long.dou ...
- Android 查看联系人电话和姓名(ContentProvider)
1.介绍 2.使用方法 3.在AndroidManifest.xml文件中添加相关设置 <uses-permission android:name="android.permissio ...
- [BZOJ 3262]陌上开花
今天写了到偏序问题,发现博主真的是个傻X 传送门 以前的写法 /************************************************************** Probl ...