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. DateUtils常用方法

    一.DateUtils常用方法   1.1.常用的日期判断 isSameDay(final Date date1, final Date date2):判断两个时间是否是同一天: isSameInst ...

  2. 如何搭建eclipse+maven环境

    Maven这个个项目管理和构建自动化工具,越来越多的开发人员使用它来管理项目中的jar包.本文仅对Eclipse中如何安装.配置和使用Maven进行了介绍.完全step by step. 如果觉得本文 ...

  3. 简明的sql优化

    网上关于SQL优化的教程很多,但是比较杂乱.近日有空整理了一下,写出来跟大家分享一下,其中有错误和不足的地方,还请大家纠正补充. 这篇文章我花费了大量的时间查找资料.修改.排版,希望大家阅读之后,感觉 ...

  4. nginx 配置中的if判断

    正则表达式匹配:     ==:等值比较;     ~:与指定正则表达式模式匹配时返回“真”,判断匹配与否时区分字符大小写:     ~*:与指定正则表达式模式匹配时返回“真”,判断匹配与否时不区分字 ...

  5. Java ActiveMQ 讲解(一)理解JMS 和 ActiveMQ基本使用

    最近的项目中用到了mq,之前自己一直在码农一样的照葫芦画瓢.最近几天研究了下,把自己所有看下来的文档和了解总结一下. 一. 认识JMS 1.概述 对于JMS,百度百科,是这样介绍的:JMS即Java消 ...

  6. Eclipse中Tomcat的配置及简单例子

    Eclipse中Tomcat的配置及简单例子 Eclipse中Tomcat的配置是很简单的一个工作 一. 工具下载 Eclipse,最新版的eclipse为Mars版本.下载地址为: http://w ...

  7. 使用sn.exe为程序集签名

    前言 在写上一篇随笔时,为理解EF事务底层的原理,我去Github上把EF的源码下载放到自己项目调试,不过在编译时遇到了下面这个报错信息.经过一番查阅,了解到了程序集签名(也称强名称签名)的概念.报错 ...

  8. “全栈2019”Java第一百零九章:匿名内部类实现唯一抽象类或接口

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  9. PHP中关于foreach的笔试题

    1,php与C++的不同之处是PHP中变量没有局部作用域,只有函数作用域和全局作用域.如下函数,在php中,$name的作用域是函数test():在C++中$name的作用域是for循环体,for循环 ...

  10. sqli-labs lession 5 之盲注型SQL入门

    本文作者:Mochazz 如果所查询的用户id在数据库中,可以发现页面显示”You are in”,而不像前4关那样会显示出具体的账号密码. 如果sql语句查询结果不存在,则不会显示”You are ...