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 ...
随机推荐
- pygame编程之font模块
方法一:pygame.font.Font(file, size=-1) 参数file:采用字体文件的路径,如果file参数设置为None则默认采用系统自带字体,如果自带字体文件无法打开就会报错: 参数 ...
- 第三次web作业
em是相对长度单位.相对于当前对象内文本的字体尺寸.如当前对行内文本的字体尺寸未被人为设置,则相对于浏览器的默认字体尺寸,最初是指字母M的宽度,故名em.现指的是字符宽度的倍数,用法类似百分比,如:0 ...
- Salesforce的公式和验证规则
公式 在Salesforce中,有些功能不需要从数据库中直接读取的数据,而是基于这些数据之间的关系来做出判断.这种情况下就要用到"公式"功能. 公式的概念和Excel中的公式类似, ...
- Python 利用Python操作excel表格之xlwt介绍
利用Python操作excel表格之xlwt介绍 by:授客 QQ:1033553122 直接上代码 案例1 #!/usr/bin/env python # -*- coding:utf-8 ...
- Linux 操作系统下为网卡配置ip
Linux操作系统下为网卡配置ip by:授客 QQ:1033553122 1. Linux单一网卡设置多IP的配置方法 在Linux下网卡接口逻辑名被称为eth0,eth1,eth2,..... ...
- Java并发编程(二)同步
在多线程的应用中,两个或者两个以上的线程需要共享对同一个数据的存取.如果两个线程存取相同的对象,并且每一个线程都调用了修改该对象的方法,这种情况通常成为竞争条件. 竞争条件最容易理解的例子就是:比如 ...
- 一种快速部署开发用oracle的办法
前段时间工作中需要在不少开发环境中快速提供开发可用的oracle环境,由于一一培训并部署原生oracle人力和时间成本过高,后来使用docker版本oracle,大大方便了开发工作的快速启动,方法记录 ...
- HBase中此类异常解决记录org.apache.hadoop.ipc.RemoteException(java.io.IOException):
ERROR: Can't get master address from ZooKeeper; znode data == null 一定注意这只是问题的第一层表象,真的问题是: File /hb ...
- PHP类多继承的替代方案Traits
概述 traits是PHP5.4新进入的特性,其目的就是解决PHP的类不能多继承的问题.Traits不是类!不能被实例化.可以理解为一组能被不同的类都能调用到的方法集合.只需要在类中使用关键词use引 ...
- is_valid校验机制
先来归纳一下整个流程 (1)首先is_valid()起手,看seld.errors中是否值,只要有值就是flase(2)接着分析errors.里面判断_errors是都为空,如果为空返回self.fu ...