Battle Over Cities

  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 (<), Mand 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 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

解题思路:

  城市之间本来有一些道路,现在有某个城市被攻陷了,与该城市连通的道路就都不能走了,如果剩下还在我们手里的城市不连通,就要修筑新道路使他们连通,任意两个城市都可以修筑新道路。
  本题每组数据首先给出城市数量n,道路数量m,查询数量k,每次查询只有一个城市被攻陷,之后给出m行数据为每条道路连接的两个城市,随后一行有k个数,表示当前次查询中被攻陷的城市,要求输出最少需要新修筑多少条道路才可以使我们手里剩下的城市都连通。

  这里使用并查集解答本题。(本题需要路径压缩,否则会T)

int getFather(int x){
if(x != father[x])
father[x] = getFather(father[x]);
return father[x];
}

  首先使用邻接表记录所有道路信息,每次查询遍历所有道路,通过并查集合并所有不与被攻陷城市连接的道路两端的城市。

        for(int i = ; i <= n; i++){    //遍历所有道路
for(auto it : G[i]){
if(i != closed && it != closed){ //道路不能与被攻陷的城市相连
int fu = getFather(i);
int fv = getFather(it);
if(fu != fv) //如果两个城市不在同一个连通块中,合并这两个连通块
father[fu] = fv;
}
}
}

  之后对于还在我们手里的城市,获得合并后的连通块数量ans,ans - 1即可获得最少需要新修筑的道路数量。

            int ans = ;
for(int i = ; i <= n; i++){ //获得连通块数量
if(getFather(i) == i && i != closed){ //只要某个城市所在连通块父节点为自己且这个城市不是被攻陷的城市
ans++; //连通块加一
}
}

AC代码

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3+;
int father[maxn]; //father[i]记录i所在连通块的父节点
vector<int> G[maxn]; //临界表记录边信息
int n, m, k;
int getFather(int x){ //并查集路径压缩找爹函数
if(x != father[x])
father[x] = getFather(father[x]);
return father[x];
}
int main(){
while(scanf("%d%d%d", &n, &m, &k) != EOF){ //输入城市数量 道路数量 查询数量
while(m--){ //输入道路信息
int u, v;
scanf("%d%d", &u, &v);
//G[i]记录与i城市连通的道路
G[u].push_back(v);
G[v].push_back(u);
//道路是双向的,所以道路两端的城市都要记录这条道路
}
int closed; //closed记录当前被攻陷的城市
while(k--){ //k条查询
for(int i = ; i <= n; i++) //初始化每个城市自己单独在一个连通块
father[i] = i;
scanf("%d", &closed); //输入被攻陷的城市
for(int i = ; i <= n; i++){ //遍历所有道路
for(auto it : G[i]){
if(i != closed && it != closed){ //道路不能与被攻陷的城市相连
int fu = getFather(i);
int fv = getFather(it);
if(fu != fv) //如果两个城市不在同一个连通块中,合并这两个连通块
father[fu] = fv;
}
}
}
int ans = ;
for(int i = ; i <= n; i++){ //获得连通块数量
if(getFather(i) == i && i != closed){ //只要某个城市所在连通块父节点为自己且这个城市不是被攻陷的城市
ans++; //连通块加一
}
}
printf("%d\n", ans - ); //输出答案
}
}
return ;
}

PTA (Advanced Level) 1013 Battle Over Cities的更多相关文章

  1. PAT (Advanced Level) 1013. Battle Over Cities (25)

    并查集判断连通性. #include<iostream> #include<cstring> #include<cmath> #include<algorit ...

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

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

  3. PAT 1013 Battle Over Cities

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

  4. PAT甲级1013. Battle Over Cities

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

  5. PAT 1013 Battle Over Cities(并查集)

    1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...

  6. pat 1013 Battle Over Cities(25 分) (并查集)

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

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

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

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

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

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

随机推荐

  1. 不写代码也能爬虫Web Scraper

    https://www.jianshu.com/p/d0a730464e0c web scraper中文网 http://www.iwebscraper.com/category/%E6%95%99% ...

  2. .Net Core下使用Ajax,并传送参数到controllers

    可以使用URL拼接方式方法传参 .cshtml部分 @section Scripts{ @{ await Html.RenderPartialAsync("_ValidationScript ...

  3. 如何在linux设置回收站 - 防止失误操作造成数据清空

    linux rm命令是即刻删除的,而且挺多人喜欢加上-f强制命令,更暴力的是删除文件夹直接 rm -rf ,这样子代表你执行完后,就完全被干掉了. 还是推荐在linux下设置回收站,写一个shell脚 ...

  4. linux进程管理(一)

    进程介绍 程序和进程 程序是为了完成某种任务而设计的软件,比如OpenOffice是程序.什么是进程呢?进程就是运行中的程序. 一个运行着的程序,可能有多个进程. 比如自学it网所用的WWW服务器是a ...

  5. C# Winform 小技巧(Datagridview某一列按状态显示不同图片)

    步骤: 一.导入状态图片到项目中: 二.在窗体中声明一个图片数组,并在窗体的OnLoad事件中加入图片资源: /// <summary> /// 存储状态图片序列,避免同一状态对图片重复读 ...

  6. luoguP4647 [IOI2007] sails 船帆

    https://www.luogu.org/problemnew/show/P4647 首先发现答案与顺序无关,令 $ x_i $ 表示高度为 $ i $ 的那一行帆的个数,第 $ i $ 行对答案的 ...

  7. Logstash配置总结和实例

    这里记录Logstash配置中注意的事项: 整个配置文件分为三部分:input,filter,output.参考这里的介绍 https://www.elastic.co/guide/en/logsta ...

  8. [ActionScript3.0] 使用FileReferenceList处理多个文件上载

    package { import flash.display.Sprite; import flash.events.DataEvent; import flash.events.Event; imp ...

  9. [Objective-C语言教程]命令行参数(23)

    执行时,可以将一些值从命令行传递给Objective-C程序. 这些值称为命令行参数,很多时候它们对程序很重要,特别是当想要从外部控制程序而不是在代码中对这些值进行硬编码时就很有用了. 命令行参数使用 ...

  10. 「工具」三分钟了解一款在线流程绘制工具:Whimsical

    Whimsical 是一款在线流程绘制工具,只需要一个浏览器就随时随地绘制精美的流程图.除了流程图(Flowcharts)功能,官方还推出了线框图(Wireframes).便利贴(Sticky Not ...