[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 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 city​1​​-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​​.

输入格式

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.

输出格式

For each of the K cities, output in a line the number of highways need to be repaired if that city is lost.

输入样例

3 2 3
1 2
1 3
1 2 3

输出样例

1
0
0

解析

1.题目大意:输入n,m,k三个数,n表示城市的个数(默认编号1~N),m表示边数(无向边),k表示要查询的城市数。然后m行输入边的两个顶点,最后一行输入K个检查的城市结点。对于每个输入的城市,表示该城市被敌军占领,切断所有该城市与其它城市的路径,要求需要新建多少条路径,才能使得除了该城市以外的城市之间能够畅通无阻地联系。

2.我的思路是,即判断除去该结点后,连通分量的个数,假如有3个分量,由于他们内部是连通的,所以只需要建2条路径,使得这3个连通分量能够连接起来即可满足条件。

3.由于是刚接触图的题目,写题还是很生疏,诸多繁杂,还望看官耐心看看:

#include<iostream>
using namespace std;
const int maxn = 1010;
int n, m, k;
int cnt = 0;  //记录连通分量的个数
bool Graph[maxn][maxn];  //记录初始图(备份)
bool Graph1[maxn][maxn]; //记录修改之后的图(每次查找城市K都不一样)
bool visit[maxn] = { false }; //某个结点有没有被访问过
void init();
void DFS(int id);
void DFSTraverse();
int main()
{scanf("%d %d %d", &n, &m, &k);for (int i = 0; i < m; i++) {int id1, id2;scanf("%d %d", &id1, &id2);Graph[id1][id2] = true;Graph[id2][id1] = true;}for (int i = 0; i < k; i++) {int t;init();scanf("%d", &t);visit[t] = true;for (int i = 1; i <= n; i++) {Graph1[t][i] = 0;Graph1[i][t] = 0;}DFSTraverse();}return 0;
}
void init() {  //初始化函数cnt = 0;for (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++) {Graph1[i][j] = Graph[i][j]; //把初始的图放到Graph1中}}for (int i = 1; i <= n; i++) visit[i] = false; //重置visit数组
}
void DFS(int id) {visit[id] = true;for (int i = 1; i <= n; i++) {if (Graph1[id][i] > 0) {Graph1[id][i] = Graph1[i][id] = 0; //删除路径,防止回头if (visit[i] == false) DFS(i); //如果与它相连的结点没有被访问过,继续访问}}
}
void DFSTraverse() {for (int i = 1; i <= n; i++) {if (visit[i] == false) {DFS(i);cnt++;}}printf("%d\n", cnt - 1);  //输出的是连通分量的个数-1
}

[PAT A1013]Battle Over Cities相关推荐

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

  2. PAT甲级1013 Battle Over Cities:[C++题解]并查集、结构体存边

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:并查集题目. 不清楚并查集的小伙伴,请移步并查集原理并查集板子:acwing836. 合并集合. 题意:给定一个连通图,当删掉任意1个 ...

  3. PAT TOP 1001 Battle Over Cities - Hard Version (35)

    问题描述: 1001 Battle Over Cities - Hard Version (35 分) It is vitally important to have all the cities c ...

  4. PAT (Advanced Level) Practise 1013. Battle Over Cities (25)

    1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...

  5. 1013 Battle Over Cities(并查集解法)

    关于背景的介绍见1013 Battle Over Cities(图的DFS解法) DFS就是不算特定结点后数连通子图的总数,再减一.我想着那么并查集就是数不算特定节点后,集合元素(根)的个数.但是我弄 ...

  6. 1013. Battle Over Cities (25) 连通图

    1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...

  7. Battle over Cities

    Battle over Cities Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/65536K (Java/Other) ...

  8. 【PAT甲级 - 1013】Battle Over Cities (25分)(并查集)

    题干: It is vitally important to have all the cities connected by highways in a war. If a city is occu ...

  9. 【解析】1013 Battle Over Cities (25 分)_31行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 It is vitally important to have all the cities connected by highw ...

  10. PAT1013. Battle Over Cities (25)

    题目如下: It is vitally important to have all the cities connected by highways in a war. If a city is oc ...

最新文章

  1. 斯人若彩虹,遇上方知有
  2. css内边距与外边距的区别
  3. hibernate 基础学习
  4. SQL 中的 AND OR
  5. python 读取yml文件_Python 读取 yaml 配置文件 | 文艺数学君
  6. [转]MySQL日志——Undo | Redo
  7. Java 中参数传递是传值还是引用?
  8. 巧用 Lazy 解决.NET Core中的循环依赖关系
  9. U盘流畅运行linux发行版,做各种linux发行版的启动U盘方法
  10. 【EMV L2】数据元格式 对齐方式
  11. python2.7安装pygame_python 安装 pygame了
  12. 计算理论笔记 9月27日
  13. 牛顿插值算法MATLAB实现
  14. obs录制视频做up主流程
  15. mac下免费svn工具
  16. 卡尔曼滤波原理学习笔记
  17. SQL Server研习录(20)——FLOOR()函数
  18. RTI路由服务入门手册
  19. 嵌入式Linux的MiniGUI研究和移植
  20. 网络硬盘(简称网盘)

热门文章

  1. 酷我音乐android2.0,酷我音乐2012 2.0.0(For iphone)享受听歌“零“消费
  2. uva 10098(全排列)
  3. 编程中,有哪些好的习惯一开始就值得坚持?
  4. python+mitmdump实战(3/3)(附源码)
  5. 商家自研美团闪购开放平台SDK对接
  6. 计算机过热保护,CPU过热 保护电脑关机的解决办法有哪些
  7. apache服务器的日志文件,apache日志文件在哪
  8. 英文科技论文写作与学术报告Lecture 4习题答案
  9. 正则表达式美元符号$
  10. Word各级标题格式设置和自动排序(标题序号)设置