题目描述
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 (<1000), M and 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.
题目大意
在战争中保证所有的城市都能通过高速公路连接起来是至关重要的。如果一个城市被敌军攻占了,那么所有与这个城市相连的高速公路都将被关闭。为了保持剩余的城市的连通性,我们必须立即知道是否需要修建其它的高速公路。给你一张城市的地图,上面有所有能正常使用的高速公路,你需要找出需要修建的高速公路的数量。
比如我们现在有3个城市和两条高速公路连接city1-city2
和city1-city3
。如果city1
被敌军攻占了,我们必须修建一条city2-city3
之间的高速公路。
输入说明
每个输入文件包含一个测试用例,每个测试用例的第一行有三个正整数N
(<1000)、M
,k
,它们分别是城市的数量,剩余的高速公路的数量,需要检查的城市的数量。接下来的M
行,每一行用两个整数描述一条高速公路,这是该高速公路所能连接的城市的数量。城市从1到N进行编号。最后一行有K个整数,它们代表我们关心的城市。
输出说明
对于K个城市中的每一个,假设这个城市沦陷了,在一行中输出需要修建的高速公路的数量。
题目分析
这道题考察的是图的遍历,需要我们遍历整张图来求连通分量。具体实现起来可以有三种写法:深度优先遍历、广度优先遍历、并查集。需要注意的是,测试用例中的边数可能非常多,如果用cin
进行输入会超时,因此需要加上iso::sync_with_stdio)(false)
以提高输入的效率。
深度优先遍历
用邻接表来存储整张图的结构,当然因为节点数目不超过1000,所以用bool
型的邻接矩阵来存储也是可以的。因为是深度优先遍历,所以需要记录每个点是否被访问过,因此需要定义一个数组bool vis[1005]
来记录每个节点的访问情况。关于DFS的写法,没有太多需要介绍的,直接套用模板即可。这个题的难点在于如何处理点的删除,也就是说某个节点沦陷之后,与这个节点相连的所有边都不存在了。显然不能真的把一个点给删掉,因为可能会有多次检查,后面恢复起来会很麻烦。我这里采取的策略是在遍历之前就将沦陷了的点标记为已访问,这样和删除了这个点的效果是相同的,因为在后续的遍历过程中,肯定不会再访问到这个点,看起来好像是这个点根本不在图中。
每进行一次DFS,就会遍历一个连通分支,而最后需要修建的高速的数目就是连通分支数减一,这是图论中比较简单的概念。在编码时需要注意,每次遍历整张图之前,需要先将vis
数组初始化,以防上一次的遍历过程对现在的遍历造成影响。
DFS源代码
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
vector<int> adj[1005];
bool vis[1005] = {};
void DFS(int s) {
vis[s] = true;
for (auto i : adj[s]) {
if (vis[i] == false) {
DFS(i);
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin >> n >> m >> k;
int city1, city2;
int check;
for (int i = 0; i < m; i++) {
cin >> city1 >> city2;
adj[city1].push_back(city2);
adj[city2].push_back(city1);
}
for (int i = 1; i <= k; i++) {
cin >> check;
int ans = 0;
memset(vis,0, 1005);
vis[check] = true; //将删除的点置为已访问,这样在遍历时就不会再访问到这个点了
for (int j = 1; j <= n; j++) {
if (vis[j] == false) {
DFS(j);
ans++;
}
}
cout << ans -1 << endl;
}
}
广度优先遍历
广度优先遍历需要记录每个节点的入队情况,以防某个节点多次进入队列,因此定义bool inq[1005]
来做记录。和深度优先遍历相似,在遍历整张图之前,先将沦陷了的节点设置为已入队,这样在后续遍历过程中就不会再将这个节点放入队列中,也就无法访问到这个节点了。
BFS源代码
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
vector<int> adj[1005];
bool inq[1005] = {};
void BFS(int s) {
queue<int> q;
q.push(s);
inq[s] = true;
while (!q.empty()) {
auto top = q.front();
q.pop();
for (auto i : adj[top]) {
if (inq[i] == false) {
q.push(i);
inq[i] = true;
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin >> n >> m >> k;
int city1, city2;
int check;
for (int i = 0; i < m; i++) {
cin >> city1 >> city2;
adj[city1].push_back(city2);
adj[city2].push_back(city1);
}
for (int i = 1; i <= k; i++) {
cin >> check;
int ans = -1;
memset(inq, 0, 1005);
inq[check] = true; //将删除的点置为已访问,这样在遍历时就不会再访问到这个点了
for (int j = 1; j <= n; j++) {
if (inq[j] == false) {
BFS(j);
ans++;
}
}
cout << ans << endl;
}
}
并查集
采用并查集的写法时,不是遍历点,而是遍历边。可以定义一个结构体用来存储一条边的两个端点,也可以直接用邻接表进行存储。当用邻接表存储时,需要注意,只需要让一个端点是另一个端点的邻接点即可,没必要再反过来,否则相当于将一条边存储了两次。
对于每一条边,如果它的两个顶点不含沦陷了的顶点,那么将它们进行合并。遍历完所有的边之后,统计集合的数目, 注意一下,最终的统计结果是包含沦陷了的点的,也就是说把沦陷了的点当做一个集合统计进去了,因此最终需要修建的高速公路数是集合数目减2。
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
vector<int> adj[1005];
int father[1005];
int FindFather(int a) {
int temp = a;
while (a != father[a])
a = father[a];
int temp2;
while (temp != a) {
temp2 = father[temp];
father[temp] = a;
temp = temp2;
}
return a;
}
void Union(int a, int b) {
int fa = FindFather(a);
int fb = FindFather(b);
if (fa != fb)
father[fa] = fb;
}
int main()
{
ios::sync_with_stdio(false);
cin >> n >> m >> k;
int city1, city2;
int check;
vector<int> ans;
for (int i = 0; i < m; i++) {
cin >> city1 >> city2;
adj[city1].push_back(city2);
}
for (int i = 1; i <= k; i++) {
cin >> check;
for (int j = 1; j <= n; j++)
father[j] = j;
for (int i = 1; i <= n; i++) {
if (i != check) {
for (auto j : adj[i]) {
if (j != check)
Union(i, j);
}
}
}
int root[1005] = {};
for (int j = 1; j <= n; j++) {
root[FindFather(j)] = true;
}
int ans = 0;
for (int j = 1; j <= n; j++)
ans += root[j];
cout << ans - 2 << endl;
}
}