PAT甲级1013. Battle Over Cities
PAT甲级1013. Battle Over Cities
题意:
将所有城市连接起来的公路在战争中是非常重要的。如果一个城市被敌人占领,所有从这个城市的高速公路都是关闭的。我们必须立即知道,如果我们需要修理任何其他高速公路,以保持其他城市的连接。鉴于所有其余高速公路标记的城市地图,
你应该告诉高速公路需要修理的次数很快。
例如,如果我们有3个城市和2个连接city1-city2和city1-city3的高速公路3。那么如果city1被敌人占领,那么我们必须有1条公路修好,那就是高速公路city2-city3。
输入
每个输入文件包含一个测试用例。
每个案例分别以3号数字N(<1000),M和K分别开始,分别是城市总数,剩余高速公路数和待检查城市数。然后M行跟随,每个描述一条公路由2个整数,这是高速公路连接的城市的数量。
城市的编号从1到N.最后有一行包含K个数字,代表我们关心的城市。
输出
对于每个K个城市,如果该城市丢失,一条线上的公路数量需要修复。
思路:
求连通支路。并查集或者dfs都可以。
ac代码:
C++ 并查集
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<unordered_map>
#include<cstring>
using namespace std;
const int maxn = 1005;
int mapx[maxn * maxn][2];
int pre[maxn];
int Find(int x)
{
return pre[x] == x ? x : pre[x] = Find(pre[x]);
}
void Union(int x, int y)
{
x = Find(x);
y = Find(y);
if (x == y) return;
pre[y] = x;
}
int main()
{
int n, m, k, sum;
cin >> n >> m >> k;
memset(mapx, 0, sizeof(mapx));
for (int i = 0; i < m; i++)
{
cin >> mapx[i][0] >> mapx[i][1];
}
int city;
while (k--)
{
cin >> city;
for (int i = 0; i <= n; i++)
{
pre[i] = i;
}
for (int i = 0; i < m; i++)
{
if (mapx[i][0] != city && mapx[i][1] != city)
Union(mapx[i][0], mapx[i][1]);
}
sum = 0;
for (int i = 1; i <= n; i++)
{
if (pre[i] == i && i != city)
sum++;
}
cout << sum - 1 << endl;
}
return 0;
}
C++ dfs
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<unordered_map>
#include<cstring>
using namespace std;
const int maxn = 1005;
int visit[maxn];
int link[maxn][maxn];
int n, m, k;
void dfs(int x)
{
visit[x] = 1;
for (int i = 1; i <= n; i++)
{
if (visit[i] != 1 && link[i][x] == 1)
dfs(i);
}
}
int main()
{
cin >> n >> m >> k;
int city1, city2;
memset(link, 0, sizeof(link));
for (int i = 0; i < m; i++)
{
cin >> city1 >> city2;
link[city1][city2] = 1;
link[city2][city1] = 1;
}
int misscity;
int count;
for (int i = 0; i < k; i++)
{
cin >> misscity;
count = 0;
memset(visit, 0, sizeof(visit));
visit[misscity] = 1;
for (int i = 1; i <= n; i++)
{
if (visit[i] != 1)
{
dfs(i);
count++;
}
}
cout << count - 1 << endl;
}
return 0;
}
PAT甲级1013. Battle Over Cities的更多相关文章
- 图论 - 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 ...
- PAT A 1013. Battle Over Cities (25)【并查集】
https://www.patest.cn/contests/pat-a-practise/1013 思路:并查集合并 #include<set> #include<map> ...
- PAT甲级——A1013 Battle Over Cities
It is vitally important to have all the cities connected by highways in a war. If a city is occupied ...
- PAT Advanced 1013 Battle Over Cities (25) [图的遍历,统计连通分量的个数,DFS,BFS,并查集]
题目 It is vitally important to have all the cities connected by highways in a war. If a city is occup ...
- 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(并查集)
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 ...
随机推荐
- 使用maven打包项目遇到错误: http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
今天在使用maven打包项目时遇到一个错误: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin ...
- Thinkphp的SQL查询方式
一.普通查询方式 a.字符串$arr=$m->where("sex=0 and username='gege'")->find();b.数组$data['sex']=0 ...
- 用sar进行CPU利用率的分析
07:40:17 PM CPU %user %nice %system %iowait %steal %idle07:40:19 PM a ...
- Linux删除除了今天以外的文件
[背景] 开发到日志记录功能时,每天都会产生当天的一个日志,久而久之就会产生累积,想要查看的时候,tab键无法自动补全,还要自己额外输入. 比较麻烦. [命令] 经过查找和实验,找到了以下的方法: 1 ...
- springboot + swagger2 生成api文档
直接贴代码: <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-sw ...
- JVM内存分配及GC简述
在阐述JVM的内存区域之前,先来看下计算机的存储单位.从小到大依次为Bit,Byte,KB,MB,GB,TB.相邻的单位相差2的10次方. 计算机运行中的存储元件主要分为寄存器(位于CPU)和内存,寄 ...
- ASP .NET CORE MVC 部署Windows 系统上 IIS具体步骤---.Net Core 部署到 IIS位系统中的步骤
一.IIS 配置 启用 Web 服务器 (IIS) 角色并建立角色服务. 1.Windows Ddesktop 桌面操作系统(win7及更高版本) 导航到“控制面板” > “程序” > “ ...
- java并发编程实战笔记---(第三章)对象的共享
3.1 可见性 synchronized 不仅实现了原子性操作或者确定了临界区,而且确保内存可见性. *****必须在同步中才能保证:当一个线程修改了对象状态之后,另一个线程可以看到发生的状态变化. ...
- MySQL-开发规范升级版
一.基础规范 表存储引擎必须使用InnoDB 表字符集默认使用utf8,必要时候使用utf8mb4 解读: (1)通用,无乱码风险,汉字3字节,英文1字节 (2)utf8mb4是utf8的超集,有 ...
- CMS(Concurrent Mark-Sweep)垃圾回收器
http://www.iteye.com/topic/1119491 1.总体介绍: CMS(Concurrent Mark-Sweep)是以牺牲吞吐量为代价来获得最短回收停顿时间的垃圾回收器.对于要 ...