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深入研究】8、Java中Unsafe类详解
java不能直接访问操作系统底层,而是通过本地方法来访问.Unsafe类提供了硬件级别的原子操作,主要提供了以下功能: 1.通过Unsafe类可以分配内存,可以释放内存: 类中提供的3个本地方法all ...
- How do I close a single buffer (out of many) in Vim?
I open several files in Vim by, for example, running vim a/*.php which opens 23 files. I then make m ...
- FormData对象的使用
一.概述 FormData类型是XMLHttpRequest 2级定义的,它是为序列化表以及创建与表单格式相同的数据提供便利. 作用:1.利用一些键值对来模拟一系列表单控件:即将form中的所有表单元 ...
- JS数组迭代方法
先说 every()和 some(),它们都用于查询数组中的项是否满足某个条件. every(): var numbers = [1,2,3,4,5,4,3,2,1]; var everyResult ...
- npm 全局执行 update 、 outdated 出现 npm-debug.log 404 错误的问题
想要执行一次全局更新,发现屡次报错: # npm update -g 提示的错误信息包含如下内容: npm ERR! code E404 npm ERR! 404 Registry returned ...
- 【代码笔记】Web--使用Chrome来查看网页源代码
一,用Chrome打开百度页面,如图所示. 二,鼠标右键--->显示网页源代码--->如图所示. 三,鼠标右键--->检查---->如图所示.此时可以通过Device来看不同设 ...
- Salesforce的站点和社区
社区 Salesforce提供了"社区"功能.建立一个"社区"相当于建立一个前端的网站,让用户.客户.其他合作伙伴等浏览并使用其中的内容. 启用Salesfor ...
- 【转】MySQL:日期函数、时间函数总结(MySQL 5.X)
转自:http://www.cnblogs.com/she27/articles/1377089.html 一.MySQL 获得当前日期时间 函数1.1 获得当前日期+时间(date + time)函 ...
- 关于CSS的一些基础内容
最近用到了CSS,刚好学学.CSS(Cascading Style Sheet)中文名层叠样式表,用于为html文档添加样式控制,也是一种计算机语言. 一.CSS语法a)CSS规则由选择器和声明组成, ...
- ImageButton和ImageView设置点击透明区域不响应
思路 ImageView和ImageButton都可以设置background和设置src,两者的区别自行度娘.由于两者的不同,获取它们的图片资源的方法也不同.倘若设置的是background,那么需 ...