PAT 甲级 1013 Battle Over Cities (25分) 图论:统计连通分量

题目

PAT 甲级 1013 Battle Over Cities (25分) 统计连通分量

思路

1 dfs
2 bfs
3 并查集

题解

dfs

易错

1 每次检查都需要重置vis数组
2 每次检查,被占领城市记为已访问,然后再求连通分量个数,比较方便
3 dfs中没有return,退出条件为:访问完一个连通分量中的所有节点

#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 1100;
int g[maxn][maxn];
int vis[maxn];
int n,m,k,c1,c2,c;
void dfs(int v) {
	vis[v]=1;
	for(int i=1; i<=n; i++) {
		if(vis[i]==1||g[v][i]==0)continue;
		dfs(i);
	}
}
int main(int argc,char * argv[]) {
	// 1 建图
	scanf("%d%d%d",&n,&m,&k);
	for(int i=0; i<m; i++) {
		scanf("%d%d",&c1,&c2);
		g[c1][c2]=g[c2][c1]=1;
	}
	// 2 check
	for(int i=0; i<k; i++) {
		scanf("%d",&c);
		fill(vis,vis+maxn,0); //易错点,每次检查都需要重置 
		// 3 统计连通分量
		vis[c]=1; //被占领的城市记为已访问
		int cnt = 0;
		for(int j=1; j<=n; j++) {
			if(vis[j]==0) {
				cnt++;
				dfs(j);
			}
		}
		printf("%d\n",cnt-1);
	}
	return 0;
}

并查集

易错

1 每轮测试,都必须重置father数组
2 并查集并操作时,被占领的城市c关联的边都跳过
3 进行根个数统计时,跳过被占领的城市c

#include <iostream>
#include <vector>
#include <cstring>
#include <queue>
#include <algorithm>
#include <cmath>
#include <set>
using namespace std;
const int maxn = 1100;
int g[maxn][maxn];
int vis[maxn];
int n,m,k,c1,c2,c;
int father[maxn];
void init(){
	for(int i=0;i<maxn;i++){
		father[i]=i;
	}
} 
int find_f(int x){
	int e = x;
	while(x!=father[x]){
		x=father[x];
	}
	while(e!=father[e]){
		int tmp = father[e];
		father[e]=x;
		e = tmp;
	}
	return x;
}
void Union(int a,int b){
	int af = find_f(a);
	int bf = find_f(b);
	if(af<bf)father[b]=a;
	else father[a]=b;
}
int main(int argc,char * argv[]) {
	// 1 建图
	scanf("%d%d%d",&n,&m,&k);
	for(int i=0; i<m; i++) {
		scanf("%d%d",&c1,&c2);
		g[c1][c2]=g[c2][c1]=1;
	}
	// 2 check
	for(int i=0; i<k; i++) {
		scanf("%d",&c);
		// 3 统计连通分量
		init();
		for(int u=1;u<=n;u++){
			// c关联的边都跳过,其他的边两顶点进行并查集 
			if(u==c)continue;
			for(int j=1;j<=n;j++){
				if(g[u][j]==0||j==c)continue;
				Union(u,j);
			}
		}
		set<int> rts;
		for(int j=1;j<=n;j++){
			if(j==c)continue; 
			rts.insert(find_f(j));
		} 
		printf("%d\n",rts.size()-1);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值