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. 输出错误long类型

    Microsoft Visual C++ 输出不了long 类型的数字怎么办? 在C/C++中,64为整型一直是一种没有确定规范的数据类型.现今主流的编译器中,对64为整型的支持也是标准不一,形态各异 ...

  2. Brett Beauregard大神的Arduino PID算法

    大神的全部PID http://brettbeauregard.com/blog/category/pid/ Improving the Beginner’s PID – Introduction I ...

  3. Java 判断密码是否是大小写字母、数字、特殊字符中的至少三种

    public class CheckPassword { //数字 public static final String REG_NUMBER = ".*\\d+.*"; //小写 ...

  4. java8-date和timeAPI

    一 我们为什么要学习 java.timeAPI 原先的Date and Calendar 类的api比较复杂,不易于理解,应用起来不是很灵活. Calendar 是个线程不安全的类会导致SimpleD ...

  5. 《Dotnet9》系列-开源C# WPF控件库1《MaterialDesignInXAML》强力推荐

    时间如流水,只能流去不流回! 点赞再看,养成习惯,这是您给我创作的动力! 本文 Dotnet9 https://dotnet9.com 已收录,站长乐于分享dotnet相关技术,比如Winform.W ...

  6. Leetcode题解 - 贪心算法部分简单题目代码+思路(860、944、1005、1029、1046、1217、1221)

    leetcode真的是一个学习阅读理解的好地方 860. 柠檬水找零 """ 因为用户支付的只会有5.10.20 对于10元的用户必须找一个5 对于20元的用户可以找(三 ...

  7. javascript树形汇总金额

    在开发企业应用的时候总会遇到树形汇总金额的场景,即将树形的列表中的叶子节点(没有子节点)的金额汇总到父节点上. 这种需求一般是在前端进行处理,即使用JavaScript处理,因为叶子节点的金额可能是不 ...

  8. 远程桌面MATLAB启动失败问题解决

    博客:博客园 | CSDN | blog 远程桌面打开MATLAB会报错,解决办法,打开matlab的licenses路径,如matlab/R2017b/licenses/,路径下存有license文 ...

  9. 弹性盒子中的order

    order order 属性 设置或检索弹性盒模型对象的子元素出现的順序.. 注意:如果元素不是弹性盒对象的元素,则 order 属性不起作用. <!DOCTYPE html> <h ...

  10. HtEditor使用总结

    最近在公司学习到ht编辑器的使用,关于使用方法上总结了一下,避免入坑.ht是做大屏数据可视化比较好的一款软件,不过多介绍,官网上有具体使用方法和展示样例,这里我整理一下我用的最多的功能. ##1.如何 ...