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 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深搜的更多相关文章
- 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 ...
- PAT 1013 Battle Over Cities
1013 Battle Over Cities (25 分) It is vitally important to have all the cities connected by highway ...
- PAT 1013 Battle Over Cities(并查集)
1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...
- pat 1013 Battle Over Cities(25 分) (并查集)
1013 Battle Over Cities(25 分) It is vitally important to have all the cities connected by highways i ...
- PAT甲级1013. Battle Over Cities
PAT甲级1013. Battle Over Cities 题意: 将所有城市连接起来的公路在战争中是非常重要的.如果一个城市被敌人占领,所有从这个城市的高速公路都是关闭的.我们必须立即知道,如果我们 ...
- 图论 - PAT甲级 1013 Battle Over Cities C++
PAT甲级 1013 Battle Over Cities C++ It is vitally important to have all the cities connected by highwa ...
- PAT 解题报告 1013. Battle Over Cities (25)
1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...
- 1013 Battle Over Cities (25分) DFS | 并查集
1013 Battle Over Cities (25分) It is vitally important to have all the cities connected by highways ...
- 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 ...
随机推荐
- Spring Cloud第十篇 | 分布式配置中心Config
本文是Spring Cloud专栏的第十篇文章,了解前九篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Clo ...
- 2016/10/13 oracle中的round()
语法: ROUND(number,num_digits) 其中Number是需要进行四舍五入的数字:Num_digits为指定的位数,按此位数进行四舍五入,如果 num_digits 大于 0,则四舍 ...
- 规范git commit提交记录和版本发布记录
在开发过程中我们一般都会用到git管理代码,在git commit提交代码时我们一般对git commit message随便写点简单的描述,可是随着项目参与人数的增多,发现提交的commit记录越来 ...
- ChinaSys 一些心得
这周不要脸的和老板一起去了 ChinaSys,可以说整个中国搞系统最nb的一批人的学术交流了.一圈报告听下来, 有几点心得,不多,可能也没有那么深刻. 系统领域的开源框架并不多 搞系统和搞AI,搞算法 ...
- TypeScript引入moment.js报错“无法找到moment模块”及解决方法
npm i moment下载moment完成后,在ts文件中引入 import * as moment from "moment"; 结果疯狂报错,未找到moment模块. 一开始 ...
- Wiki语法大全
原文链接:wiki语法大全 编辑一个维客页面十分容易.只要点击页面上方的“编辑本页”或右侧的[编辑]链接即可修改该页,或点击“讨论本页”然后再点击“编辑页面”来讨论该页面.点击后您就会看到一个包含那 ...
- Java连载62-使用throws关键字处理异常
一.处理异常的两种方式 1.所有的编译时异常,要求程序员在编写程序阶段,必须对它进行处理,如果不处理的话,编译就会无法通过,处理异常有两种方法:捕捉和声明抛出. 2.捕捉:try.....catch ...
- 通过谷歌浏览器从官网下载Android Studio
谷歌访问助手安装教程参考:https://www.cnblogs.com/waiwai14/p/11697371.html 谷歌访问助手下载地址:https://pan.baidu.com/s/1YH ...
- Android 避免内存泄漏
什么是内存泄露? 就是该回收的内存由于种种原因没有被回收,还驻留在内存中. 内存泄露有什么影响? 可能一处小小的内存泄露就会导致整个应用卡顿,甚至崩溃. 例子说明: Toast.makeText(Ma ...
- C# 中的基本数值类型
在之前的文章中(地址:https://www.vinanysoft.com/c-sharp-basics/introducing/),以 HelloWorld 程序为基础,介绍 C# 语言.它的结构. ...