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 city​1​​-city​2​​ and city​1​​-city​3​​. Then if city​1​​ is occupied by the enemy, we must have 1 highway repaired, that is the highway city​2​​-city​3​​.

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 分)——图遍历,联通块个数的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 1013 Battle Over Cities (25分) DFS | 并查集

    1013 Battle Over Cities (25分)   It is vitally important to have all the cities connected by highways ...

  5. 【PAT甲级】1013 Battle Over Cities (25 分)(并查集,简单联通图)

    题意: 输入三个整数N,M,K(N<=1000,第四个数据1e5<=M<=1e6).有1~N个城市,M条高速公路,K次询问,每次询问输入一个被敌军占领的城市,所有和该城市相连的高速公 ...

  6. 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 ...

  7. A10131013 Battle Over Cities (25分)

    一.技术总结 这一题是考查图的知识,题目的意思要理解清楚,就是考查统计图中连通块的数量,也就是没有一个结点后. 怎么删除该结点,并且统计连通块的数量成为问题解决的关键,这里可以当访问到结点时,直接返回 ...

  8. 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 ...

  9. PAT 解题报告 1013. Battle Over Cities (25)

    1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...

随机推荐

  1. 【Spring】30、Spring,SpringMVC用法汇总

    SpringMVC的工作原理图: springMVC中的几个组件: 前端控制器(DispatcherServlet):接收请求,响应结果,相当于电脑的CPU. 处理器映射器(HandlerMappin ...

  2. Linux常用基本命令:三剑客命令之-awk数组用法

    AWK的数组用法跟javascript类似. 1,定义数组 awk 'BEGIN{a[0]="zhangsan";a[1]="lisi";print a[0]} ...

  3. Python全栈学习_day002作业

    Day2作业及默写 1.判断下列逻辑语句的True,False. 1)1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 & ...

  4. 网络安全之sql注入

    1.何为Sql注入? 所谓SQL注入,就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令.具体来说,它是利用现有应用程序,将(恶意的)SQ ...

  5. [HTML/CSS]有一种节点叫做文本节点

    HTML可以看成是由节点(node)组成的树结构 我们一般都是在<p>节点里面写字符串. 在上图中,<p>节点和字符串之间有一个text, 这个text就是文本节点. 我们可以 ...

  6. 2017-12-21 FriceEngine试用与API中文化

    早先就听闻FriceEngine已有中文接口版本, 可惜没有机会尝试. 经原作者 @大笨蛋千里冰封 (Github账号 @ice1000 )建议, 在FriceEngine的DSL封装的接口基础上, ...

  7. 《数据库系统概念》11-扩展的E-R特性

    ​虽然基本的E-R特性可以应对大部分数据库建模,但为了使用方便,也提供扩展的E-R特性 一.特化Specialization有时一个实体集会保护若干子集,这些子集各自具有不同的属性.比如person可 ...

  8. 【转】HTTP协议之multipart/form-data请求分析

    原文链接:http://blog.csdn.net/five3/article/details/7181521 首先来了解什么是multipart/form-data请求: 根据http/1.1 rf ...

  9. 【Java入门提高篇】Day23 Java容器类详解(六)HashMap源码分析(中)

    上一篇中对HashMap中的基本内容做了详细的介绍,解析了其中的get和put方法,想必大家对于HashMap也有了更好的认识,本篇将从了算法的角度,来分析HashMap中的那些函数. HashCod ...

  10. Dbvisualizer软件设置SQL语句的自动提示功能

    之前从来没有使用过Dbvisualizer软件,用起来之后发现比mysqlfront不是好一点.之前一直不知道sql语句的自动提示功能,只能一个个单词输入,而且不是默认设置.之后在网上找到了怎么设置, ...