题目

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

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 K numbers, which represent the cities we concern.

Output

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

100

题目分析

已知城市数N,现有公路数M,现有公路信息,测试样例数K,求若某一城市被敌军占领后,需要修建几条公路可以重新连接剩余城市

题目翻译:已知图中顶点数和边,求删除某一顶点后,有多少个连通分量cnt,需cnt-1条边将这些连通分量连接

解题思路

保存图中边信息

  • 邻接矩阵
  • 邻接表

求连通分量数量

  • 深度优先遍历DFS
  • 广度优先遍历BFS
  • 并查集

Code

Code 01(邻接矩阵 DFS)

#include <iostream>
using namespace std;
const int maxn=1010;
int n,g[maxn][maxn],vis[maxn];
void dfs(int c) {
vis[c]=true;
for(int i=1; i<=n; i++) {
if(g[c][i]!=0&&vis[i]==false) {
dfs(i);
}
}
}
int dfs_travel(int c) {
int ans=0;
vis[c]=true;
for(int i=1; i<=n; i++) {
if(vis[i]==false) {
dfs(i);
ans++;
}
}
return ans-1;
}
int main(int argc,char * argv[]) {
int m,k,a,b,c;
scanf("%d %d %d",&n,&m,&k);
for(int i=0; i<m; i++) {
scanf("%d %d",&a,&b);
g[a][b]=1;
g[b][a]=1;
}
for(int i=0; i<k; i++) {
scanf("%d",&c);
fill(vis,vis+maxn,0);
int ans=dfs_travel(c);
printf("%d\n",ans);
}
return 0;
}

Code 02(邻接矩阵 BFS)

#include <iostream>
#include <queue>
using namespace std;
const int maxn=1010;
int n,g[maxn][maxn],inq[maxn];
void bfs(int c) {
queue<int> q;
q.push(c);
inq[c]=true;
while(!q.empty()) {
int p = q.front();
q.pop();
for(int i=1; i<=n; i++) {
if(g[p][i]==1&&inq[i]==false) {
q.push(i);
inq[i]=true;
}
}
}
}
int bfs_travel(int c) {
int ans=0;
inq[c]=true;
for(int i=1; i<=n; i++) {
if(inq[i]==false) {
bfs(i);
ans++;
}
}
return ans-1;
}
int main(int argc,char * argv[]) {
int m,k,a,b,c;
scanf("%d %d %d",&n,&m,&k);
for(int i=0; i<m; i++) {
scanf("%d %d",&a,&b);
g[a][b]=1;
g[b][a]=1;
}
for(int i=0; i<k; i++) {
scanf("%d",&c);
fill(inq,inq+maxn,0);
int ans=bfs_travel(c);
printf("%d\n",ans);
}
return 0;
}

Code 03(邻接表 DFS)

#include <iostream>
#include <vector>
using namespace std;
const int maxn=1010;
int n,vis[maxn];
vector<int> g[maxn];
void dfs(int c) {
vis[c]=true;
for(int i=0; i<g[c].size(); i++) {
if(vis[g[c][i]]==false) {
dfs(g[c][i]);
}
}
}
int dfs_travel(int c) {
int ans=0;
vis[c]=true;
for(int i=1; i<=n; i++) {
if(vis[i]==false) {
dfs(i);
ans++;
}
}
return ans-1;
}
int main(int argc,char * argv[]) {
int m,k,a,b,c;
scanf("%d %d %d",&n,&m,&k);
for(int i=0; i<m; i++) {
scanf("%d %d",&a,&b);
g[a].push_back(b);
g[b].push_back(a);
}
for(int i=0; i<k; i++) {
scanf("%d",&c);
fill(vis,vis+maxn,0);
int ans=dfs_travel(c);
printf("%d\n",ans);
}
return 0;
}

Code 04(邻接表 BFS)

#include <iostream>
#include <queue>
using namespace std;
const int maxn=1010;
int n,inq[maxn];
vector<int> g[maxn];
void bfs(int c) {
queue<int> q;
q.push(c);
inq[c]=true;
while(!q.empty()) {
int p = q.front();
q.pop();
for(int i=0; i<g[p].size(); i++) {
if(inq[g[p][i]]==false) {
q.push(g[p][i]);
inq[g[p][i]]=true;
}
}
}
}
int bfs_travel(int c) {
int ans=0;
inq[c]=true;
for(int i=1; i<=n; i++) {
if(inq[i]==false) {
bfs(i);
ans++;
}
}
return ans-1;
}
int main(int argc,char * argv[]) {
int m,k,a,b,c;
scanf("%d %d %d",&n,&m,&k);
for(int i=0; i<m; i++) {
scanf("%d %d",&a,&b);
g[a].push_back(b);
g[b].push_back(a);
}
for(int i=0; i<k; i++) {
scanf("%d",&c);
fill(inq,inq+maxn,0);
int ans=bfs_travel(c);
printf("%d\n",ans);
}
return 0;
}

Code 05(邻接表 并查集)

#include <iostream>
#include <vector>
#include <set>
using namespace std;
const int maxn=1010;
int n,father[maxn];
vector<int> g[maxn];
/* 并查集 father[n]初始化*/
void initial() {
for(int i=1; i<=n; i++)father[i]=i;
}
/* 并查集 查+路径压缩*/
int find_root(int x) {
int a = x;
while(x!=father[x]) {
x=father[x];
}
// 路径压缩
while(a!=father[a]) {
int temp = a;
a=father[a];
father[temp]=x;
}
return x;
}
/* 并查集 并 */
void Union(int a, int b) {
int fa = find_root(a);
int fb = find_root(b);
if(fa<fb)father[fa]=fb;
else father[fb]=fa;
}
int main(int argc,char * argv[]) {
int m,k,a,b,c;
scanf("%d %d %d",&n,&m,&k);
for(int i=0; i<m; i++) {
scanf("%d %d",&a,&b);
g[a].push_back(b);
g[b].push_back(a);
}
for(int i=0; i<k; i++) {
scanf("%d",&c);
initial();
for(int j=1; j<=n; j++) { //c关联的边除外的所有边进行并查集操作
if(j==c)continue;
for(int e=0; e<g[j].size(); e++) {
if(g[j][e]==c)continue;
Union(j,g[j][e]);
}
}
//
set<int> ans;
for(int j=1; j<=n; j++) { //找到每个连通分量的根节点(唯一标识一个连通分量),加入set集合(保证唯一)
if(j==c)continue;
int f = find_root(j);
ans.insert(f);
}
printf("%d\n",ans.size()-1); //连通分量数-1即为需要建公路数
}
return 0;
}

PAT Advanced 1013 Battle Over Cities (25) [图的遍历,统计连通分量的个数,DFS,BFS,并查集]的更多相关文章

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

  2. PAT A 1013. Battle Over Cities (25)【并查集】

    https://www.patest.cn/contests/pat-a-practise/1013 思路:并查集合并 #include<set> #include<map> ...

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

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

  4. 图论 - PAT甲级 1013 Battle Over Cities C++

    PAT甲级 1013 Battle Over Cities C++ It is vitally important to have all the cities connected by highwa ...

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

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

  6. PAT甲级1013. Battle Over Cities

    PAT甲级1013. Battle Over Cities 题意: 将所有城市连接起来的公路在战争中是非常重要的.如果一个城市被敌人占领,所有从这个城市的高速公路都是关闭的.我们必须立即知道,如果我们 ...

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

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

  9. 1013. Battle Over Cities (25)

    题目如下: It is vitally important to have all the cities connected by highways in a war. If a city is oc ...

随机推荐

  1. Maven是什么,如何使用Maven

    一.简单的小问题? 1.1.假如你正在Eclipse下开发两个Java项目,姑且把它们称为A.B,其中A项目中的一些功能依赖于B项目中的某些类,那么如何维系这种依赖关系的呢? 很简单,这不就是跟我们之 ...

  2. 第1节 HUE:13、hue的下载以及安装配置

    hue的基本介绍:主要是用于与其他各个框架做整合的,提供一个web界面可以供我们去操作其他的大数据框架可以理解为这个hue就是一个与其他各个框架整合的工具,hue本身不提供任何的功能,所有的功能,都是 ...

  3. 文本情感分析(二):基于word2vec、glove和fasttext词向量的文本表示

    上一篇博客用词袋模型,包括词频矩阵.Tf-Idf矩阵.LSA和n-gram构造文本特征,做了Kaggle上的电影评论情感分类题. 这篇博客还是关于文本特征工程的,用词嵌入的方法来构造文本特征,也就是用 ...

  4. mybatis-generator-plugin

    1.背景 这篇文章刚开始想着哪个分类呢?mybatis.idea或是maven呢,最后还是选择了mybatis.最初使用这个逆向工具是在eclipse上,使用的是eclispe上mbg插件执行配置ge ...

  5. 004.Delphi插件之QPlugins,参数传递

    界面如下 插件框架中大量使用了接口的东西,看的眼花缭乱,很多地方只做了申明,具体的实现是在另外的子类. DLL的代码如下 unit ParamTest; interface uses classes, ...

  6. [转]SparkSQL – 有必要坐下来聊聊Join

    转载自网易范欣欣http://hbasefly.com Join背景介绍 Join是数据库查询永远绕不开的话题,传统查询SQL技术总体可以分为简单操作(过滤操作-where.排序操作-limit等), ...

  7. python中pandas数据分析基础3(数据索引、数据分组与分组运算、数据离散化、数据合并)

    //2019.07.19/20 python中pandas数据分析基础(数据重塑与轴向转化.数据分组与分组运算.离散化处理.多数据文件合并操作) 3.1 数据重塑与轴向转换1.层次化索引使得一个轴上拥 ...

  8. java基础源码 (6)--ArrayListt类

    原作出处:https://www.cnblogs.com/leesf456/p/5308358.html 简介: 1.ArrayList是一个数组队列,相当于动态数组.与java中的数组相比,它的容量 ...

  9. poj 2376 Cleaning Shifts 最小区间覆盖

    Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40751   Accepted: 9871 ...

  10. 软件管理-RPM命令管理:安装升级与卸载

    1.包名与包全名 包名 : 操作已经安装的软件包时,使用包名:系统会搜索var/lib/rpm中的数据库 包全名: 操作的包时没有安装的软件包时,使用包全名,而且注意路径 2.RPM安装 切换到光盘p ...