PAT甲级1013. Battle Over Cities

题意:

将所有城市连接起来的公路在战争中是非常重要的。如果一个城市被敌人占领,所有从这个城市的高速公路都是关闭的。我们必须立即知道,如果我们需要修理任何其他高速公路,以保持其他城市的连接。鉴于所有其余高速公路标记的城市地图,

你应该告诉高速公路需要修理的次数很快。

例如,如果我们有3个城市和2个连接city1-city2和city1-city3的高速公路3。那么如果city1被敌人占领,那么我们必须有1条公路修好,那就是高速公路city2-city3。

输入

每个输入文件包含一个测试用例。

每个案例分别以3号数字N(<1000),M和K分别开始,分别是城市总数,剩余高速公路数和待检查城市数。然后M行跟随,每个描述一条公路由2个整数,这是高速公路连接的城市的数量。

城市的编号从1到N.最后有一行包含K个数字,代表我们关心的城市。

输出

对于每个K个城市,如果该城市丢失,一条线上的公路数量需要修复。

思路:

求连通支路。并查集或者dfs都可以。

ac代码:

C++ 并查集

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<string>
  4. #include<algorithm>
  5. #include<queue>
  6. #include<vector>
  7. #include<unordered_map>
  8. #include<cstring>
  9. using namespace std;
  10. const int maxn = 1005;
  11. int mapx[maxn * maxn][2];
  12. int pre[maxn];
  13. int Find(int x)
  14. {
  15. return pre[x] == x ? x : pre[x] = Find(pre[x]);
  16. }
  17. void Union(int x, int y)
  18. {
  19. x = Find(x);
  20. y = Find(y);
  21. if (x == y) return;
  22. pre[y] = x;
  23. }
  24. int main()
  25. {
  26. int n, m, k, sum;
  27. cin >> n >> m >> k;
  28. memset(mapx, 0, sizeof(mapx));
  29. for (int i = 0; i < m; i++)
  30. {
  31. cin >> mapx[i][0] >> mapx[i][1];
  32. }
  33. int city;
  34. while (k--)
  35. {
  36. cin >> city;
  37. for (int i = 0; i <= n; i++)
  38. {
  39. pre[i] = i;
  40. }
  41. for (int i = 0; i < m; i++)
  42. {
  43. if (mapx[i][0] != city && mapx[i][1] != city)
  44. Union(mapx[i][0], mapx[i][1]);
  45. }
  46. sum = 0;
  47. for (int i = 1; i <= n; i++)
  48. {
  49. if (pre[i] == i && i != city)
  50. sum++;
  51. }
  52. cout << sum - 1 << endl;
  53. }
  54. return 0;
  55. }

C++ dfs

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<string>
  4. #include<algorithm>
  5. #include<queue>
  6. #include<vector>
  7. #include<unordered_map>
  8. #include<cstring>
  9. using namespace std;
  10. const int maxn = 1005;
  11. int visit[maxn];
  12. int link[maxn][maxn];
  13. int n, m, k;
  14. void dfs(int x)
  15. {
  16. visit[x] = 1;
  17. for (int i = 1; i <= n; i++)
  18. {
  19. if (visit[i] != 1 && link[i][x] == 1)
  20. dfs(i);
  21. }
  22. }
  23. int main()
  24. {
  25. cin >> n >> m >> k;
  26. int city1, city2;
  27. memset(link, 0, sizeof(link));
  28. for (int i = 0; i < m; i++)
  29. {
  30. cin >> city1 >> city2;
  31. link[city1][city2] = 1;
  32. link[city2][city1] = 1;
  33. }
  34. int misscity;
  35. int count;
  36. for (int i = 0; i < k; i++)
  37. {
  38. cin >> misscity;
  39. count = 0;
  40. memset(visit, 0, sizeof(visit));
  41. visit[misscity] = 1;
  42. for (int i = 1; i <= n; i++)
  43. {
  44. if (visit[i] != 1)
  45. {
  46. dfs(i);
  47. count++;
  48. }
  49. }
  50. cout << count - 1 << endl;
  51. }
  52. return 0;
  53. }

PAT甲级1013. Battle Over Cities的更多相关文章

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

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

  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. PAT A 1013. Battle Over Cities (25)【并查集】

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

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

  5. PAT Advanced 1013 Battle Over Cities (25) [图的遍历,统计连通分量的个数,DFS,BFS,并查集]

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

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

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

  7. PAT 1013 Battle Over Cities

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

  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(25 分) (并查集)

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

随机推荐

  1. thinkphp对数据库的增删改查(查询构造器)

  2. flask插件系列之flask_session会话机制

    flask_session是flask框架实现session功能的一个插件,用来替代flask自带的session实现机制. 配置参数详解 SESSION_COOKIE_NAME 设置返回给客户端的c ...

  3. [Leetcode] Combination Sum 系列

    Combination Sum 系列题解 题目来源:https://leetcode.com/problems/combination-sum/description/ Description Giv ...

  4. JS时间转换的一个坑位

    在做项目的时候,无意发现了一个小东西. new Date('2018-05-15') new Date('2018-5-15') 输出的结果是不同的,相差了8小时.然后让我回忆到之前看的一个时间转换函 ...

  5. Petrozavodsk Winter Training Camp 2018

    Petrozavodsk Winter Training Camp 2018 Problem A. Mines 题目描述:有\(n\)个炸弹放在\(x\)轴上,第\(i\)个位置为\(p_i\),爆炸 ...

  6. Selenium_Page Object设计模式

    Page Object 介绍 Page Object设计模式的优点如下: 减少代码的重复 提高测试用例的可读性 提高测试用例的可维护性,特别是针对UI频繁变化的项目 当Web页面编写测试时,需要操作该 ...

  7. HDU 2102 A计划(BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102 题目大意:公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输 ...

  8. Ceph 时钟偏移问题 clock skew detected 解决方案--- 部署内网NTP服务

    告警:HEALTH_WARN clock skew detected on mon.ip-10-25-195-6; 8 requests are blocked > 32 sec; Monito ...

  9. 深入理解 WordPress 数据库中的用户数据 wp_user

    WordPress 使用 wp_users 数据表存储用户的主要数据,该数据表结构类似于wp_posts 和 wp_comments 数据表,存储的是需要经常访问的用户数据,该数据表的结构以及该数据表 ...

  10. PhpStorm函数注释的设置

    首先,PhpStorm中文件.类.函数等注释的设置在:setting->Editor->FIle and Code Template->Includes下设置即可,其中方法的默认是这 ...