问题
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.
Sample Input:
3 2 3
1 2
1 3
1 2 3
Sample Output:
1
0
0
解决方法
题目大意
给出n个城市之间有相互连接的m条道路,当删除一个城市和其连接的道路的时候,问其他几个剩余的城市至少要添加多少个路线才能让它们重新变为连通图。
实际上这个题是求连通分支的数量。
dfs
#include<iostream>
#include<string.h>
using namespace std;
const int maxn = 1010;
bool visit[maxn];
int graph[maxn][maxn];
void dfs(int node)
{
visit[node] = true;
for (int i = 0; i < maxn; i++)
{
if (graph[node][i] == 1 && visit[i] == false) dfs(i);
}
}
int main()
{
int n, m, k, point;
scanf("%d %d %d", &n, &m, &k);
for (int i = 0; i < m; i++)
{
int a, b;
scanf("%d %d", &a, &b);
graph[a][b] = graph[b][a] = 1;
}
for (int i = 0; i < k; i++)
{
int block = 0;
scanf("%d", &point);
fill(visit, visit + maxn, false);
visit[point] = true;
for (int j = 1; j <=n ; j++)
{
if (visit[j] == false)
{
dfs(j);
block++;
}
}
printf("%d\n", block - 1);
}
return 0;
}
并查集
#include<iostream>
#include<vector>
using namespace std;
const int maxn = 1010;
vector<int>graph[maxn];
int visit[maxn];
int father[maxn];
void init()
{
fill(visit, visit + maxn, false);
for (int i = 0; i < maxn; i++) father[i] = i;
}
int findfather(int root)
{
int son = root;
while (root != father[root]) root = father[root];
while (son != father[son])
{
int tmp = son;
son = father[son];
father[tmp] = root;
}
return root;
}
void Union(int a, int b)
{
int faa = findfather(a);
int fab = findfather(b);
if (faa != fab) father[faa] = fab;
}
int main()
{
int n, m, k, point;
scanf("%d %d %d", &n, &m, &k);
for (int i = 0; i < m; i++)
{
int a, b;
scanf("%d %d", &a, &b);
graph[a].push_back(b);
graph[b].push_back(a);
}
for (int query = 0; query < k; query++)
{
scanf("%d", &point);
init();
for (int i = 1; i <= n; i++)
{
for (int j = 0; j < graph[i].size(); j++)
{
int u = i, v = graph[i][j];
if(u!=point&&v!=point) Union(u, v);
}
}
int block = 0;
for (int i = 1; i <= n; i++)
{
if (i != point)
{
int fai = findfather(i);
if (visit[fai] == false)
{
visit[fai] = true;
block++;
}
}
}
printf("%d\n", block - 1);
}
return 0;
}
后记
这道题主要掌握图的dfs和并查集。并查集的详解,主要参考:并查集。不得不说,非常精彩!
除此之外,并查集的模板,必须要记住!