原题目
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
题目大意
战争中,确保所有城市有高速公路连通极其重要。如果有城市被敌人占领,所有与它连通的公路都要被关闭。所以为了保证剩余的城市间的连通性,必须立刻得知是否需要维修另外的公路。给出标有剩余的公路的城市地图,需要尽快得到需要维修的公路的数目。
例如,我们有由2条高速公路连接的3个城市,连接状态为城市1-城市2,城市1-城市3。如果城市1被敌人攻占,则必须有一条高速公路需要维修,即城市2到城市3的公路。
输入的第一行为N,M和K三个数字,分别表示城市的总数目、剩余公路的数目和需要检查的城市的数目。接下来的M行每行用两个数字表示一条公路,分别表示公路连接的两个城市。城市由1编号到N。最后一行的K个数字表示我们关心的K个城市。对于K个城市中的每一个,均需在一行内输出当这个城市被攻占后需要维修的高速公路数目。
题解
被一月的力扣每日一题洗脑了,遇事不决直接并查集。对每一个需要检查的城市都建立一个并查集,重新遍历一遍原图并将路的两端合并,最后数出并查集中不同的根即可。
C语言代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define ROAD_COL_SIZE 2
int n, m, k;
int *fa;
int find(int *fa, int x){
if(fa[x] == x) return x;
else return fa[x] = find(fa, fa[x]);
}
void merge(int *fa, int x, int y){
fa[find(fa, x)] = find(fa, y);
}
int in(int *nums, int len, int check){
for(int i = 0;i < len;++i)
if(nums[i] == check)
return 1;
return 0;
}
int count_union_set(int *fa){
int *roots = malloc(sizeof(int) * (n + 1));
memset(roots, 0, sizeof(int) * (n + 1));
int cnt = 0;
for(int i = 1;i <= n;++i){
if(in(roots, cnt, find(fa, fa[i])))
continue;
else{
roots[cnt++] = find(fa, fa[i]);
}
}
return cnt;
}
int get_need_repaired_num(int *roads, int lost){
for(int i = 1;i <= n;++i) fa[i] = i;
for(int i = 0;i < m;++i){
if(roads[i*ROAD_COL_SIZE] == lost || roads[i*ROAD_COL_SIZE + 1] == lost)
continue;
merge(fa, roads[i*ROAD_COL_SIZE], roads[i*ROAD_COL_SIZE + 1]);
}
return count_union_set(fa) - 2;
}
int main(){
scanf("%d %d %d", &n, &m, &k);
int *roads = malloc(sizeof(int) * m * ROAD_COL_SIZE);
fa = malloc(sizeof(int) * (n + 1));
int need_check;
for(int i = 0;i < m;++i){
scanf("%d %d", roads + i*ROAD_COL_SIZE, roads + i*ROAD_COL_SIZE + 1);
}
for(int i = 0;i < k;++i){
scanf("%d", &need_check);
printf("%d\n", get_need_repaired_num(roads, need_check));
}
return 0;
}