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. eayui js动态加载Datagrid,自适应宽度,高度

    HTML: <div class="easyui-layout" style="min-height:100%;min-width:100%;"> ...

  2. SQL Server 数据库的分类和用户数据库文件组成

      数据库的分类       数据库分为两大类,一类是系统数据库:另一类是用户数据库,系统数据库我们一般使用的时候较少, 下面我们看看系统数据库包含哪些并分别有什么作用,如下图所示 用户数据库文件组成 ...

  3. leecode刷题(21)-- 删除链表的倒数第N个节点

    leecode刷题(21)-- 删除链表的倒数第N个节点 删除链表的倒数第N个节点 描述: 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2- ...

  4. 【文文殿下】【HAOI2008】硬币购物

    题目描述 硬币购物一共有4种硬币.面值分别为c1,c2,c3,c4.某人去商店买东西,去了tot次.每次带di枚ci硬币,买si的价值的东西.请问每次有多少种付款方法. 数据规模 di,s<=1 ...

  5. How to write date range query in Nest ElasticSearch client?

    Looking at the source code, there are two overloads of the OnField method. When I use the the that t ...

  6. 基于Dubbo的Hessian协议实现远程调用

    Dubbo基于Hessian实现了自己Hessian协议,可以直接通过配置的Dubbo内置的其他协议,在服务消费方进行远程调用,也就是说,服务调用方需要使用Java语言来基于Dubbo调用提供方服务, ...

  7. 爬虫实战3:使用request,bs4爬动态加载图片

    参考网站:https://blog.csdn.net/Young_Child/article/details/78571422 在爬的过程中遇到的问题: 1.被ban:更改header的User-Ag ...

  8. delphi 10.2 ----简单的叠乘例子

    unit Unit11; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, Syste ...

  9. 使用hexo+coding搭建免费个人博客

    1.检测node和npm 先检测一下有没有node.js和npm $ node -v //如果有,说明node.js安装成功! $ node -v v8.4.0 //如果有,说明npm安装成功! $n ...

  10. 1.CentOS6.5下的基础DNS配置

    常规DNS的安全和配置1.安装DNSyum -y install bind bind-utils安装后生成的文件,我们主要配置下面几个/etc/named.conf/var/named/xx这个xx是 ...