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

题目意思:n个城市之间有m条道路,当其中的一个城市被敌人占领了,那么该城市通往其他城市的道路都将关闭,问剩下的几个城市至少需要再添加多少条道路,才能重新让这些城市连通起来。

解题思路:如果将这道题映射到图论上,就可以看成原来的一个连通图,由于某一点的缺失,其关联边缺失而将连通图拆分成几个连通分量,问需要再添加多少条边才能重新变为连通图。其实只需要统计连通分量的个数就行了,因为将a连通分量抽象成点,仅需要a-1条边就可以使其连通。而统计连通分量的个数则直接DFS就可以了,利用邻接矩阵存图,对于每一个被占领的城市,去除这个城市结点,就是把它标记为已经访问过,这样在DFS的时候,对于所有未访问的结点进行遍历,就能将所有的连通分量的个数统计出来了。注意需要k次查询被占领的城市,因此每次都需要将vis数组置零。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int mp[][];
int vis[];
int n,m,k;
void DFS(int x)
{
int i;
vis[x]=;
for(i=;i<=n;i++)
{
if(vis[i]==&&mp[x][i]==)
{
DFS(i);
}
}
}
int main()
{ int i,j, a,cnt;
int v,w;
scanf("%d%d%d",&n,&m,&k);
for(i=;i<m;i++)
{
scanf("%d%d",&v,&w);
mp[v][w]=mp[w][v]=;
}
for(i=;i<k;i++)
{
memset(vis,,sizeof(vis));
scanf("%d",&a);
cnt=;
vis[a]=;
for(j=;j<=n;j++)
{
if(vis[j]==)
{
DFS(j);
cnt++;
}
}
printf("%d\n",cnt-);
}
return ;
}

PAT 1013 Battle Over Cities DFS深搜的更多相关文章

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

  2. PAT 1013 Battle Over Cities

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

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

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

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

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

  5. PAT甲级1013. Battle Over Cities

    PAT甲级1013. Battle Over Cities 题意: 将所有城市连接起来的公路在战争中是非常重要的.如果一个城市被敌人占领,所有从这个城市的高速公路都是关闭的.我们必须立即知道,如果我们 ...

  6. 图论 - PAT甲级 1013 Battle Over Cities C++

    PAT甲级 1013 Battle Over Cities C++ It is vitally important to have all the cities connected by highwa ...

  7. PAT 解题报告 1013. Battle Over Cities (25)

    1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...

  8. 1013 Battle Over Cities (25分) DFS | 并查集

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

  9. PAT 甲级 1013 Battle Over Cities (25 分)(图的遍历,统计强连通分量个数,bfs,一遍就ac啦)

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

随机推荐

  1. 2019 ECfianl

    这真的是一次失败的旅行,不过也有所收获. 我也是醉了,真的是,热身赛的时候对面队伍把B题快快的过了,就开始在那里说个不停,真的是超级烦(以为他们是个大佬队) 第二天正式赛了,他们过了两题,就没有了,( ...

  2. Python批量更新模块的方法【面试必学】

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者:ranchlin      小编的环境为win10+python 3. ...

  3. Python基础-day02-1

    判断(if)语句 目标 开发中的应用场景 if 语句体验 if 语句进阶 综合应用 01. 开发中的应用场景 生活中的判断几乎是无所不在的,我们每天都在做各种各样的选择,如果这样?如果那样?-- 程序 ...

  4. Supermap/Cesium 开发心得----定位

    SuperMap的WebGL是基于开源JS库Cesium做的修改而形成的产品,理论上用起来大同小异,如果在有不一样的地方再看,基本上还是与Cesium的接口名称和结构是一样的. 定位方法有基于Cesi ...

  5. Linux-3.14.12内存管理笔记【伙伴管理算法(4)】

    此处承接前面未深入分析的页面释放部分,主要详细分析伙伴管理算法中页面释放的实现.页面释放的函数入口是__free_page(),其实则是一个宏定义. 具体实现: [file:/include/linu ...

  6. EF Core 中处理 1对1 关系

    最近在开发记录感想功能的时候用到了1对1的数据关系,具体情况是这样的,有这样两个1对1的类型 public class Item { public int Id { get; set; } publi ...

  7. openssl 证书请求和签名命令req基本分析

    一 基本概念: OpenSSL 是一个开源项目,其组成主要包括一下三个组件: openssl:多用途的命令行工具 libcrypto:加密算法库 libssl:加密模块应用库,实现了ssl及tls o ...

  8. UWP GridView切换数据时界面闪动

    在选择数据时,比如1-10集,和11-20集切换时,GridView需要切换对应的数据,但是会发生界面闪动. 这是默认的Item Transition导致的. 可以去掉默认的转换效果. <Gri ...

  9. Mysql - 高可用方案之MM+Keepalived

    一.概述 本文将介绍mysql的MM+Keepalived方案.该方案由两个mysql服务器组成,这两个mysql互为主备.其中一台主作为写服务器,另一台主作为读服务器.通过keepalived软件管 ...

  10. Python中Pyyaml模块的使用

    一.YAML是什么 YAML是专门用来写配置文件的语言,远比JSON格式方便. YAML语言的设计目标,就是方便人类读写. YAML是一种比XML和JSON更轻的文件格式,也更简单更强大,它可以通过缩 ...