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. Spring学习(六)——集成memcached客户端

    memcached是高性能的分布式内存缓存服务器.许多Web应用都将数据保存到RDBMS中,应用服务器从中读取数据并在浏览器中显示. 但随着数据量的增大.访问的集中,就会出现RDBMS的负担加重.数据 ...

  2. 安装、启动consul

    1.下载 从consul官网https://www.consul.io/downloads.html下载 2.解压.配置 将下载的  consul_1.4.4_linux_amd64.zip 解压 t ...

  3. CSS3 线性渐变linear-gradient

    CSS3 Gradient 分为 linear-gradient(线性渐变)和 radial-gradient(径 向渐变).为了更好的应用 CSS3 Gradient,需要先了解一下目前的几种现代浏 ...

  4. Java--下载历史版本登录账户

    目前在官网下载低于jdk1.8的java jdk的时候需要登陆,这边分享一个账号,方便下载 2696671285@qq.com 密码:Oracle123 转自-- jdk下载以前版本需要的账号(转) ...

  5. struts2 Convention插件好处及使用

    现在JAVA开发都流行SSH.而很大部分公司也使用了struts2进行开发..因为struts2提供了很多插件和标签方便使用..在之前开发过程中总发现使用了struts2会出现很多相应的配合文件.如果 ...

  6. 大公司怎么做Android代码混淆的?

    3月17日,网易资深安全工程师钟亚平在安卓巴士全球开发者论坛上做了<安卓APP逆向与保护>的演讲.其中就谈到了关于代码混淆的问题.现摘取部分重点介绍如下:   Java代码是非常容易反编译 ...

  7. RxJava / RxAndroid

    RxJava 是什么 RxJava 是函数响应式编程框架,它用观察者设计模式. 常用来做异步数据处理,在安卓中用来代替传统的 AsyncTask + Handler 的组合结构. RxJava 架构简 ...

  8. mongodb 备份还原

    一.简介 说起来数据库的“备份-还原”,在RDBMS系统中,都有很好的支持,也有很多选项可以设置,功能强大,也能自动完成大部分的备份功能,只要当初设置好了就可以了.对于MongoDB文档型的数据库来说 ...

  9. LOJ#162. 快速幂 2(分块)

    题面 传送门 题解 orzljz 我们分块,设\(s=\sqrt{p}+1\),那么\(x^a\)可以拆成\((x^s)^{a/s}\)和\(x^{a\bmod s}\),\(O(s)\)预处理,\( ...

  10. [HTML] <meta name="viewport" content="width=device-width,initial-scale=1.0">释义

    <meta name="viewport" content="width=device-width,initial-scale=1.0">这是 HT ...