题目描述

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

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

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

题目解析

使用深度遍历DFS,检查在缺少一个节点之后,图被分成了cnt个连通域,则需要修建cnt-1条通道将所有点连通。

代码

#include<iostream>
#include<vector>
using namespace std;int N, M, K;void DFS(int n,vector<bool>&citis, vector<vector<bool> >&hways)
{if (citis[n] != 0)return;citis[n] = 1;for (int p = 1; p < N + 1; ++p){if (hways[n][p] == 1)DFS(p, citis, hways);}return;
}int main()
{cin >> N >> M >> K;vector<int> lost(K);vector<vector<bool> >highways(N+1, vector<bool>(N+1, 0));for (int i = 0; i < M; ++i){int a, b;cin >> a >> b;highways[a][b] = 1;highways[b][a] = 1;}for (int i = 0; i < K; ++i){int a;cin >> a;lost[i] = a;}for (int i = 0; i < K; ++i){vector<vector<bool> >h_ways_lost = highways;for (int p = 1; p < N + 1; ++p){h_ways_lost[lost[i]][p] = 0;h_ways_lost[p][lost[i]] = 0;}vector<bool> citis(N + 1, 0);citis[lost[i]] = 1;int cnt = 0;for (int i = 1; i < N + 1; ++i){if (citis[i] == 0){++cnt;DFS(i, citis, h_ways_lost);}}cout << cnt-1 << endl;}system("pause");return 0;
}

PAT甲级A1013. Battle Over Cities (25)相关推荐

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

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

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

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

  3. PAT甲级1009 Product of Polynomials (25分)

    PAT甲级1009 Product of Polynomials (25分) 题目: 题目分析:和之前有一道多项式相加很像,注意点是不仅仅数组系数会变,还可能会出现之前没有的指数,因此要开2001大小 ...

  4. 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 (25 分)_31行代码AC

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

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

  7. 17冬第二题 PAT甲级 1141 Ranking of Institutions (25分) 有点鸡贼

    题目 After each PAT, the PAT Center will announce the ranking of institutions based on their students' ...

  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. PAT甲级 -- 1009 Product of Polynomials (25 分)

    This time, you are supposed to find A×B where A and B are two polynomials. Input Specification: Each ...

最新文章

  1. iphone应用程序结构
  2. TFS自定义开发中的反射应用
  3. c语言变量加常量,C语言(二)---常量与变量(示例代码)
  4. .net p/invoke 异常处理
  5. APPLE苹果电子设备模型样机|展示你的专业设计最佳选择
  6. Linus 发文宣布Linux Kernel 5.0 正式发布
  7. This is Huge! PhysBAM code is going to be released?
  8. Unity高管:深耕游戏、VR/AR,致力为开发者提供极致体验
  9. 【毕设】JAVA+SQL办公自动化系统(源代码+论文+外文翻译)
  10. 2019Java查漏补缺(二)
  11. 最新版mysql8.0.13如何汉化_MySQL 8.0.13 下载安装教程图文详解
  12. java 读取文件inputstream_使用FileInputStream读取本地文件
  13. python字符串按ascii码排序_C# 字符串按 ASCII码 排序的方法
  14. 虚拟机十步安装VMware_workstation
  15. .Net asp.net MVC 实现短信验证
  16. Windows7+Ubuntu10.04双系统安装指南
  17. iOS录音麦克风和系统震动的冲突
  18. Linux下nginx完全卸载删除+安装以及环境配置
  19. 运维脚本 内存管理统计(5)
  20. javascript的原始数据类型

热门文章

  1. css进阶01 |CSDN创作打卡
  2. 《大数据架构详解》读后感
  3. SAP Commerce Cloud Github 项目的个性化配置 customization
  4. C语言学习手记-一元二次函数解法
  5. 计算机辅助有什么用cad,CAD是什么 有哪些用途
  6. Ubuntu中修改grub重启出现 Memtest86 解决方法
  7. MKV视频如何转换为SWF格式
  8. html 判断iphonex,iPhoneX和iPhoneXS区别
  9. android实现本地视频的播放,类似于一个小型的MP4,可以选择本地的文件进行播放
  10. org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:processDebugManifest'