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. P2P编程(十)

    此为网络编程的一个系列,后续会把内容补上....

  2. Java [Leetcode 43]Multiply Strings

    题目描述: Given two numbers represented as strings, return multiplication of the numbers as a string. No ...

  3. java的四舍五入算法

    粗力度的四舍五入为整数 package math; public class MathRoundTest { /** * Math类中提供了三个与取整有关的方法:ceil,floor,round, * ...

  4. CF GYM 100703A Tea-drinking

    题意:龙要制作n个茶,每个茶的配方是一个字符串,两个字符串之间有一个差值,这个差值为两个字符串每个对应字母之间差的绝对值的最大值,求制作所有茶时获得的所有差值中的最大值. 解法:克鲁斯卡尔.将茶的配方 ...

  5. HDU 5288 OO’s Sequence

    题意:给一个序列,函数f(l, r)表示在[l, r]区间内有多少数字不是其他数字的倍数,求所有区间的f(l, r)之和. 解法:第一次打多校……心里还有点小激动……然而一道签到题做了俩点……呜呜呜… ...

  6. ln (link)命令

    ln是linux中又一个非常重要命令,它的功能是为某一个文件在另外一个位置建立一个同步的链接.当我们需要在不同的目录,用到相同的文件时,我们不需要在每一个需要的目录下都放一个必须相同的文件,我们只要在 ...

  7. NGINX(三)HASH表

    前言 nginx的hash表有几种不同的种类, 不过都是以ngx_hash_t为基础的, ngx_hash_t是最普通的hash表, 冲突采用的是链地址法, 不过这里冲突的元素不是一个链表, 而是一个 ...

  8. WPF 中资源路径的问题

    WPF 中资源路径的问题 1. 引用当前工程的资源(注意xxxx.png的build action 应设置为Resource 或Embedded Resource) <ImageBrush Im ...

  9. QT中使用 slot 传递 opencv 中得Mat对象以及 使用多线程集成开源代码。

    关于 slot传递 Mat 对象 以前一直是使用 Qtimer 定时器,设定超时后读取 dialog 对象的 Mat成员实现在 UI 里显示图像,发现这样对以后集成其他面向过程的代码增加了复杂度. 所 ...

  10. 《Nagios系统监控实践》一书出版

    本书是我的第一本译著,有此机会实属机缘巧合.虽然使用Nagios只有一年多的时间,但是作为用户,我深感其设计的简洁与高效—没有一丝多余的东西.因为工作的关系,要求对各个领域都有所了解,所以没有仔细地阅 ...