0
点赞
收藏
分享

微信扫一扫

PAT 1013 Battle Over Cities (25 分) (DFS or 并查集)

倚然君 2023-03-02 阅读 60


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条边,k个关心的城市

最后一行分别输出k个点,再除掉与该点相连的路径后,如果要保证除攻占之外的城市连通,需要最少修几条路。

其实就是求联通块的个数,最后的结果 -1 即可了   可以用 dfs 或者 并查集。

很简单,关键是理解题意。

// dfs

#include <bits/stdc++.h>
#define Max 1111
using namespace std;
int mapp[Max][Max];
int vis[Max];
int n, m, k, u, v;
void dfs (int u) {
vis[u] = 1;
for (int i = 1; i <= n; i++)
{
if(!vis[i] && mapp[u][i])
{
dfs(i);
}
}
}
int main() {

ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
while (cin >> n >> m >> k) {
memset(mapp, 0, sizeof(mapp));
for (int i = 0; i < m; i++)
{
cin >> u >> v;
mapp[u][v] = mapp[v][u] = 1;
}
int cnt = 0;
for (int i = 0; i < k; i++) {
cnt = 0;
memset(vis, 0,sizeof(vis));
cin >> u;
vis[u] = 1;
for (int j = 1;j <= n;j++) {
if (!vis[j]) {
dfs(j);
cnt++; //连通块的个数
}
}
cout << cnt - 1 <<endl;
}

}
return 0;
}

// 并查集  

为什么在 init() 初始化时 把 范围 改成 Max 就会出错呢 ????

#include <bits/stdc++.h>
#define Max 1111
using namespace std;
//并查集
int fa[Max];
vector <int> vec[Max];
int n, m, k, u, v, x;
void init() {
for (int i = 0; i<= n ; i++){
fa[i] = i;
}
}
void init1() {
for (int i = 1; i <= n; i++){
vec[i].clear();
}
}
int Find (int x) {
if (fa[x] == x) return x;
return fa[x] = Find(fa[x]);
}
int mix(int x, int y) {
int fx = Find(x);
int fy = Find(y);
if (fx != fy) {
fa[fx] = fy;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
while (cin >> n >> m >> k) {
init1();
for (int i = 0; i < m; i++){
cin >> u >> v;
vec[u].push_back(v);
vec[v].push_back(u);
}

for (int ii = 0; ii < k; ii++){
cin >> x;
init ();
for (int i = 1; i <= n; i++){
for (int j = 0; j < vec[i].size(); j++){
int u = i, v = vec[i][j];
if(x == u || x == v ) {
continue;
}
mix(u, v);
// cout << "--> " << u << " " << v <<endl;
}
}
int cnt = 0;
for (int i = 1; i <= n; i++){
if (i == x) continue;
if (fa[i] == i) cnt++;
}
cout << cnt - 1 <<endl;
}return 0;
}
}

 

举报

相关推荐

0 条评论