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. HDU 4009 Transfer water 最小树形图

    分析:建一个远点,往每个点连建井的价值(单向边),其它输水线按照题意建单向边 然后以源点为根的权值最小的有向树就是答案,套最小树形图模板 #include <iostream> #incl ...

  2. eclipse+xdebug

    root@leeyoung-Satellite-M300:/etc/apache2/sites-available# nano 000-default.conf <IfModule dir_mo ...

  3. 【原】Storm配置

    Storm入门教程 1. Storm基础 Storm Storm主要特点 Storm基本概念 Storm调度器 Storm配置 Guaranteeing Message Processing(消息处理 ...

  4. 2016"百度之星" - 初赛(Astar Round2B) 1006 中位数计数

    思路:统计当前数左边比它小|大 i个人,相应右边就应该是比它大|小i个人 l数组表示左边i个人的方案 r表示右边i个人的方案 数组下标不可能是负数所以要加n //#pragma comment(lin ...

  5. Win10系统安装

    2016正月十一来到了学校,刚刚拿到了姐姐的thinkpad,到学校来想重新安装一下系统并且重新磁盘分区. 上一次也安装过win10,不过基本方法已经忘了,制作的U启动盘也不在了. 首先按照http: ...

  6. 析构函数释放内存时出现_BLOCK_TYPE_IS_VALID错误

    错误信息截图: 原因: 1.内存泄漏:所以当程序退出时,系统会收回分配的内存,于是调析构函数,由于内存已被错误地释放,于是就会出现"Debug Assertion Failed"的 ...

  7. linux内存负载分析

    衡量内存负载的一个很重要的指标就是页面置换的频率.当linux系统频繁的对页进行换进换出 的时候,说明物理内存不过,不得不进行频繁的置换页面. 使用vmstat(virtual memory stat ...

  8. berserkJS(大名:狂暴JS / 昵称:疯子JS)

    极分享:高质分享+专业互助=没有难做的软件+没有不得已的加班 http://www.finalshares.com/read-6763?f=g-20

  9. pcb原理图注意问题大汇总

    1. 元器件之间连线用Wire(■)命令而不是用Line( /)命令,虽然从表面上看,两者都是直线,只是颜色稍有些不同,但用Wire(■)命令放置的导线是具有电气特性的,而用Line(/)命令放置的直 ...

  10. mssql游标demo

    declare @billIds varchar(400) declare @billId varchar(40) DECLARE c1 CURSOR FOR select top 5 SaleNo ...