10pts 带权并查集 代码求调
查看原帖
10pts 带权并查集 代码求调
739899
p256rrq楼主2024/4/29 19:05

WA on #6

#include<bits/stdc++.h>
using namespace std;
const int N=1e3+5,M=5e3+5;
int n,m;
int f[N],rel[N];
bitset<N>b0,b1;
inline int read(){
	int sym=1,res=0;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-') sym=-1;
		ch=getchar();
	}while(ch>='0'&&ch<='9'){
		res=(res<<3)+(res<<1)+(ch^48);
		ch=getchar(); 
	}return res*sym;
}
void print(int x){
	if(x<0){
		x=-x;
		putchar('-');
	}if(x>=10) print(x/10);
	putchar(x%10^48);
}
int find(int x){
	if(x==f[x]) return x;
	int ft=find(f[x]);
	rel[x]=(rel[x]+rel[f[x]])%2;
	return f[x]=ft;
}
inline void merge(int x,int y,int r){
	int fx=find(x),fy=find(y);
	if(fx==fy) return;
	f[fx]=fy;rel[fx]=(r+rel[x])%2;
}
int main(){
	int p,q;char opt;
	n=read();m=read();
	for(int i=1;i<=n;++i) f[i]=i;
	while(m--){
		opt=getchar();p=read();q=read();
		if(opt=='E') merge(p,q,1);
		else merge(p,q,0);
	}
	for(int i=1;i<=n;++i){
		int ft=find(i);
		if(rel[i]) b1[ft]=1;
		else b0[ft]=1;
	}	
	int ans=0;
	for(int i=1;i<=n;++i)
		ans+=b0[i]+b1[i];
	print(ans);putchar('\n');
	return 0;
} 
2024/4/29 19:05
加载中...