题目

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. c++类初始化列表初探

    目录 1 初始化和赋值 1.1 结论 2 构造函数初始化列表 2.1 结论 3 必须使用初始化列表的情况 3.1 结论 4 成员初始化顺序 5 参考资料 1 初始化和赋值 初始化:创建一个对象并赋予一 ...

  2. 2018版移动端ui规范

    计规范是一种将移动端常用控件标准化.统一化的的文档 今天整理了一篇设计规范的文章概论,讲诉中会以ios做介绍,安卓由于开源,平台相对教多不做单一阐述,实际操作的时候,我们不管是做一代还是二次的迭代产品 ...

  3. Mysql表的对应关系

    表关系 一对一一张表中的一条记录与另一张表中最多有一条明确的关系:通常,此设计方案保证两张表中使用同样的主键即可假设一张学生表:id 姓名 年龄 性别 籍贯 婚否 住址那么姓名 年龄 性别 这种字段比 ...

  4. Linux网络编程(2)

    Preview 基于上一篇博客,本文将继续展开TCP面向连接的,客户端以及服务端各自需要进行的操作,我们按照真实TCP连接的顺序,分别阐述客户端socket(), connect()以及服务端sock ...

  5. input type file onchange上传文件的过程中,同一个文件二次上传无效的问题。

    不要采用删除当前input[type=file]这个节点,然后再重新创建dom这种方案,这样是不合理的.解释如下:input[type=file]使用的是onchange去做,onchange监听的为 ...

  6. webform 最后的黄昏之力

    前言 现在有人谈起webform 一般都会说这种技术已经过时了,毫无用处. 因为我们在日常开发中已经不会去开发哪种几个简单的网页的程序,我们的业务更加复杂,这种拖动式的过于死板. 但是是否毫无用处呢? ...

  7. 播放声音 (c++) (windows)

    自己看自己看自己看自己看自己看自己看 在<windows.h>中 一:BOOL WINAPI MessageBeep (_in UINT uType ); 播放一个波形文件 (也就是wac ...

  8. Linux网络服务第五章NFS共享服务

    1.笔记 NFS一般用在局域网中,网络文件系统c/s格式 服务端s:设置一个共享目录 客户端c:挂载使用这个共享目录 rpc:111远程过程调用机制 Showmount -e:查看共享目录信息 def ...

  9. Excel导入异常Cannot get a text value from a numeric cell解决及poi导入时注意事项

    POI操作Excel时偶尔会出现Cannot get a text value from a numeric cell的异常错误. 异常原因:Excel数据Cell有不同的类型,当我们试图从一个数字类 ...

  10. 【高并发】又一个朋友面试栽在了Thread类的stop()方法和interrupt()方法上!

    写在前面 新一轮的面试已经过去,可能是疫情的原因吧,很多童鞋纷纷留言说今年的面试题难度又提高了,尤其是对并发编程的知识.我细想了下,也许有那么点疫情的原因吧,但无论面试的套路怎么变,只要掌握了核心知识 ...