A1013. 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
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<iostream>
#include<vector>
#include<queue>
using namespace std;
int G[][] = {};
int visit[] = {}, cnt = ;
int N, M, K;
void dfs(int vt, int lost){
visit[vt] = ;
for(int i = ; i <= N; i++){
if(G[vt][i] == && visit[i] == && i != lost && vt != lost){
dfs(i, lost);
}
}
}
int main(){
scanf("%d%d%d", &N, &M, &K);
int tempA, tempB;
for(int i = ; i < M; i++){
scanf("%d%d", &tempA, &tempB);
G[tempA][tempB] = G[tempB][tempA] = ;
}
for(int i = ; i < K; i++){
int lost;
scanf("%d", &lost);
for(int j = ; j <= N; j++)
visit[j] = ;
visit[lost] = ;
cnt = ;
for(int j = ; j <= N; j++){
if(visit[j] == ){
dfs(j, lost);
cnt++;
}
}
printf("%d\n", cnt - );
}
cin >> N;
return ;
}
总结:
1、题意:给出一个无向图G,再给出一个要去掉的节点,求图G在去掉该节点之后的连通分量个数。
2、要去掉lost的点,但在图G上直接将它与所有的点的边置0是不行的,因为不止一个查询,置0后无法恢复。可以在dfs传入lost的点,遍历的时候避开该点即可。
3、计数连通分量个数:利用visit数组,由于一次从点A开始的搜索会把所有和A联通的节点置1。所以可以从1到N每个节点使用一次dfs,当它的visit为0时,说明它和之前访问的节点不属于同一个连通分量。
4、还可以使用并查集:在输入每条边时,判断边上的两个点是否在同一个集合,如果在则不做改变,如果不在,则对两个集合做并。需要路径压缩。
A1013. Battle Over Cities的更多相关文章
- PAT A1013 Battle Over Cities (25 分)——图遍历,联通块个数
It is vitally important to have all the cities connected by highways in a war. If a city is occupied ...
- PAT甲级——A1013 Battle Over Cities
It is vitally important to have all the cities connected by highways in a war. If a city is occupied ...
- PAT_A1013#Battle Over Cities
Source: PAT A1013 Battle Over Cities (25 分) Description: It is vitally important to have all the cit ...
- 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-Top1001. Battle Over Cities - Hard Version (35)
在敌人占领之前由城市和公路构成的图是连通图.在敌人占领某个城市之后所有通往这个城市的公路就会被破坏,接下来可能需要修复一些其他被毁坏的公路使得剩下的城市能够互通.修复的代价越大,意味着这个城市越重要. ...
- PAT 1013 Battle Over Cities
1013 Battle Over Cities (25 分) It is vitally important to have all the cities connected by highway ...
- PAT Battle Over Cities [未作]
1013 Battle Over Cities (25)(25 分) It is vitally important to have all the cities connected by highw ...
- PTA (Advanced Level) 1013 Battle Over Cities
Battle Over Cities It is vitally important to have all the cities connected by highways in a war. If ...
随机推荐
- elasticsearch概念及倒排索引简单介绍
一.概念 集群:一个或者多个节点组织在一起 节点:一个节点是集群中的一个服务器,由一个名字来标识,默认是一个随机的漫威角色名字. 分片:将索引划分为多份的能力,允许水平分割和扩展容量,多个分片相应请求 ...
- linux中$1的意思
$1 在shell中成为“位置参数”,表示传入的第一个参数.在shell脚本主体中,表示shell脚本的第一个参数.用在shell脚本函数里时,表示的是函数的第一个入参.
- Appium之开发计算器自动化测试脚本Demo
1.依赖包 <!-- https://mvnrepository.com/artifact/io.appium/java-client --> <dependency> < ...
- Software License Manager
slmgr -ilc lenovo.xrm-ms slmgr -ipk lenovo-lenovo-lenovo-lenovo-lenovo
- UVA 1602 Lattice Animals
题目 输入n.w.h($1\leqslant n \leqslant 10, 1\leqslant w,h \leqslant n$),求能放在w*h网格里的不同的n连块的个数(注意,平移.旋转.翻转 ...
- ubuntu18.04系统下用devstack安装openstack(最新版)
ubuntu18.04系统下用devstack安装openstack(最新版) 2018年12月14日 16:34:14 Cherls 阅读数:427 前期准备: 安装git,升级pip,其他 s ...
- linq之group by 的使用
group by var list = from s in _sysBll.GetList(s => s.ParamID == "TraSchType" && ...
- MySql获取树型结构的所有子节点
stackoverflow的解决方案,亲测有效: SELECT * FROM person WHERE department IN (SELECT department_id FROM departm ...
- nginx 正向代理上网
配置文件: server { #resolver 21.202.152.10; #指定DNS服务器IP地址 |如果指定IP$scheme://22.2.65.214$request_uri 可以不指定 ...
- Fence Repair POJ - 3253 哈夫曼思想 优先队列
题意:给出一段无限长的棍子,切一刀需要的代价是棍子的总长,例如21切一刀 变成什么长度 都是代价21 列如7切成5 和2 也是代价7题解:可以利用霍夫曼编码的思想 短的棍子就放在底层 长的尽量切少一次 ...