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 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 分)的更多相关文章
- 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 ...
 - 1013 Battle Over Cities (25分) DFS  |  并查集
		
1013 Battle Over Cities (25分) It is vitally important to have all the cities connected by highways ...
 - 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 ...
 - 【PAT甲级】1013 Battle Over Cities (25 分)(并查集,简单联通图)
		
题意: 输入三个整数N,M,K(N<=1000,第四个数据1e5<=M<=1e6).有1~N个城市,M条高速公路,K次询问,每次询问输入一个被敌军占领的城市,所有和该城市相连的高速公 ...
 - PAT 解题报告 1013. Battle Over Cities (25)
		
1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...
 - PAT A 1013. Battle Over Cities (25)【并查集】
		
https://www.patest.cn/contests/pat-a-practise/1013 思路:并查集合并 #include<set> #include<map> ...
 - 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 ...
 - 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 ...
 - 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 ...
 
随机推荐
- Asp.Net Core 中IdentityServer4 授权中心之自定义授权模式
			
一.前言 上一篇我分享了一篇关于 Asp.Net Core 中IdentityServer4 授权中心之应用实战 的文章,其中有不少博友给我提了问题,其中有一个博友问我的一个场景,我给他解答的还不够完 ...
 - 基于微信小程序的租房小程序
			
乐直租全国租房小程序前端 房源分钟上传,可快捷联系房东的小程序. 该小程序操作简单,布局清新,欢迎 start ~ 传送门:Github 扫码体验: pages: 首页 index 选择发布页 bef ...
 - ggplot2(3) 语法突破
			
3.1 简介 图形图层语法基于Wilkinson的图形语法,并在其基础上添加了许多新功能,使得图形更有表现力,并能完美地嵌入到R的环境中. 图形图层语法使得图形的重复更新变得简单——每次只更新一个特征 ...
 - 聊聊count(*)
			
count(*) 的实现方式 MyISAM 引擎把一个表的总行数存在了磁盘上,因此执行 count(*) 的时候会直接返回这个数,效率很高 InnoDB 引擎就麻烦了,它执行 count(*) 的时候 ...
 - IPv4地址表示法详解
			
在TCP/IP协议中,IP地址是一个最基本的概念,本文就来参考<计算机网络>谢希仁 这本书,总结一下IPv4地址表示法的发展阶段,做个读书笔记. IP地址的编址方法共经过了三个历史阶段: ...
 - 【Weiss】【第03章】练习3.13:桶排序
			
[练习3.13] 利用社会安全号码对学生记录构成的数组排序.编写一个程序进行这件工作,使用具有1000个桶的基数排序并且分三趟进行. Answer: 首先,对社会安全号码不了解的就把它当成一个不超过9 ...
 - springboot基础-redis集群
			
一.pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...
 - python 顺序读取文件夹下面的文件(自定义排序方式)
			
我们在读取文件夹下面的文件时,有时是希望能够按照相应的顺序来读取,但是 file_lists=os.listdir()返回的文件名不一定是顺序的,也就是说结果是不固定的.就比如读取下面这些文件,希望能 ...
 - java常用容器简要性能分析(List。Map。Set)
			
嗯,实习的时候看到这个,感觉蛮好,这里摘录学习,生活加油: 我曾经害怕别人嘲笑的目光,后来,发现他们的目光不会在我身上停留太久,人们更愿意把目光放在自己身上. 知乎上看到,讲给自己. List Lis ...
 - CompletableFuture异步编排
			
什么是CompletableFuture CompletableFuture是JDK8提供的Future增强类.CompletableFuture异步任务执行线程池,默认是把异步任务都放在ForkJo ...