五行学说

五行学说

带权并查集。

我们发现,克一次相当于生两次。

image-20231008201411483

如图,火、土、金的部分。

发现,这样这个问题就同食物链问题很像了。

#include<cstdio>
#include<algorithm>
using namespace std;
const int mod=5,N=100010;
#define add(a,b) (a+=b)>=mod&&(a-=mod)
#define sub(a,b) (a-=b)<0&&(a+=mod)
int n,m,f[N],d[N];
void read(int &x){
	x=0;
	char c=getchar();
	while(c<'0'||c>'9')c=getchar();
	while(c>='0'&&c<='9'){
		x=x*10+c-48;
		c=getchar();
	}
}
void gc(char &x){
	x=getchar();
	while(x!='k'&&x!='s')x=getchar();
}
int find(int x){
	if(f[x]==x)return x;
	int fa=f[x];
	f[x]=find(f[x]);
	add(d[x],d[fa]);
	return f[x];
}
bool edge(int u,int v,int dis){
	int fu=find(u),fv=find(v);
	if(fu==fv){
		add(dis,d[v]);
		if(dis!=d[u])return 1;
	}
	else{
		f[fu]=v;
		sub(dis,d[u]);
		d[fu]=dis;
	}
	return 0;
}
int main(){
	freopen("theory.in","r",stdin);
	freopen("theory.out","w",stdout);
	read(n),read(m);
	int ans=0;
	for(int i=1;i<=n;++i)f[i]=i;
	while(m--){
		char c;
		int a,b;
		gc(c),read(a),read(b);
		ans+=c=='k'?edge(a,b,2):edge(a,b,1);
	}
	printf("%d",ans);
	return 0;
}
posted @ 2023-10-08 20:16  wscqwq  阅读(5)  评论(0编辑  收藏  举报