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 city​1​​-city​2​​ and city​1​​-city​3​​. Then if city​1​​ is occupied by the enemy, we must have 1 highway repaired, that is the highway city​2​​-city​3​​.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 3 numbers N (<), 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 Specification:

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

题目分析:这是一道利用图的遍历的题 将要去掉的城市节点的visit置为false 然后求出最大连通分量 这个最大连通分量减一就是我们要求的值
 #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int G[][];
bool visit[] = { };
int N, M, K;
void dfs(int v)
{
visit[v] = true;
for (int i = ; i <=N; i++)
{
if (!visit[i] && G[v][i])
dfs(i);
}
}
int main()
{ cin >> N >> M >> K;
int v1, v2;
int v;
int count = ;
for (int i = ; i < M; i++)
{
cin >> v1 >> v2;
G[v1][v2] = G[v2][v1] = ;
}
for (int i = ; i < K; i++)
{
fill(visit, visit + , false);
count = ;
cin >> v;
visit[v] = true;
for (int i = ; i <=N; i++)
{
if (!visit[i])
{
dfs(i);
count++;
}
}
cout << count-<<endl;
}
}

1013 Battle Over Cities (25 分)的更多相关文章

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

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

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

  3. 1013 Battle Over Cities (25分) 图的连通分量+DFS

    题目 It is vitally important to have all the cities connected by highways in a war. If a city is occup ...

  4. 【PAT甲级】1013 Battle Over Cities (25 分)(并查集,简单联通图)

    题意: 输入三个整数N,M,K(N<=1000,第四个数据1e5<=M<=1e6).有1~N个城市,M条高速公路,K次询问,每次询问输入一个被敌军占领的城市,所有和该城市相连的高速公 ...

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

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

  6. PAT A 1013. Battle Over Cities (25)【并查集】

    https://www.patest.cn/contests/pat-a-practise/1013 思路:并查集合并 #include<set> #include<map> ...

  7. 1013. Battle Over Cities (25)

    题目如下: It is vitally important to have all the cities connected by highways in a war. If a city is oc ...

  8. 1013 Battle Over Cities (25)(25 point(s))

    problem It is vitally important to have all the cities connected by highways in a war. If a city is ...

  9. 1013. Battle Over Cities (25)(DFS遍历)

    For example, if we have 3 cities and 2 highways connecting city1-city2 and city1-city3. Then if city ...

随机推荐

  1. vs远程调试iis

    1.在开发电脑上 找到 D:\Software\VS2010\Common7\IDE\Remote Debugger 下面msvsmon.exe所在的两个文件夹x86和x64,使用x86或者x64是根 ...

  2. 第一个爬虫经历----豆瓣电影top250(经典案例)

    因为要学习数据分析,需要从网上爬取数据,所以开始学习爬虫,使用python进行爬虫,有好几种模拟发送请求的方法,最基础的是使用urllib.request模块(python自带,无需再下载),第二是r ...

  3. Flink系列之1.10版流式SQL应用

    随着Flink 1.10的发布,对SQL的支持也非常强大.Flink 还提供了 MySql, Hive,ES, Kafka等连接器Connector,所以使用起来非常方便. 接下来咱们针对构建流式SQ ...

  4. 机器学习——详解经典聚类算法Kmeans

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是机器学习专题的第12篇文章,我们一起来看下Kmeans聚类算法. 在上一篇文章当中我们讨论了KNN算法,KNN算法非常形象,通过距离公 ...

  5. .NET Conf: Xamarin专场会议3.23 开幕

    聚焦于 Xamarin 的 NET Conf 是一项免费的为期一天的直播活动,来自社区和.NET产品团队的演讲者正在使用Xamarin技术构建本机移动应用程序!Xamarin允许您使用C#(而不是Ja ...

  6. require.context('.', true, /\.router\.js/) webpack 编译的时候读取目录文件

    const routerList = [] function importAll (r) { r.keys().map(value => { r(value).default.map(item ...

  7. Jmeter中使用MD5方法

    在现在这家公司做测试的项目有用到鉴权,token的生成方式有使用到md5,具体的请求url和入参就不方便透露,讲一讲使用方法吧! 自带的Jmeter插件中是没有这个md5的所以,我们可以 (1)打开选 ...

  8. 从零开始学习R语言(二)——数据结构之“因素(Factor)”

    本文首发于知乎专栏:https://zhuanlan.zhihu.com/p/60101041 也同步更新于我的个人博客:https://www.cnblogs.com/nickwu/p/125370 ...

  9. mysql数据库中的mybatis中xml解决in不起作用的问题

    在sql语句中,某个字段进行in条件的时候,不起作用, 但是执行语句查询为null数据,但是根据表中数据来看是不可能有null数据的可能性的,所以不知道什么原因导致数据出不来 我因此想到以下解决办法来 ...

  10. 01.为什么要学习Go语言

    为什么互联网世界需要Go语言 世界上已经有太多太多的编程语言了,为什么又出来一个Go语言? 硬件限制:摩尔定律已然失效 摩尔定律:当价格不变时,集成电路上可容纳的元器件的数目,约每隔18-24个月便会 ...