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

3 2 3 
1 2
1 3
1 2 3

Sample Output

1 
0
0

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

代码

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 int find_num_of_tree(int,int);
 5 int map[][];
 6 int mTree[];
 7 int flag[];
 8 int queue[];
 9 int main()
 {
     int N,M,K;
     int checked;
     int i;
     int s,e;
     while(scanf("%d%d%d",&N,&M,&K) != EOF){
         memset(map,,sizeof(map));
         for(i=;i<M;++i){
             scanf("%d%d",&s,&e);
             map[s][e] = map[e][s] = ;
         }
         for(i=;i<K;++i){
             scanf("%d",&checked);
             printf("%d\n",find_num_of_tree(N,checked)-);
         }
     }
     return ;
 }
 
 int find_num_of_tree(int n,int checked)
 {
     if(n < )
         return ;
     int base,top;
     int i,j;
     memset(flag,,sizeof(flag));
     flag[checked] = ;
     for(i=;i<=n;++i){
         mTree[i] = -;
     }
     mTree[checked] = ;
     for(i=;i<=n;++i){
         if(flag[i])
             continue;
         queue[] = i;
         base = ;
         top = ;
         while(base < top){
             int x = queue[base++];
             flag[x] = ;
             for(j=;j<=n;++j){
                 if(!flag[j] && map[x][j]){
                     queue[top++] = j;
                     mTree[j] = x;
                 }
             }
         }
     }
     int num = ;
     for(i=;i<=n;++i){
         if(mTree[i] == -)
             ++num;
     }
     return num;
 }

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. ↗☻【编写可维护的JavaScript #BOOK#】第4章 变量、函数和运算符

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...

  2. class属性添加多个类

    <html> <head> <style type="text/css"> h1.intro { color:blue; text-align: ...

  3. STL序列式容器

    1.vector      空间运用的灵活性.      实现技术——关键是对大小的控制以及重新配置时的数据移动效率.      配置新空间.数据移动.释还旧空间      erase(int pos ...

  4. 文件I/O操作(2)

    lseek函数原型为int lseek(int fd,int offset, int whence),fd为定位文件的描述符,offset为偏移量,如果是正数,则向文件末尾偏移,负数时,则向文件头偏移 ...

  5. 标准的TCP/IP工业以太网-EtherNet/IP

    标准的TCP/IP工业以太网-EtherNet/IP                            徐智穹以太网具有传输速度高.兼容性好.应用广泛等方面的优势,支持几乎所有流行的网络协议,其中 ...

  6. Makefile.am讲解

    Makefile.am详解 实战Makefile.amMakefile.am是一种比Makefile更高层次的规则.只需指定要生成什么目标,它由什么源文件生成,要安装到什么目录等构成.表一列出了可执行 ...

  7. pip 安装python环境及打包

    0.安装虚拟环境 pip install virtualenv   virtualenv env1   source env1/bin/activate   1. 将包依赖信息保存在requireme ...

  8. 题解西电OJ (Problem 1003 -最喜欢的数字)--动态规划

    Description zyf最喜欢的数字是1!所以他经常会使用一些手段,把一些非1的数字变 成1,并为此得意不已.他会且仅会的两种手段是: 1.把某个数m除以某个质数p——当然p必须能整除这个数,即 ...

  9. openstack学习线路指导

    原文链接: http://www.aboutyun.com/thread-7225-1-1.html 网上很多hadoop资料,openstack资料相对较少,这里整理一下,帮助初学者尽快入门. 首先 ...

  10. 我是如何理解Java抽象类和接口的

    在面试中我们经常被问到:Java中抽象类和接口的区别是什么? 然后,我们就大说一通抽象类可以有方法,接口不能有实际的方法啦:一个类只能继承一个抽象类,却可以继承多个接口啦,balabala一大堆,就好 ...