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

  1. 3 2 3
    1 2
    1 3
    1 2 3

Sample Output

  1. 1
    0
    0

层次遍历,查找图中有多少个联通的子图。注意数组mTree的使用,当mTree中的元素为-1时,表示该节点为树根,当mTree中的元素为0时,表示改点被占有,其余大于0的值表示该点的父节点。

代码

  1.  1 #include <stdio.h>
  2.  2 #include <string.h>
  3.  3 
  4.  4 int find_num_of_tree(int,int);
  5.  5 int map[][];
  6.  6 int mTree[];
  7.  7 int flag[];
  8.  8 int queue[];
  9.  9 int main()
  10.  {
  11.      int N,M,K;
  12.      int checked;
  13.      int i;
  14.      int s,e;
  15.      while(scanf("%d%d%d",&N,&M,&K) != EOF){
  16.          memset(map,,sizeof(map));
  17.          for(i=;i<M;++i){
  18.              scanf("%d%d",&s,&e);
  19.              map[s][e] = map[e][s] = ;
  20.          }
  21.          for(i=;i<K;++i){
  22.              scanf("%d",&checked);
  23.              printf("%d\n",find_num_of_tree(N,checked)-);
  24.          }
  25.      }
  26.      return ;
  27.  }
  28.  
  29.  int find_num_of_tree(int n,int checked)
  30.  {
  31.      if(< )
  32.          return ;
  33.      int base,top;
  34.      int i,j;
  35.      memset(flag,,sizeof(flag));
  36.      flag[checked] = ;
  37.      for(i=;i<=n;++i){
  38.          mTree[i] = -;
  39.      }
  40.      mTree[checked] = ;
  41.      for(i=;i<=n;++i){
  42.          if(flag[i])
  43.              continue;
  44.          queue[] = i;
  45.          base = ;
  46.          top = ;
  47.          while(base < top){
  48.              int x = queue[base++];
  49.              flag[x] = ;
  50.              for(j=;j<=n;++j){
  51.                  if(!flag[j] && map[x][j]){
  52.                      queue[top++] = j;
  53.                      mTree[j] = x;
  54.                  }
  55.              }
  56.          }
  57.      }
  58.      int num = ;
  59.      for(i=;i<=n;++i){
  60.          if(mTree[i] == -)
  61.              ++num;
  62.      }
  63.      return num;
  64.  }

PAT 1013的更多相关文章

  1. PAT 1013 Battle Over Cities

    1013 Battle Over Cities (25 分)   It is vitally important to have all the cities connected by highway ...

  2. PAT 1013 数素数 (20)(代码)

    1013 数素数 (20)(20 分) 令P~i~表示第i个素数.现任给两个正整数M <= N <= 10^4^,请输出P~M~到P~N~的所有素数. 输入格式: 输入在一行中给出M和N, ...

  3. PAT——1013. 数素数

    令Pi表示第i个素数.现任给两个正整数M <= N <= 104,请输出PM到PN的所有素数. 输入格式: 输入在一行中给出M和N,其间以空格分隔. 输出格式: 输出从PM到PN的所有素数 ...

  4. PAT 1013 Battle Over Cities(并查集)

    1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...

  5. pat 1013 Battle Over Cities(25 分) (并查集)

    1013 Battle Over Cities(25 分) It is vitally important to have all the cities connected by highways i ...

  6. PAT 1013 Battle Over Cities (dfs求连通分量)

    It is vitally important to have all the cities connected by highways in a war. If a city is occupied ...

  7. PAT 1013. 数素数 (20)

    令Pi表示第i个素数.现任给两个正整数M <= N <= 104,请输出PM到PN的所有素数. 输入格式: 输入在一行中给出M和N,其间以空格分隔. 输出格式: 输出从PM到PN的所有素数 ...

  8. PAT 1013 数素数

    https://pintia.cn/problem-sets/994805260223102976/problems/994805309963354112 令P~i~表示第i个素数.现任给两个正整数M ...

  9. PAT 1013 Battle Over Cities DFS深搜

    It is vitally important to have all the cities connected by highways in a war. If a city is occupied ...

随机推荐

  1. 打印web页面指定区域的三种方法

    本文和大家分享一下web页面实现指定区域打印功能的三种方法,一起来看下吧. 第一种方法:使用CSS 定义一 个.noprint的class,将不打印的内容放入这个class内. 代码如下: <s ...

  2. Idea无法DEBUG的问题

    最近对web工程进行debug,突然发现无法进入断点了,原来以为是maven的问题,后来发现是tomcat环境变量导致的. 使用tomcat时经常碰到内存不足的情况,我们会对catalina.bat类 ...

  3. 【转】 Android 开发 之 JNI入门 - NDK从入门到精通

    原文网址:http://blog.csdn.net/shulianghan/article/details/18964835 NDK项目源码地址 : -- 第一个JNI示例程序下载 : GitHub  ...

  4. jquery滚动条

    查看demo: 下载Demo

  5. Asp.net MVC 4 异步方法

    今天我们来看一下,同样功能在 Asp.net MVC 4 下的实现,基于.net framework 4.5 下的async支持,让我们的代码更加简单,看下面片断代码名叫Index的Action方法: ...

  6. Unicode中跟汉字相关的一些内容的总结陈词

    UniHan 这几天琢磨着怎么方便的给汉字注音, 因为要知道具体哪些Unicode是给汉字用的, 就读了读Unicode的官方文档. 目前unicode已经发展到了7.0. 不看不知道, 发现Unic ...

  7. 2014年25 款最新最棒的jQuery插件

    网络上提供了大量非常有用的 jQuery 插件,帮助大家完善网站的体验.所以我们在这里收集了 2014 年发布的,并且是非常有用的插件,希望能帮助大家找到自己需要并且喜欢的,提升网站的质量! HAMM ...

  8. HDU 3289 Cat VS Dog (二分匹配 求 最大独立集)

    题意:每个人有喜欢的猫和不喜欢的狗.留下他喜欢的猫他就高心,否则不高心.问最后最多有几个人高心. 思路:二分图求最大匹配 #include<cstdio> #include<cstr ...

  9. Magento 前台的logo更改

    进入后台: 系统-配置, 然后选择左栏的“设计”, 选择右栏的“页眉”里面, 一般logo的路径在: skin/frontend/base/default/images/media/logo.png ...

  10. PHP与MySQL动态网站开发2

    处理HTML表单 <input type="text" name="city"/> 通过$_REQUEST['city'];访问,$_REQUEST ...