• 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. }

  • 相关阅读:
    Redis:Redis的数据结构、key的操作命令
    SAAS智能打印设计->自定义打印模板
    闲人闲谈PS之二十九——关于精确统计工程合同产值问题
    Springboot操作mongodb的两种方法:MongoTemplate和MongoRepository
    Service Mesh之Istio部署bookinfo
    Java 中数据结构LinkedList的用法
    危险化工品出口注意事项及法规要求_箱讯科技
    VMware NSX 4.0 -- 网络安全虚拟化平台
    数据分析篇-数据认知分析
    python给企微发消息
  • 原文地址:https://blog.csdn.net/CY_hhxx/article/details/132666006