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 cit**y1-cit**y2 and cit**y1-cit**y3. Then if cit**y1 is occupied by the enemy, we must have 1 highway repaired, that is the highway cit**y2-cit**y3.

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:

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

Sample Output:

  1. 1
  2. 0
  3. 0

思路

给定一个图后,封锁一个点,若要使得剩下的点联通,求添加道路的最少数量。

用flag数组标记一个点是否访问过,用dfs可以求出封锁某点后,该图的联通分量数block(未连通的块)。答案就是block-1

最后一组数据会超时,可以用个map记录下封锁某点的答案,可以重复使用。

此题还可以继续优化时间复杂度,但是PAT应该可以直接过了。

代码

  1. #include <stdio.h>
  2. #include <string>
  3. #include <stdlib.h>
  4. #include <iostream>
  5. #include <vector>
  6. #include <string.h>
  7. #include <algorithm>
  8. #include <cmath>
  9. #include <map>
  10. #include <limits.h>
  11. using namespace std;
  12. int maze[1000+10][1000+10];
  13. int N, M, K;
  14. int block = 0;
  15. bool flag[1000+10];
  16. void dfs(int index){
  17. flag[index] = 1;
  18. for(int i = 1; i <= N; i++){
  19. if(maze[i][index] && !flag[i]) dfs(i);
  20. }
  21. }
  22. map<int, int> mmp;
  23. int main() {
  24. cin >> N >> M >> K;
  25. int e, s;
  26. for(int i = 0; i < M; i++){
  27. cin >> e >> s;
  28. maze[e][s] = 1;
  29. maze[s][e] = 1;
  30. }
  31. for(int i = 0; i < K; i++){
  32. cin >> e;
  33. block = 0;
  34. if(mmp.count(e)){
  35. cout << mmp[e] << endl;
  36. continue;
  37. }
  38. memset(flag, 0, sizeof(flag));
  39. flag[e] = 1;
  40. for(int j = 1; j <= N; j++){
  41. if(!flag[j]){
  42. block++;
  43. dfs(j);
  44. }
  45. }
  46. mmp[e] = block - 1;
  47. cout << block - 1 << endl;
  48. }
  49. return 0;
  50. }

PAT 1013 Battle Over Cities (dfs求连通分量)的更多相关文章

  1. PAT 1013 Battle Over Cities DFS深搜

    It is vitally important to have all the cities connected by highways in a war. If a city is occupied ...

  2. PAT 1013 Battle Over Cities

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

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

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

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

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

  5. PAT甲题题解-1013. Battle Over Cities (25)-求联通分支个数

    题目就是求联通分支个数删除一个点,剩下联通分支个数为cnt,那么需要建立cnt-1边才能把这cnt个联通分支个数求出来怎么求联通分支个数呢可以用并查集,但并查集的话复杂度是O(m*logn*k)我这里 ...

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

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

  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

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

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

随机推荐

  1. detach() 使用和.detach()和.data的区别 、cpu()函数的作用

    detach() 使用和.detach()和.data的区别 .cpu()函数的作用 待办 detach使用 https://blog.csdn.net/qq_27825451/article/det ...

  2. 题解【洛谷P3478】[POI2008]STA-Station

    题面 设\(dp_i\)表示以\(i\)为根节点时所有节点的深度之和. 首先以 \(1\) 为根求出所有点深度之和\(dp_1\),并预处理每个点的子树大小. 设 \(v\) 是 \(u\) 的孩子, ...

  3. 【Python】程序计时

  4. 注释web.xml

    注释掉红框里的内容

  5. java基础异常处理

    异常的定义:中断了正常指令流的事件. try..catch..finally结构: class Test{ public static void main(String[] args){ System ...

  6. C#中字节数组byte[]和字符串string类型的相互转换

    C#中字节数组byte[]和字符串string类型的相互转换: string转byte[]: byte[] byteArray = System.Text.Encoding.Default.GetBy ...

  7. OSS上传文件时设置请求头

    直接上传: // 如果需要上传时设置存储类型与访问权限,请参考以下示例代码. // ObjectMetadata metadata = new ObjectMetadata(); // metadat ...

  8. codis安装以及启动

    https://blog.csdn.net/ygd266/article/details/78469654

  9. LitElement(四)属性

    1.概述 LitElement管理您声明的属性及其对应的属性.默认情况下,LitElement将: 确保在任何声明的属性更改时进行元素更新. 捕获已声明属性的实例值.在浏览器注册自定义元素定义之前设置 ...

  10. python之路之线程,进程,协程

    一.线程和进程概述 1.python线程的Event 2.python线程其他和队列以及生产者消费者 3. 使用multprocessing创建进程 4.进程间数据共享方式——sharedmeory( ...