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的更多相关文章

  1. 图论 - PAT甲级 1013 Battle Over Cities C++

    PAT甲级 1013 Battle Over Cities C++ It is vitally important to have all the cities connected by highwa ...

  2. 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 ...

  3. PAT A 1013. Battle Over Cities (25)【并查集】

    https://www.patest.cn/contests/pat-a-practise/1013 思路:并查集合并 #include<set> #include<map> ...

  4. 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 ...

  5. 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 ...

  6. PAT 解题报告 1013. Battle Over Cities (25)

    1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...

  7. PAT 1013 Battle Over Cities

    1013 Battle Over Cities (25 分)   It is vitally important to have all the cities connected by highway ...

  8. PAT 1013 Battle Over Cities(并查集)

    1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...

  9. pat 1013 Battle Over Cities(25 分) (并查集)

    1013 Battle Over Cities(25 分) It is vitally important to have all the cities connected by highways i ...

随机推荐

  1. python之自然语言处理入门(一)

    前言 NTLK是著名的Python自然语言处理工具包,记录一下学习NTLK的总结. 安装nltk pip install nltk # 测试 import nltk 安装相关的包 import nlt ...

  2. Shell-history命令加记录用户IP

    记录输入的命令 history命令可以查看用户输入过的命令,一个典型history命令输出如下: 980 2017-05-29 20:17:37 cd - 981 2017-05-29 20:17:4 ...

  3. HDU 6198 2017沈阳网络赛 线形递推

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6198 题意:给出一个数k,问用k个斐波那契数相加,得不到的数最小是几. 解法:先暴力打表看看有没有规律 ...

  4. 「pycaffe指南」使用caffe的NetSpec.py中的Python接口自动生成×.prototxt文件

    https://www.jianshu.com/p/1a420445deea 作者:MapReducer 链接:https://www.jianshu.com/p/1a420445deea 來源:简书 ...

  5. linux系统查找具体进程

    ps -ef | grep '查找内容' eg:ps -ef | grep '测试USB设备穿透'

  6. java中常见异常汇总(根据自己遇到的异常不定时更新)

    1.java.lang.ArrayIndexOutOfBoundsException:N(数组索引越界异常.如果访问数组元素时指定的索引值小于0,或者大于等于数组的长度,编译程序不会出现任何错误,但运 ...

  7. C# 随笔 【ToList().Foreach()和Foreach()】

    1. 最近在做一个Socket通讯的例子,但是如果使用UTF-8编码传输中文的话取和的会不一样.早上做了测试 . string str = "a我..";看代码中间是一个英文,一个 ...

  8. /proc文件夹介绍

    Linux系统上的/proc目录是一种文件系统,即proc文件系统.与其它常见的文件系统不同的是,/proc是一种伪文件系统(也即虚拟文件系统),存储的是当前内核运行状态的一系列特殊文件,用户可以通过 ...

  9. 【前端笔记】浅谈js继承

    我们先想想我们用js最后要怎样实现面向对象的编程.事实上我们必须用上原型链这种东西. 我们的父类superType有属性和方法,并且一些能被子类subType继承,一些能被覆盖,但是丝毫不会影响到父类 ...

  10. CAS单点登陆的两个原理图

    最近学习CAS单点登录,所以在网上找了两张比较清晰的原理图以供参考: [CAS浏览器请求认证序列图] 其中:*  ST:Service Ticket,用于客户端应用持有,每个ST对应一个用户在一个客户 ...