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 cit**y1-cit**y2 and cit**y1-cit**y3. Then if cit**y1 is occupied by the enemy, we must have 1 highway repaired, that is the highway cit**y2-cit**y3.
Input Specification:
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 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
思路
给定一个图后,封锁一个点,若要使得剩下的点联通,求添加道路的最少数量。
用flag数组标记一个点是否访问过,用dfs可以求出封锁某点后,该图的联通分量数block(未连通的块)。答案就是block-1
。
最后一组数据会超时,可以用个map记录下封锁某点的答案,可以重复使用。
此题还可以继续优化时间复杂度,但是PAT应该可以直接过了。
代码
#include <stdio.h>
#include <string>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <map>
#include <limits.h>
using namespace std;
int maze[1000+10][1000+10];
int N, M, K;
int block = 0;
bool flag[1000+10];
void dfs(int index){
flag[index] = 1;
for(int i = 1; i <= N; i++){
if(maze[i][index] && !flag[i]) dfs(i);
}
}
map<int, int> mmp;
int main() {
cin >> N >> M >> K;
int e, s;
for(int i = 0; i < M; i++){
cin >> e >> s;
maze[e][s] = 1;
maze[s][e] = 1;
}
for(int i = 0; i < K; i++){
cin >> e;
block = 0;
if(mmp.count(e)){
cout << mmp[e] << endl;
continue;
}
memset(flag, 0, sizeof(flag));
flag[e] = 1;
for(int j = 1; j <= N; j++){
if(!flag[j]){
block++;
dfs(j);
}
}
mmp[e] = block - 1;
cout << block - 1 << endl;
}
return 0;
}
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 (25)-求联通分支个数
题目就是求联通分支个数删除一个点,剩下联通分支个数为cnt,那么需要建立cnt-1边才能把这cnt个联通分支个数求出来怎么求联通分支个数呢可以用并查集,但并查集的话复杂度是O(m*logn*k)我这里 ...
- 图论 - 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 ...
- PAT甲级1013. Battle Over Cities
PAT甲级1013. Battle Over Cities 题意: 将所有城市连接起来的公路在战争中是非常重要的.如果一个城市被敌人占领,所有从这个城市的高速公路都是关闭的.我们必须立即知道,如果我们 ...
- 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 ...
随机推荐
- int*v=newint[src.cols*4]
在学习:使用OpenCV2.x计算图像的水平和垂直积分投影中,有下图一种代码: 对比上面两个代码对于同一张图片求得的结果会发现不同: 为什么会出现这个原因呢?不知道为啥这样初始化? 首先查看一下图片深 ...
- eclipse无法启动报错、打开Eclipse报错、Eclipse无法打开
有时候在新的电脑中安装Eclipse的时候总会报一些错误,要么就是环境变量没配好.要么就是JDK没装.要么就是JDK位数与Eclipse位数不同(版本),反正会报一些奇奇怪怪恶心的问题,我第一次装的时 ...
- Jquery change方法
jQuery 事件 - change() 方法 当输入域发生变化时改变其颜色: $(".field").change(function(){ $(this).css("b ...
- 2019牛客多校第七场 F Energy stones 树状数组+算贡献转化模拟
Energy stones 题意 有n块石头,每块有初始能量E[i],每秒石头会增长能量L[i],石头的能量上限是C[i],现有m次时刻,每次会把[s[i],t[i]]的石头的能量吸干,问最后得到了多 ...
- 【Python】字符串操作符
- linq和扩展方法
c#的扩展方法 1.必须是在一个非嵌套.非泛型的静态类中的静态方法 2.至少一个参数,第一个参数附加this,不能有其他修饰符如out.ref 3.第一个参数不能是指针类型 上面例子是自定义的一个扩展 ...
- C++学习书籍评价
1.C++程序设计-现代方法 本书非常适合学习了C语言基础,想跨步到C++学习的同学,前20章都是C基础的回顾,简直不要太简单,后面的课后习题花了半个小时做完了,没怎么出错,嗯,我的C语言基础还是可以 ...
- (转)Hadoop 简介
转自:http://www.open-open.com/lib/view/open1385685943484.html mapreduce是一种模式,一种什么模式呢?一种云计算的核心计算模式,一种分布 ...
- 在多租户(容器)数据库中如何创建PDB:方法5 DBCA远程克隆PDB
基于版本:19c (12.2.0.3) AskScuti 创建方法:DBCA静默远程克隆PDB.将 CDB1 中的 PDB1 克隆为 CDB2 中的 ERP2 对应路径:Creating a PDB ...
- css div布局示例1(head-main-footer)
很简单的文档流布局 <!doctype html> <html lang="en"> <head> <meta charset=" ...