并查集求解
  • 板块P2307 迷宫
  • 楼主Mikemao666
  • 当前回复2
  • 已保存回复2
  • 发布时间2020/8/4 11:39
  • 上次更新2023/11/6 21:20:56
查看原帖
并查集求解
267517
Mikemao666楼主2020/8/4 11:39
#include <bits/stdc++.h>
using namespace std;
const int MAXI=100001;
int f[MAXI],a,b;
int n,tot;
bool is_circle,p[MAXI];
void fi() {
	for(int i=1; i<=MAXI; ++i)f[i]=i,p[i]=false;
	is_circle=false;
	tot=0;
}
int find(int x) {
	if(x!=f[x])
		f[x]=find(f[x]);
	return f[x];
}
int main() {
	while(scanf("%d%d",&a,&b)) {
		if(a==-1&&b==-1)break;
		if(a==b&&a==0) {
			if(is_circle==false&&tot==1)puts("1");
			else puts("0");
			fi();
			continue;
		}
		if(!p[a])tot++;
		if(!p[b])tot++;
		p[a]=p[b]=true;
		int a1=find(a),b1=find(b);
		if(a1!=b1)f[a1]=b1,tot--;
		else is_circle=true;
	}
	return 0;
}

Rt,全输出0.。。

2020/8/4 11:39
加载中...