zoukankan      html  css  js  c++  java
  • A1013. Battle Over Cities (25)

     

    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 <stdio.h>
     2 #include <stdlib.h>
     3 #include <iostream>
     4 #include <string.h>
     5 #include <math.h>
     6 #include <algorithm>
     7 #include <string>
     8 #include <stack> 
     9 #include <queue>
    10 #include <vector>
    11 using namespace std;
    12 const int maxn=1010;
    13 int father[maxn];
    14 int vis[maxn];
    15 int n,m,k; 
    16 vector<int> G[maxn];
    17 void init()
    18 {
    19     for(int i=1;i<maxn;i++)
    20     {
    21         father[i]=i;
    22         vis[i]=false;
    23     }
    24  } 
    25  int isFather(int x)
    26  {
    27      int tmp=x;
    28      while(x!=father[x])
    29      {
    30       x=father[x];    
    31     }
    32     //至此,x为所在集合的根 
    33     while(tmp!=father[tmp])
    34     {
    35         int z=tmp;
    36         tmp=father[tmp];
    37         father[z]=x;
    38     }
    39     return x;
    40  }
    41  
    42 void Union(int a,int b)
    43 {
    44       int fa=isFather(a);
    45     int fb=isFather(b);
    46     if(fa!=fb)
    47     {
    48         father[fa]=fb;
    49      } 
    50 } 
    51 
    52 
    53 
    54 int main(){
    55   scanf("%d%d%d",&n,&m,&k);
    56   for(int i=0;i<m;i++)
    57   {
    58       int c1,c2;
    59       scanf("%d %d",&c1,&c2);
    60       G[c1].push_back(c2);
    61       G[c2].push_back(c1);
    62   }
    63   for(int i=0;i<k;i++)
    64   {
    65       int tmp;
    66       scanf("%d",&tmp);
    67       //消除tmp点的相连边,之后计算分隔的块数,就可以求得所有的repair边
    68     //遍历 
    69     init();
    70     //合并集合
    71     for(int i=1;i<=n;i++)
    72     {
    73        for(int j=0;j<G[i].size();j++)
    74        {
    75            int v=G[i][j];
    76            if(v!=tmp&&i!=tmp)Union(i,v);
    77        }    
    78     }
    79     
    80     int  block=0;
    81     for(int i=1;i<=n;i++)
    82     {
    83         if(i==tmp)continue;
    84         if(i==father[i])block++;
    85      } 
    86      printf("%d
    ",block-1);
    87   }
    88   return 0;
    89 }
  • 相关阅读:
    Python--前端之HTML
    Python--MySql(主键的创建方式、存储引擎、存储过程、索引、pymsql)
    python--MySql(外键约束、多表查询(*****))
    python--MySql 表记录的操作
    python--MySql
    Python--线程队列(queue)、multiprocessing模块(进程对列Queue、管道(pipe)、进程池)、协程
    Python--同步锁(互斥锁)、死锁(状态)、递归锁、信号量、Event对象
    Python--多线程、多进程常用概念
    Python--基础之socket编程
    ubuntu 安装 flashplayer
  • 原文地址:https://www.cnblogs.com/ligen/p/4330277.html
Copyright © 2011-2022 走看看