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 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
题目解读
有n个城市,给出了其中几个城市之间存在路径(直接或间接),也就是连通,现在假设某个城市沦陷,和它有关联的路径全部断开,问需要修几条路才能保证剩下的城市全连通。相当于连通的城市组成一个集合,可能有好几个这样的集合,那么n个集合需要n-1条路就能全部连通。也就是说,沦陷的城市,单独作为一个集合,再去找出剩下的这些连通的集合,假如有n个,返回n-1即可。
给你一个无向图,给定某个节点,把这个节点直接关联的边全断开,然后去求连通分量的个数,最后返回连通分量的个数减1。
思路解释
一般对于求连通分量的问题,都是并查集或者DFS,我这里采用DFS,因为DFS是真的简单。
- 用一个
visit
数组记录节点的访问状况,初始化全部为false
。 dfs(int i)
内,完成把和i
直接关联或间接关联的节点都标记为true
(邻接节点继续递归找到的就是间接相关联的),这样,一次dfs就相当于一个连通分量从整个节点集中排除出去了,我们只需要统计dfs执行了多少次才使得visit数组全为false
,就能得到连通分量的个数。- 对于这个题,加入沦陷的城市是
lost
,本来应该把和它关联的划分到一起,但是因为它沦陷了,需要断开全部路径,我们只需要把visit[lost]单独设置为false再去划分别的,就能达到将他排除在外的效果。
满分代码
注释很详细,大家自己看吧,啦啦啦。。。
#include <iostream>
#include <algorithm>
using namespace std;
/**
* 有n个城市,给出了其中几个城市直接存在路(直接或间接),也就是连通,
* 现在假设某个城市沦陷,和它有关联的路径全部断开,问需要修几条路才能保证剩下的城市全连通
*
* 相当于连通的城市组成一个集合,可能有好几个这样的集合,那么n个集合需要n-1条路就能全部连通
*
* 也就是说,沦陷的城市,单独作为一个集合,再去找出剩下的这些连通的集合,假如有n个,返回n-1即可
*
*/
// 邻接矩阵,节点编号 1-1000
bool graph[1001][1001];
// 用是否访问来划分连通的集合
bool visit[1001];
// 有几个城市
int g_nodes;
// 每一次dfs,都把和这个城市连通的所有城市标记为以访问,这样就相当于划分出一个集合,统计这个函数执行了几次就能知道划分出了几个集合
void dfs(int node) {
// 标记自己
visit[node] = true;
// 找到和他连通的,且没有标记过的,进行标记,划分为一个连通集合
for (int j = 1; j <= g_nodes; ++j) {
if (!visit[j] && graph[node][j])
// 注意这里是深度优先遍历,不是直接visit[j]=true,要找到全部直接或间接连连通的
dfs(j);
}
}
int main() {
int edges, k;
cin >> g_nodes >> edges >> k;
int s, e;
while (edges-- > 0) {
cin >> s >> e;
// 无向图
graph[s][e] = graph[e][s] = true;
}
// k种假设
int lost;
while (k-- > 0) {
cin >> lost;
// 重新初始化viste数组
fill(visit, visit + g_nodes + 1, false);
// 统计连通分量有几个
int cnt = 0;
// 沦陷的城市单独作为一个集合
visit[lost] = true;
// 统计剩下的连通分量有几个
for (int j = 1; j <= g_nodes; ++j) {
// 它所在的连通分量还未被划分并统计
if (!visit[j]) {
dfs(j);
// 每一次dfs都会划分出一个连通分量
cnt++;
}
}
// n集合,需要n-1个边
cout << cnt - 1 << endl;
}
return 0;
}
1013 Battle Over Cities (25分) 图的连通分量+DFS的更多相关文章
- 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 ...
- 【PAT甲级】1013 Battle Over Cities (25 分)(并查集,简单联通图)
题意: 输入三个整数N,M,K(N<=1000,第四个数据1e5<=M<=1e6).有1~N个城市,M条高速公路,K次询问,每次询问输入一个被敌军占领的城市,所有和该城市相连的高速公 ...
- 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 ...
- PAT 解题报告 1013. Battle Over Cities (25)
1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...
- PAT Advanced 1013 Battle Over Cities (25) [图的遍历,统计连通分量的个数,DFS,BFS,并查集]
题目 It is vitally important to have all the cities connected by highways in a war. If a city is occup ...
- 1013. Battle Over Cities (25)(DFS遍历)
For example, if we have 3 cities and 2 highways connecting city1-city2 and city1-city3. Then if city ...
- 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 A 1013. Battle Over Cities (25)【并查集】
https://www.patest.cn/contests/pat-a-practise/1013 思路:并查集合并 #include<set> #include<map> ...
随机推荐
- BIOS时间与系统时间(windows/linux时间同步问题)
写作动机 双系统是不少人喜欢的方式,但安装双系统之后一般会出现两个系统时间不一样的问题,刚开始用双系统的时候也没怎么在意,就是装上后在网上找找相关解决方法,复制粘贴代码完事儿.但是次数多了就有点烦了, ...
- JDBC中的时间处理
MySQL中常用的时间类有: java.sql.Date, Time, Timestamp 用的比较多的是ava.sql.Date和TimeStamp: 先看表结构 CREATE TABLE `t_u ...
- Sublime text 3快捷键壁纸版
- CentOS下宝塔如何部署Django项目?
基础环境 装好宝塔服务 宝塔里装好[Python项目管理器] 宝塔里装好[Nginx] 把Django项目代码发到服务器 把代码放到服务器上有两种方法: 方法一:服务器上安装Git,通过Git Clo ...
- deepin下深度终端使用ssh-agent(xshell中的xagent功能)
背景:从windows10换到deepin后,在连接公司的服务器遇到了问题:windows下用的是xshell,开启xagent后,可直接从公司的跳转板上连接生产服务器:在deepin的深度终端上,从 ...
- c++指定输出小数的精度
在c++中,有的时候要对输出的double型或float型保留几位小数,这时可以使用setflags(ios::fixed),不过要先包含有文件<iomainp>,具体如下 例: #inc ...
- tp5.0--多个条件查询全部数据
用where来查询的话(非主键): 查找:
- php二维数组的排序
/** * @desc arraySort php二维数组排序 按照指定的key 对数组进行排序 * @param array $arr 将要排序的数组 * @param string $key ...
- docker配置dns与容器的访问控制(6)
Docker 没有为每个容器专门定制镜像,那么怎么自定义配置容器的主机名和DNS配置?秘诀就是它利用虚拟文件来挂载到容器的3个相关的配置文件. 进入容器内使用mount命令可以看到挂载信息,这种机制可 ...
- HDU 5954 Do Not Pour Out
#include<bits/stdc++.h> using namespace std; #define rep(i,a,b) for(int i=a;i<=b;++i) #defi ...