PAT-1013——Battle Over Cities

原题链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805500414115840

题目描述:
在这里插入图片描述

题目翻译:

1013 城市之间的战争

在战争中,所有的城市都通过高速公路连接在一起,这一点是至关重要的。如果一个城市被敌人占领了,那么所有连接这个城市的高速公路都会被封闭。我们必须马上知道为了使得余下的城市保持连接状态,我们是否需要修建其他的高速公路。给你一张城市地图,上面标识出了所有余下的高速公路,你需要快速说出需要修建的高速公路的数量。

举个例子,如果我们有3座城市,2条高速公路分别连接city1-city2、city1-city3。如果city1被敌人占领了,我们就需要修建一条高速公路,那就是city2-city3。

输入格式:

每个输入文件包含一个测试用例。对每个测试用例,第一行包含3个数字:N(<= 1000)表示城市总数量,M表示高速公路数量,K表示需要检查的城市数量。接下来的M行,每行用2个整数描述一条高速公路,这2个整数分别代表这条高速公路所连接的两个城市的编号。城市编号从1到N。最后一行有K个数字,代表了我们关注的城市。

输出格式:

对K个城市中的每一个城市,分别在1行中输出如果该城市被敌人占领所需要修建的高速公路的数量。

输入样例:

3 2 3
1 2
1 3
1 2 3

输出样例:

1
0
0

知识点:图的深度优先遍历、并查集

思路一

DFS

#include<bits/stdc++.h>

using namespace std;
const int N=1e3+10;
bool mp[N][N];
int n,m,k;
bool vis[N];
void dfs(int v)
{
    vis[v]=true;
    for(int i=1;i<=n;i++){
        if(mp[v][i]&&!vis[i]){
            dfs(i);
        }
    }
}
int main()
{
    fill(mp[0],mp[0]+N*N,false);
    scanf("%d %d %d",&n,&m,&k);
    for(int i=0;i<m;i++){
        int a,b;
        scanf("%d %d",&a,&b);
        mp[a][b]=mp[b][a]=true;
    }
    while(k--){
        fill(vis,vis+N,false);//初始化全部未访问
        int x;
        scanf("%d",&x);
        vis[x]=true;//占领的城市设为已访问
        int sum=0;
        for(int i=1;i<=n;i++){//注意城市从1-N

            if(!vis[i]){//在经过一次深搜后仍为访问的结点可以视为一个单独连通分量
                sum++;
                dfs(i);//把该结点所在的连通分量中所有结点设为已访问
            }
        }
        printf("%d\n",sum-1);
    }
    return 0;
}

思路二

BFS 与DFS区别不大,但用的邻接表存储

#include<iostream>
#include<vector>
#include<queue>
 
using namespace std;
 
int N, M, K;
vector<int> graph[1001];	//无向图
bool inq[1001];
int lost_city, count;
 
void bfs(int nowVisit);
 
int main() {
	scanf("%d %d %d", &N, &M, &K);
	int city1, city2;
	for(int i = 0; i < M; i++) {
		scanf("%d %d", &city1, &city2);
		graph[city1].push_back(city2);
		graph[city2].push_back(city1);
	}
	for(int i = 0; i < K; i++) {
		scanf("%d", &lost_city);
		count = 0;
		fill(inq + 1, inq + N + 1, false);
		for(int j = 1; j < N + 1; j++) {
			if(j == lost_city) {
				continue;
			}
			if(!inq[j]) {
				bfs(j);
				count++;
			}
		}
		printf("%d\n", count - 1);
	}
	return 0;
}
 
void bfs(int nowVisit) {
	queue<int> q;
	q.push(nowVisit);
	inq[nowVisit] = true;
	while(!q.empty()) {
		int now = q.front();
		q.pop();
		for(int i = 0; i < graph[now].size(); i++) {
			if(graph[now][i] != lost_city && !inq[graph[now][i]]) {
				q.push(graph[now][i]);
				inq[graph[now][i]] = true;
			}
		}
	}
}

思路三

并查集
推荐这篇博客https://blog.csdn.net/qq_41593380/article/details/81146850

#include<iostream>
#include<vector>
#include<set>
 
using namespace std;
 
struct edge{
	int u, v;
	edge(int _u, int _v){
		u = _u;
		v = _v;
	}
};
 
int N, M, K;
vector<edge> edges;
int lost_city, count;
int father[1001];
set<int> fathers;
 
int findFather(int x);
void unionTwo(int a, int b);
 
int main(){
	scanf("%d %d %d", &N, &M, &K);
	int city1, city2;
	for(int i = 0; i < M; i++){
		scanf("%d %d", &city1, &city2);
		edges.push_back(edge(city1, city2));
	}
	for(int i = 0; i < K; i++){
		for(int j = 1; j < N + 1; j++){
			father[j] = j;
		}
		count = 0;
		fathers.clear();
		scanf("%d", &lost_city);
		for(int j = 0; j < edges.size(); j++){
			if(edges[j].u == lost_city || edges[j].v == lost_city){
				continue;
			}
			unionTwo(edges[j].u, edges[j].v);
		}
		for(int j = 1; j < N + 1; j++){
			if(j == lost_city){
				continue;
			}
			fathers.insert(findFather(j));
		}
		printf("%d\n", fathers.size() - 1);
	}
	return 0;
} 
 
int findFather(int x){
	int a = x;
	while(x != father[x]){
		x = father[x];
	}
	while(a != father[a]){
		int z = a;
		a = father[a];
		father[z] = x;//路径压缩算法
	}
	return x;
}
 
void unionTwo(int a, int b){
	int a_father = findFather(a);
	int b_father = findFather(b);
	if(a_father != b_father){
		father[a_father] = b_father;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值