• 1013 Battle Over Cities 甲级 xp_xht123


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

     解题思路:标准的并查集,保持其余城市连通性,需要维修的最少高速公路条数

    标准find函数

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

    注意每次判断的时候需要将p数组初始化

    判断的方法就是每一个输入的数,与原先输入的联通的边的节点对比,如果发现这个数没有出现在某一条边的两个节点中,那么证明这个节点并不与这两个节点相连,需要再判断一下祖宗节点是否一样,不同证明拥有两个联通块。

    1. #include
    2. #include
    3. using namespace std;
    4. const int N = 10100 , M = 500010;
    5. int p[N] , n , m , k;
    6. struct edge
    7. {
    8. int a , b;
    9. }e[M];
    10. int find(int x)
    11. {
    12. if(p[x] != x) p[x] = find(p[x]);
    13. return p[x];
    14. }
    15. int main()
    16. {
    17. scanf("%d %d %d", &n ,&m ,&k);
    18. for(int i = 0;i < m;i ++) scanf("%d %d", &e[i].a, &e[i].b);
    19. while(k --)
    20. {
    21. int num;
    22. scanf("%d", &num);
    23. for(int i = 1;i <= n;i ++) p[i] = i;
    24. int cnt = n - 1;//最多连n-1条边
    25. for(int i = 0;i < m;i ++)
    26. {
    27. int ea = e[i].a , eb = e[i].b;
    28. if(ea != num && eb != num)//num这个城市和ea、eb城市不相连
    29. {
    30. int pa = find(ea) , pb = find(eb);
    31. if(pa != pb) p[pa] = pb , cnt --;//连接两个连通块
    32. }
    33. }
    34. printf("%d\n" , cnt - 1);//注意最后需要减一排除重连的情况
    35. }
    36. return 0;
    37. }

  • 相关阅读:
    申请测试号进行练习
    C++_继承
    FreeRTOS中断与任务之间同步(Error:..\..\FreeRTOS\portable\RVDS\ARM_CM4F\port.c,422 )
    Java从入门到精通-流程控制(二)
    shopee API 接入说明
    安全测试工具分为 SAST、DAST和IAST 您知道吗?
    数据库三范式
    年薪50W+的测试大佬都在用这个:Jmeter 脚本开发之——扩展函数
    Spring的声明式事务
    数据结构与算法:数据结构基础
  • 原文地址:https://blog.csdn.net/xp_xht123/article/details/126388190