PAT A1013 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 (<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 Knumbers, 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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <string.h>
using namespace std;
const int maxn = ;
int g[maxn][maxn],l[maxn][maxn];
int n, m, k, lost;
bool vis[maxn];
void dfs(int i) {
if (vis[i] == false) {
vis[i] = true;
for (int j = ; j <= n; j++) {
if (l[i][j] != ) {
dfs(j);
}
}
}
}
int dfsTrave() {
int count = ;
fill(vis, vis + maxn, false);
vis[lost] = true;
for (int i = ; i <= n; i++) {
if (vis[i] == false) {
dfs(i);
count++;
}
}
return count;
}
int main() {
fill(g[], g[] + maxn * maxn, );
scanf("%d %d %d", &n,&m,&k);
for (int i = ; i < m; i++) {
int c1, c2;
scanf("%d %d\n",&c1, &c2);
g[c1][c2] = ;
g[c2][c1] = ;
}
for (int i = ; i < k; i++) {
scanf("%d", &lost);
//copy(l[0], l[0] + maxn * maxn, g);
memcpy(l, g, maxn*maxn * sizeof(int));
for (int j = ; j <= n; j++) {
if (l[lost][j] != ) {
l[lost][j] = ;
l[j][lost] = ;
}
}
/*for (int i = 0; i <= 5+n; i++) {
for (int j = 0; j <= 5+n; j++) {
printf("%d ", l[i][j]);
}
printf("\n");
}*/
int res = dfsTrave()-;
printf("%d\n", res);
}
system("pause");
}
注意点:仔细分析题目后发现其实考的就是一个图有几个联通块,用dfs遍历一遍就可以了。二维数组的复制要用到 string.h 头文件下的 memcpy 函数,不知道为什么copy函数一直报错
PAT A1013 Battle Over Cities (25 分)——图遍历,联通块个数的更多相关文章
- 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 分) 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 (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 ...
- 【PAT甲级】1013 Battle Over Cities (25 分)(并查集,简单联通图)
题意: 输入三个整数N,M,K(N<=1000,第四个数据1e5<=M<=1e6).有1~N个城市,M条高速公路,K次询问,每次询问输入一个被敌军占领的城市,所有和该城市相连的高速公 ...
- 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 ...
- A10131013 Battle Over Cities (25分)
一.技术总结 这一题是考查图的知识,题目的意思要理解清楚,就是考查统计图中连通块的数量,也就是没有一个结点后. 怎么删除该结点,并且统计连通块的数量成为问题解决的关键,这里可以当访问到结点时,直接返回 ...
- 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 ...
- PAT 解题报告 1013. Battle Over Cities (25)
1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...
随机推荐
- Java,第16天,属性与方法;
public class 类名{ private double 财产 = 0://设一个财产的属性: public void 一个月工资(){ this.财产 +=4500: }//设一个方法增加财产 ...
- Maven远程仓库的各种配置
1.远程仓库的配置 在平时的开发中,我们往往不会使用默认的中央仓库,默认的中央仓库访问的速度比较慢,访问的人或许很多,有时候也无法满足我们项目的需求,可能项目需要的某些构件中央仓库中是没有的,而在其他 ...
- java-两个整数变量的交换-不需要定义第三方变量
代码如下: class Example { public static void main(String[] args) { /* * 位异或运算符的特点 * ^的特点:一个数据对另一个数据位异或两次 ...
- 秒懂AOP
AOP(Aspect Orient Programming),作为面向对象编程的一种补充,广泛应用于处理一些具有横切性质的系统级服务,如事务管理.安全检查.缓存.对象池管理等.AOP 实现的关键就在于 ...
- JAVA的高并发基础认知 二
一.JAVA高级并发 1.5JDK之后引入高级并发特性,大多数的特性在java.util.concurrent 包中,是专门用于多线程发编程的,充分利用了现代多处理器和多核心系统的功能以编写大规模并发 ...
- HDU5037(SummerTrainingDay01-C)
Frog Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Subm ...
- echarts2.0仪表盘
option = { backgroundColor: '#0e0b2a', tooltip : { formatter: "{a} <br/>{b} : {c}%" ...
- 【读书笔记】iOS-storyboard-两个场景间的切换(二)
接着上一节 一,在storybord画布上面,新增加一个场景,即拖动一个View Controller到画布上面,同时建立一个button,名字为secondButton.如图所示. 二,点击第一个按 ...
- MVC与单元测试实践之健身网站(七)-添加计划
计划的制定涉及到周期-动作包-动作的关联操作,在上一篇<计划的添加与重置>完成了周期的设置.动作包的添加,现在要完成的是动作的添加操作. 一 具体功能 a) 在选定了一个大周期具有的天数 ...
- Android中使用progurad混淆代码
第一步,取消project.properties中关于progurad的注释,开启progurad,默认的配置文件会被加载进来. proguard.config=${sdk.dir}/tools/pr ...