zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    设计模式读书笔记-----适配器模式
    设计模式读书笔记-----命令模式
    一种另类的解决URL中文乱码问题--对中文进行加密、解密处理
    设计模式读书笔记-----单例模式
    Mysql的一些小知识点
    2-逻辑题二
    1-逻辑题一
    12-1054. 求平均值
    11-1048.数字加密
    10-string类的length()返回值一起的问题
  • 原文地址:https://www.cnblogs.com/Deribs4/p/4712713.html
Copyright © 2011-2022 走看看