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 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 (<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 Knumbers, 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
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <string>
  6. #include <map>
  7. #include <stack>
  8. #include <vector>
  9. #include <queue>
  10. #include <set>
  11. #define LL long long
  12. #define INF 0x3f3f3f3f
  13. using namespace std;
  14.  
  15. const int MAXN = 1e6 + ;
  16.  
  17. int n, m, k, pre[MAXN], t;
  18.  
  19. struct node
  20. {
  21. int a, b;
  22. }edge[MAXN];
  23.  
  24. void init()
  25. {
  26. for (int i = ; i <= n; ++ i)
  27. pre[i] = i;
  28. }
  29.  
  30. int my_find(int x)
  31. {
  32. int n = x;
  33. while (n != pre[n])
  34. n = pre[n];
  35. int i = x, j;
  36. while (n != pre[i])
  37. {
  38. j = pre[i];
  39. pre[i] = n;
  40. i = j;
  41. }
  42. return n;
  43. }
  44.  
  45. int main()
  46. {
  47. scanf("%d%d%d", &n, &m, &k);
  48. for (int i = ; i < m; ++ i)
  49. scanf("%d%d", &edge[i].a, &edge[i].b);
  50. for (int i = ; i < k; ++ i)
  51. {
  52. init();
  53. scanf("%d", &t);
  54. for (int i = ; i < m; ++ i)
  55. {
  56. if (edge[i].a == t || edge[i].b == t)
  57. continue;
  58. int n1 = my_find(edge[i].a), n2 = my_find(edge[i].b);
  59. if (n1 != n2) pre[n1] = n2;
  60. }
  61. int ans = , temp = == t ? my_find() : my_find();
  62. for (int i = ; i <= n; ++ i)
  63. {
  64. if (i == pre[i] && i != t)
  65. ++ ans;
  66. }
  67.  
  68. printf("%d\n", -- ans);
  69. }
  70. return ;
  71. }

pat 1013 Battle Over Cities(25 分) (并查集)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

  9. PAT 1013 Battle Over Cities

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

随机推荐

  1. python使用input().split()接收多个用户输入

    1.input() 接收多个用户输入需要与split()结合使用 host, port, username, passwd, dbname = input("请输入服务器地址,端口号,用户名 ...

  2. Python 中的for....else....

    在一个for循环中,当循环遇到break语句之后程序就会跳出循环,执行for循环之后的语句:但是,当整个循环都没有遇上break语句,而且你想在这种情况下做一些事情的话,你就可以通过结合else来完成 ...

  3. K8s 还是 k3s?This is a question

    本文来自:Rancher Labs 自k3s问世以来,社区里有许多小伙伴都问过这样的问题"除了中间的数字之外,k3s和K8s的区别在哪里?","在两者之间应该如何选择?& ...

  4. SYZOJ中文安装指南

    Made By:Spaceskynet Thanks to other developers. 测试系统 Ubuntu-17.04 PS(全局变量): [syzoj2 path] = 您git的syz ...

  5. Java中 实体类 VO、 PO、DO、DTO、 BO、 QO、DAO、POJO的概念

    PO(persistant object) 持久对象 在 o/r 映射的时候出现的概念,如果没有 o/r 映射,没有这个概念存在了.通常对应数据模型 ( 数据库 ), 本身还有部分业务逻辑的处理.可以 ...

  6. 一:XMind

  7. python基础-列表List及内置方法

    数据类型之列表-List 用途:用于存一个或多个不同类型的值 定义:通过中括号存值,每个值之间通过逗号进行分隔 l1 = [1,'a',3,'b'] 特性:有序.可变.存多个值的数据类型 常用方法: ...

  8. 前端技术之:如何在Vue中使用clipboard.js复制服务端数据

    第一步 创建点击对象页面元素,并绑定业务数据. <el-button type="text" size="mini" class="copy-b ...

  9. [考试反思]1016csp-s模拟测试76:自知

    要打对拍. 要打对拍. 要打对拍. 要手模数据. 要手模数据. 要手模数据. 不要相信样例. 不要相信样例. 不要相信样例. 不要飘. 不要飘. 不要飘. 跟skyh学坏了.最近不打对拍. 连续十几次 ...

  10. NOIP模拟27

    两个机房又和在一起考试 开场看了看T1,感觉挺水的,过. T2,这个式子有点奇怪,暂时没什么思路,过 T3,好像保留最后几位换个根处理一下就行了,过,先去打T1 于是T1大概打了0.5h,连暴力带正解 ...