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 ...
随机推荐
- BurpSuite 设置Hostname Resolution
#写在前面 这种情况你可能遇到过: 对方用了CDN, 你查到了对方真实IP, 但还不能100%肯定. 这时候, 最好的测试就是 win/linux修改HOST文件 Win重启电脑 Linux重启网络 ...
- dm368 ipnc3.0环境搭建脚本
前言 为了方便其他人搭建dm368 ipnc3.0环境,我写了个脚本,执行脚本就可以自动搭建好环境了,绝对的傻瓜操作了,不过有一个地方让我很郁闷,那就是在用sed替换掉某段内容的时候(143行--15 ...
- memcached安装【转】
1.安装依赖软件 # yum -y install libevent libevent-devel perl-Test-Harness perl-Time-HiRes perl-TermReadKey ...
- 2017 SWERC
2017 SWERC A:Cakey McCakeFace 题目描述:有一个炉每次只能放一个蛋糕,炉的进口和出口各放了一个探测器,当放蛋糕进去时,进口的探测器会记录时刻,当蛋糕做好后,蛋糕从出口出来, ...
- URAL题解一
URAL题解一 URAL 1002 题目描述:一种记住手机号的方法就是将字母与数字对应,如图.这样就可以只记住一些单词,而不用记住数字.给出一个数字串和n个单词,用最少的单词数来代替数字串,输出对应的 ...
- HDU 6197 array array array 2017沈阳网络赛 LIS
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6197 题意:给你n个数,问让你从中删掉k个数后(k<=n),是否能使剩下的序列为非递减或者非递增 ...
- linux下rz,sz安装
1.sz rz yum安装 yum install lrzsz
- 在数据库中(Oracle),根据时间查询数据: to_date()和to_char()函数
1. to_date() 函数 1.1 格式 to_date("要转换的字符串","转换的格式") //两个参数的格式必须匹配,否则会报错.即按照第二个参数 ...
- 洛谷P2827 蚯蚓 题解
洛谷P2827 蚯蚓 题解 题目描述 本题中,我们将用符号 ⌊c⌋ 表示对 c 向下取整. 蛐蛐国最近蚯蚓成灾了!隔壁跳蚤国的跳蚤也拿蚯蚓们没办法,蛐蛐国王只好去请神刀手来帮他们消灭蚯蚓. 蛐蛐国里现 ...
- Base Class 慎用箭头函数
在项目中,child继承base的时候,需要重新修改base.fun的逻辑,但是有些情况下面并不是简单的覆盖,而是在base.fun的逻辑基础上进行加工处理. 刚开始接触es6的时候也许都遇到过,ch ...