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

Input Specification:

Each input file contains one test case. Each case starts with a line containing 3 numbers N (<), Mand 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:

  1. 3 2 3
  2. 1 2
  3. 1 3
  4. 1 2 3

Sample Output:

  1. 1
  2. 0
  3. 0

解题思路:

  城市之间本来有一些道路,现在有某个城市被攻陷了,与该城市连通的道路就都不能走了,如果剩下还在我们手里的城市不连通,就要修筑新道路使他们连通,任意两个城市都可以修筑新道路。
  本题每组数据首先给出城市数量n,道路数量m,查询数量k,每次查询只有一个城市被攻陷,之后给出m行数据为每条道路连接的两个城市,随后一行有k个数,表示当前次查询中被攻陷的城市,要求输出最少需要新修筑多少条道路才可以使我们手里剩下的城市都连通。

  这里使用并查集解答本题。(本题需要路径压缩,否则会T)

  1. int getFather(int x){
  2. if(x != father[x])
  3. father[x] = getFather(father[x]);
  4. return father[x];
  5. }

  首先使用邻接表记录所有道路信息,每次查询遍历所有道路,通过并查集合并所有不与被攻陷城市连接的道路两端的城市。

  1. for(int i = ; i <= n; i++){ //遍历所有道路
  2. for(auto it : G[i]){
  3. if(i != closed && it != closed){ //道路不能与被攻陷的城市相连
  4. int fu = getFather(i);
  5. int fv = getFather(it);
  6. if(fu != fv) //如果两个城市不在同一个连通块中,合并这两个连通块
  7. father[fu] = fv;
  8. }
  9. }
  10. }

  之后对于还在我们手里的城市,获得合并后的连通块数量ans,ans - 1即可获得最少需要新修筑的道路数量。

  1. int ans = ;
  2. for(int i = ; i <= n; i++){ //获得连通块数量
  3. if(getFather(i) == i && i != closed){ //只要某个城市所在连通块父节点为自己且这个城市不是被攻陷的城市
  4. ans++; //连通块加一
  5. }
  6. }

AC代码

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = 1e3+;
  4. int father[maxn]; //father[i]记录i所在连通块的父节点
  5. vector<int> G[maxn]; //临界表记录边信息
  6. int n, m, k;
  7. int getFather(int x){ //并查集路径压缩找爹函数
  8. if(x != father[x])
  9. father[x] = getFather(father[x]);
  10. return father[x];
  11. }
  12. int main(){
  13. while(scanf("%d%d%d", &n, &m, &k) != EOF){ //输入城市数量 道路数量 查询数量
  14. while(m--){ //输入道路信息
  15. int u, v;
  16. scanf("%d%d", &u, &v);
  17. //G[i]记录与i城市连通的道路
  18. G[u].push_back(v);
  19. G[v].push_back(u);
  20. //道路是双向的,所以道路两端的城市都要记录这条道路
  21. }
  22. int closed; //closed记录当前被攻陷的城市
  23. while(k--){ //k条查询
  24. for(int i = ; i <= n; i++) //初始化每个城市自己单独在一个连通块
  25. father[i] = i;
  26. scanf("%d", &closed); //输入被攻陷的城市
  27. for(int i = ; i <= n; i++){ //遍历所有道路
  28. for(auto it : G[i]){
  29. if(i != closed && it != closed){ //道路不能与被攻陷的城市相连
  30. int fu = getFather(i);
  31. int fv = getFather(it);
  32. if(fu != fv) //如果两个城市不在同一个连通块中,合并这两个连通块
  33. father[fu] = fv;
  34. }
  35. }
  36. }
  37. int ans = ;
  38. for(int i = ; i <= n; i++){ //获得连通块数量
  39. if(getFather(i) == i && i != closed){ //只要某个城市所在连通块父节点为自己且这个城市不是被攻陷的城市
  40. ans++; //连通块加一
  41. }
  42. }
  43. printf("%d\n", ans - ); //输出答案
  44. }
  45. }
  46. return ;
  47. }

PTA (Advanced Level) 1013 Battle Over Cities的更多相关文章

  1. PAT (Advanced Level) 1013. Battle Over Cities (25)

    并查集判断连通性. #include<iostream> #include<cstring> #include<cmath> #include<algorit ...

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

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

  3. PAT 1013 Battle Over Cities

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

  4. PAT甲级1013. Battle Over Cities

    PAT甲级1013. Battle Over Cities 题意: 将所有城市连接起来的公路在战争中是非常重要的.如果一个城市被敌人占领,所有从这个城市的高速公路都是关闭的.我们必须立即知道,如果我们 ...

  5. PAT 1013 Battle Over Cities(并查集)

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

  6. pat 1013 Battle Over Cities(25 分) (并查集)

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

  7. 图论 - PAT甲级 1013 Battle Over Cities C++

    PAT甲级 1013 Battle Over Cities C++ It is vitally important to have all the cities connected by highwa ...

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

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

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

随机推荐

  1. Jenkins 集成 git .net 和nuget

    1. 源码配置 在 Credentials中配置 git 账号密码(如果是Gitee  可以使用 Jenkins Gitee Plugin) 2. 构建编译版本 2.1 批处理的目的 还原Nuget包 ...

  2. (zxing.net)一维码EAN 8的简介、实现与解码

    一.简介 一维码EAN 8:属于国际标准条码,由8个数字组成,属EAN的简易编码形式(EAN缩短码).当包装面积小于120平方公分以下无法使用标准码时,可以申请使用缩短码. 依结构的不同,EAN条码可 ...

  3. JSON is undefined. Infopath Form People Picker in SharePoint 2013

    After some analysis, we found that, this is a known defect with the Microsoft and it is being fixed ...

  4. ClamAV学习【8】——64位Windows7下编译运行实践

    之前用SourceInsight静态分析了ClamAV引擎源码,现在打算开始动态研究下.不过出师不利,一开始就遇到纠结的问题,能力还有待提高. 从官网下了一个VS2005工程的源码包(http://d ...

  5. Android Studio显示可视化编辑界面

    选中项目,依次展开“src/main/res/layout",双击"activity_main.xml",这样右侧就有“preview”选项卡了,点击activity_m ...

  6. 50余本中外Python电子教程及源码下载地址

    链接:http://pan.baidu.com/s/1c0VTwsC 密码:hapu

  7. robot framework学习笔记之三—Scalar变量

    一.变量赋值 1)Set赋值 通常使用Set Variable关键字对变量进行赋值,其他Set相关的带Variable的关键字也可以进行赋值 赋值的时候,变量后面写不写『=』都可以,如下: 如果${v ...

  8. jdk(1.8)命令行工具(二)

    2.3 jinfo:java配置信息工具 jinfo(Configuration Info for Java)的作用是实时的查看和调整虚拟机的各项参数.使用jps -v 可以查看虚拟机启动时显示指定的 ...

  9. Linux下配置环境变量的几个方法实例

    场景:一般来说,配置交叉编译工具链的时候需要指定编译工具的路径,此时就需要设置环境变量.例如我的mips-linux-gcc编译器在“/opt/au1200_rm/build_tools/bin”目录 ...

  10. 总结day5 ---- ,字典的学习,增删改查,以及字典的嵌套, 赋值运算

    内容大纲: 一:字典的定义 二:字典的增加 >1:按照key增加,  无则增加,有则覆盖 >2:setdefault()  ,无则增加,有则不变 三:字典的删除 >1:pop()  ...