PTA (Advanced Level) 1013 Battle Over Cities
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 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 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的更多相关文章
- PAT (Advanced Level) 1013. Battle Over Cities (25)
并查集判断连通性. #include<iostream> #include<cstring> #include<cmath> #include<algorit ...
- PAT 解题报告 1013. Battle Over Cities (25)
1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...
- PAT 1013 Battle Over Cities
1013 Battle Over Cities (25 分) It is vitally important to have all the cities connected by highway ...
- PAT甲级1013. Battle Over Cities
PAT甲级1013. Battle Over Cities 题意: 将所有城市连接起来的公路在战争中是非常重要的.如果一个城市被敌人占领,所有从这个城市的高速公路都是关闭的.我们必须立即知道,如果我们 ...
- PAT 1013 Battle Over Cities(并查集)
1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...
- pat 1013 Battle Over Cities(25 分) (并查集)
1013 Battle Over Cities(25 分) It is vitally important to have all the cities connected by highways i ...
- 图论 - PAT甲级 1013 Battle Over Cities C++
PAT甲级 1013 Battle Over Cities C++ It is vitally important to have all the cities connected by highwa ...
- 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 ...
- 1013 Battle Over Cities (25分) DFS | 并查集
1013 Battle Over Cities (25分) It is vitally important to have all the cities connected by highways ...
随机推荐
- 创建TFS团队项目时自动建立代码库的文件夹结构
很多客户都跟我提过一个这样的需求,即需要在创建团队TFS项目时,自动创建起源代码库的文档结构,例如类似下列结构的文件夹: <teamProject> |- DEVELOPMENT ...
- 用canvas画布画一个画板
前段时间,在对H5的回顾中突然对canvas有了感觉,闲来无事便对其进行了一些捯饬.这不,上周我还做了一个好玩的画板呢,废话不多说,直接上代码(PS:翠花,上代码~): HTML部分: <!DO ...
- c# 图片等比缩略
public Bitmap getnew(Image bit, int TargetWidth, int TargetHeight)//beishu参数为放大的倍数.放大缩小都可以,0.8即为缩小至原 ...
- Java50道经典习题-程序7 处理字符串
题目:输入一行字符,分别统计出其中英文字母.空格.数字和其它字符的个数.分析:利用while语句,条件为输入的字符不为'\n'. import java.util.*; public class Pr ...
- DOS目录相关命令
MD----创建自目录命令 格式:MD[盘符:][路径名]<子目录名> 1)在C盘的根目录下创建名为FOX的子目录 C:\>MD FOX 2)在FOX子目录下创建USER子目录 ...
- 关于Mysql数据库进行多表查询时设计编程思想
SQL代码:
- jzoj3511
设f[i][j][k] 表示第i行状态为j i+1行将要被放为状态k的最优解 每次枚举这行和上一行的状态来dfs,注意细节 不合法的状态会直接被赋值成为inf
- 【表单验证】基于jQuery的高度灵活的表单验证(无UI)
表单验证是前端开发过程中常见的一个需求,产品需求.业务逻辑的不同,表单验证的方式方法也有所区别.而最重要的是我们要清楚,表单验证的核心原则是--错误信息提示准确,并且尽可能少的打扰/干扰用户的输入和体 ...
- Android-获取手机已经安装的程序
有时候我们会查询手机里面是否安装了某个程序,或者获取已经安装软件名称的集合. android这边提供了相应的接口. [java] view plaincopy final PackageManager ...
- C#-WebForm-Request、Response、QueryString
知识点: Request - 获取请求对象 专门用来接传递过来的值 Request["key"](李献策lxc) 1.获取地址栏传递过来的值 get 2.获取表单传递过来的参数值 ...