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. Adding a struct into an array(stackoverflow)

    Question: So lets say I have a struct like this: struct example_structure { int thing_one; int thing ...

  2. response slider

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <t ...

  3. The open source JavaScript graphing library that powers Plotly

    https://plot.ly/javascript/time-series/ https://plot.ly/javascript/ https://github.com/plotly/plotly ...

  4. java.io.IOException No space left on device

    磁盘空间不足 1.df -k,发现程序所在的工作目录/data,居然到了100%. 1604050 free allocated Kb 535144219 used allocated Kb 100 ...

  5. 洛谷P4719 【模板】动态dp(ddp LCT)

    题意 题目链接 Sol 动态dp板子题.有些细节还没搞懂,待我研究明白后再补题解... #include<bits/stdc++.h> #define LL long long using ...

  6. 【代码笔记】Web-ionic-安装及第一个app

    一,下载ionic v1.0.1版本,下载地址为:ionic-v1.0.1.zip. ionic 最新版本下载地址:http://ionicframework.com/docs/overview/#d ...

  7. 【读书笔记】iOS-发布你的应用

    一,添加图标 你的应用在iPhone主屏幕上的标准图标(Icon.png)是57像素*57像素的正方形,PNG格式,不能有透明效果或者图层,72DPI.除些之外,你还可以提供一个同样格式的114像素* ...

  8. android recovery代码修改之原生建议

    我们都知道recovery升级的时候,是根据升级脚本updater-script里面的函数,去一步步执行的,比如mount,format等相关的操作,有时候我们需要增加一些自己特殊的更新的接口,这时候 ...

  9. OneAPM大讲堂 | 基于图像质量分析的摄像头监控系统的实现

    今天咱们要介绍的技术很简单,请看场景: 你在家里安装了几个摄像头想监视你家喵星人的一举一动,然而,就在喵星人准备对你的新包发动攻击的时候,图像突然模糊了.毕竟图像模糊了以后你就没法截图回家和喵当面对质 ...

  10. Spring Boot Actuator认识

    概述 spring-boot-starter-actuator:是一个用于暴露自身信息的模块,主要用于监控与管理. 为了保证actuator暴露的监控接口的安全性,需要添加安全控制的依赖spring- ...