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. I.MX6 android shutdown 内核崩溃

    /**************************************************************************** * I.MX6 android shutdo ...

  2. python20151125

    python exit() python test.py 输入输出 print('2\n2') print(1, 2, 3) name = input() print(':', name) 数据类型和 ...

  3. ↗☻【HTML5秘籍 #BOOK#】第4章 Web表单

    from元素用于组织所有表单部件,负责告诉浏览器把数据提交到哪里,方法是在action属性中提供一个URL.假如你只想在客户端使用JavaScript操作表单,那么只要在action属性中指定一个#即 ...

  4. 先前设定的sa密码忘记了,如何修改sa密码?

    在window身份验证登陆后,新建查询,输入以下代码就可以修改sa密码了: use master go exec sp_password null,'123456','sa' go

  5. 《深入Java虚拟机学习笔记》- 第8章 连接模型

    Java虚拟机学习笔记(八)连接模型

  6. Java String StringBuffer StringBuilder

    String  字符串常量存储在常量区,每次追加操作会创建新的对象: StringBuffer  字符串变量  线程安全 在堆上创建,每次追加操作在原对象上进行操作:  速度 StringBuffer ...

  7. 三相异步电动机过载保护及报警PLC控制

    一.项目所需设备.工具.材料 见表7-1. 表7-1  项目所需设备.工具.材料 二.  训练内容: 1.项目描述 试设计一电动机过载保护程序,要求电动机过载时能自动停止运转,同时发出10秒钟的声光报 ...

  8. ios开发中,A valid provisioning profile for this executable was not found,的解决方法

    手头上的一个ios项目在上架后,再进行时出现了以上的这个错误,这是上架后忘了对一些配置进行复原 我的项目解决方法是: 是上面的这一块出现了问题,图片上的配置是正常的情况,但是上架的时候对其进行了修改, ...

  9. 2015北京网络赛A题The Cats' Feeding Spots

    题意:给你一百个点,找个以这些点为中心的最小的圆,使得这个圆恰好包含了n个点,而且这个圆的边界上并没有点 解题思路:暴力枚举每个点,求出每个点到其他点的距离,取第n大的点,判断一下. #include ...

  10. php 实现文件下载,兼容IE、Firefox、Chrome等浏览器

    一.下载任意文件: Header ( "Content-type: application/octet-stream" ); $ua = $_SERVER ["HTTP_ ...