题目

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​​ -city​2​​ and city​1

​​ -city​3​ . Then if city​1​​ is occupied by the enemy, we must have 1 highway repaired, that is the highway city​2​​ -city​3​​ .

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的更多相关文章

  1. 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 ...

  2. 1013 Battle Over Cities (25分) DFS | 并查集

    1013 Battle Over Cities (25分)   It is vitally important to have all the cities connected by highways ...

  3. 【PAT甲级】1013 Battle Over Cities (25 分)(并查集,简单联通图)

    题意: 输入三个整数N,M,K(N<=1000,第四个数据1e5<=M<=1e6).有1~N个城市,M条高速公路,K次询问,每次询问输入一个被敌军占领的城市,所有和该城市相连的高速公 ...

  4. 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 ...

  5. PAT 解题报告 1013. Battle Over Cities (25)

    1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. PAT A 1013. Battle Over Cities (25)【并查集】

    https://www.patest.cn/contests/pat-a-practise/1013 思路:并查集合并 #include<set> #include<map> ...

随机推荐

  1. RabbitMQ 消息队列入门

    文档 入门 主要的内容:one two three four five six seven 前言 中间件 消息队列 异步处理,注册完发短信 应用解耦,订单接口调用扣库存接口,失败了怎么办? 流量削峰, ...

  2. 嵌入css方式

    总体见思维导图 . 嵌入css方式 1 内联式 内联式css样式表就是把css代码直接写在现有的HTML标签中,如下面代码: <p style="color:red"> ...

  3. requets中urlencode的问题

    前言 今天团队群里有师傅问requests怎么设置不解码,这里是语误,其实师傅想说的是,如果设置不编码. 一开始我没懂,然后师傅们解答了这个问题后,我想了会儿懂了. 在一些CTF题目中,可能会碰到这样 ...

  4. [HarekazeCTF2019] web

    在 buuoj 上看到的这个比赛题目,期间平台关了,就拿了 Dockerfile 本地做了,web 题目感觉还不错 encode_and_encode [100] 打开靶机,前两个页面都是 html ...

  5. 域名和服务器绑定及https协议更换

    服务器是之前已经购买了的 1.腾讯云产品中搜索域名注册(产品太多了懒得找,直接搜索来得快些) 2.进去之后可以选择各种后缀的域名,输入自己喜欢的,看看哪些后缀是没有被注册的.自己挑选一个就可以,按照指 ...

  6. MySQL之外键、主键、自增

    1.创建外键 create table userinfo( uid int auto_increment primary key, name varchar(32), department_id in ...

  7. mysql5.7免安装版配置

    解压之后,新建一个my.ini 内容是: [mysql] # 设置mysql客户端默认字符集 default-character-set = utf8 [mysqld] #安装目录 basedir = ...

  8. 13206抢票代码 py

    抢票代码 https://github.com/Bingshuli/12306Python 谷歌驱动 http://chromedriver.storage.googleapis.com/index. ...

  9. 五分钟秒懂机器学习混淆矩阵、ROC和AUC

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是机器学习专题的第18篇文章,我们来看看机器学习领域当中,非常重要的其他几个指标. 混淆矩阵 在上一篇文章当中,我们在介绍召回率.准确率 ...

  10. Nmap详细用法

    探测主机存活 (1)-sP :进行ping扫描 (2) -sn: ping探测扫描主机, 不进行端口扫描 (3)-sA     发送ACK探测存活 端口扫描 (1) -sS :半开放扫描 (2) sT ...