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 ...
随机推荐
- restful 规范(建议)
需求:开发cmdb,对用户进行管理. 做前后端分离,后端写api(URL),对用户表进行增删改查,应该写四个URL(还要给文档(返回值,返回,请求成功,干嘛,失败,干嘛)),然后分别写视图函数. ht ...
- 版本控制--git+idea
- 原生JS实现增加删除class
<!DOCTYPE html> <html> <head> <style type="text/css"> .night-mode{ ...
- 本地git连接远程github
git要连接GitHub仓库,是通过SSH加密连接的,所以必须要创建SSH key ssh-key -t rsa -C "youremail@example.com" 这里邮箱必须 ...
- cmd & tree & bash
cmd & tree & bash bug E: Unable to locate package tree solution # 1. update $ sudo apt-get u ...
- Object...与Object[]使用的一点区别和记录
Object是所有类的基类 简述: Object ...objects(称为可变个数的形参)这种参数定义是在不确定方法参数的情况下的一种多态表现形式.Java可变参数,即这个方法可以传递多个参数,这个 ...
- mesh函数
[t,W]=meshgrid([2:0.2:7],[0:pi/6:3*pi]); %设置时-频相平面网格点 Gs1=(1/(sqrt(2*pi)*a))*exp(-0.5*abs((t1-t)/a). ...
- 数据库MySQL5.7.21win64位安装配置
1,在MySQL官网下载mysql对应版本 https://dev.mysql.com/downloads/mysql/ 2,解压压缩文件到想要的位置 3,配置环境 打开 右键我的电脑-->属 ...
- python 机械学习之sklearn的数据正规化
from sklearn import preprocessing #导入sklearn的处理函数用于处理一些大值数据 x_train, x_test, y_train, y_test = tr ...
- ElasticHD Linux环境下安装
ElasticHD Linux环境下安装教程 ElasticHD windows环境下安装教程 上一篇讲了ElasticHD windows环境下安装,这一篇继续说明ElasticH ...