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

    C++:

    1. #include <bits/stdc++.h>
    2. using namespace std;
    3. int n,m,k;
    4. int fa[1005];
    5. pair<int,int> a[1000000];
    6. int find(int x)
    7. {
    8. if(fa[x]==x)
    9. {
    10. return x;
    11. }
    12. return fa[x]=find(fa[x]);
    13. }
    14. void merge(int x,int y)
    15. {
    16. int xx=find(x);
    17. int yy=find(y);
    18. fa[xx]=yy;
    19. }
    20. int main()
    21. {
    22. int x,y,xx,cnt;
    23. scanf("%d %d %d",&n,&m,&k);
    24. for(int i=0;i<m;i++)
    25. {
    26. scanf("%d %d",&a[i].first,&a[i].second);
    27. }
    28. for(int i=0;i<k;i++)
    29. {
    30. for(int j=1;j<=n;j++)
    31. {
    32. fa[j]=j;
    33. }
    34. scanf("%d",&xx);
    35. cnt=n-1;
    36. for(int j=0;j<m;j++)
    37. {
    38. x=a[j].first,y=a[j].second;
    39. if(x!=xx&&y!=xx)
    40. {
    41. if(find(x)!=find(y))
    42. {
    43. merge(x,y);
    44. cnt--;
    45. }
    46. }
    47. }
    48. printf("%d\n",cnt-1);
    49. }
    50. return 0;
    51. }

  • 相关阅读:
    爆款视频怎么做?这里或许有答案
    CSP-J第二轮试题-2019年-3题
    Python面向对象(一)
    软件常见设计模式
    Android进程与线程
    节点基础~节点层级
    C++中double类型使用技巧
    MATLAB矩阵
    SpringBootWeb登录认证
    Docker自定义镜像
  • 原文地址:https://blog.csdn.net/CY_hhxx/article/details/132666006