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> ...
随机推荐
- 使用redis-dump与redis-load方式迁移redis数据库
实际生产场景中,有可能会因为迁移机房或者更换物理机等原因需要在生产环境迁移redis数据.本文就来为大家介绍一下迁移redis数据的方法. 迁移redis数据一般有如下3种方式: 1.第三方工具red ...
- 01、Hibernate安装配置
1.查看你的Eclipse的版本:Help | About Eclipse Version: 2018-12 (4.10.0) 2.HibernateTools的下载地址为:http:// ...
- 解决Typecho Gravatar头像加载缓慢的问题
前言 Typecho评论默认使用的是Gravatar头像,但因为Gravatar网站总是被墙,导致页面加载被拖慢,而且加载半天也还是个裂图,太影响心情,所以我们可以不使用Gravatar头像,换成另一 ...
- tensorflow1.0 dropout层
""" Please note, this code is only for python 3+. If you are using python 2+, please ...
- pytorch 矩阵数据增加维度unsqueeze和降低维度squeeze
增加一个维度 out.unsqueeze(-1) 降低一个维度 out.squeeze(dim=1)
- solr管理集合
其实完全版的管理,在web页面上就有. 同时,在官网文档上,也有:https://lucene.apache.org/solr/guide/6_6/coreadmin-api.html#CoreAdm ...
- 五分钟学会Python装饰器,看完面试不再慌
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是Python专题的第12篇文章,我们来看看Python装饰器. 一段囧事 差不多五年前面试的时候,我就领教过它的重要性.那时候我Pyt ...
- 0day学习笔记(3)--修改函数返回地址
环境: devc++(编译改为32位),windows10 源码(来自书中) #include <stdio.h> #define PASSWORD "1234567" ...
- qt tableview 选择模式
QAbstractItemView::SingleSelection QAbstractItemView::ContiguousSelection QAbstractItemView::Extende ...
- java中CyclicBarrier的使用
文章目录 CyclicBarrier的方法 CyclicBarrier的使用 java中CyclicBarrier的使用 CyclicBarrier是java 5中引入的线程安全的组件.它有一个bar ...