• pat1013. Battle Over Cities (25)


    1013. Battle Over Cities (25)

    时间限制
    400 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue

    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

    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

    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
    3 2 3
    1 2
    1 3
    1 2 3
    
    Sample Output
    1
    0
    0
    

    提交代码

    判断图的联通分量个数。并查集。

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <string>
     4 #include <queue>
     5 #include <stack>
     6 #include <iostream>
     7 using namespace std;
     8 int f[1000];
     9 int getf(int i){
    10     if(f[i]==-1){
    11         return i;
    12     }
    13     return f[i]=getf(f[i]);
    14 }
    15 int main(){
    16     //freopen("D:\INPUT.txt","r",stdin);
    17     int n,m,k;
    18     scanf("%d %d %d",&n,&m,&k);
    19     int *u=new int[m];
    20     int *v=new int[m];
    21     int i,j;
    22     for(i=0;i<m;i++){
    23         scanf("%d %d",&u[i],&v[i]);
    24     }
    25     int pnum;
    26     for(i=0;i<k;i++){
    27         memset(f,-1,sizeof(f));
    28         scanf("%d",&pnum);
    29         for(j=0;j<m;j++){
    30             if(u[j]!=pnum&&v[j]!=pnum){
    31                 int fu=getf(u[j]);
    32                 int fv=getf(v[j]);
    33                 if(fu!=fv){
    34                     f[fv]=fu;
    35                 }
    36             }
    37         }
    38         int count=0;
    39         for(j=1;j<=n;j++){
    40             if(f[j]==-1){
    41                 count++;
    42             }
    43         }
    44         count-=2;
    45         cout<<count<<endl;
    46     }
    47     delete []u;
    48     delete []v;
    49     return 0;
    50 }
  • 相关阅读:
    leetcode -- Add Binary
    leetcode -- 4sum
    leecode -- 3sum Closet
    C++单例模式
    MapReduce的模式、算法和用例
    react 学习笔记
    css3 文字过长用...代替
    scss/css 中添加ie hack
    springmvc 解决跨域CORS
    springmvc 添加Junit4
  • 原文地址:https://www.cnblogs.com/Deribs4/p/4712713.html
Copyright © 2020-2023  润新知