bzoj4690: Never Wait for Weights 并查集

Posted f321dd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了bzoj4690: Never Wait for Weights 并查集相关的知识,希望对你有一定的参考价值。

裸带权并查集

#include<cstdio>
#define N 100005
int m,i,j,s,t,u,d[N],p[N];
char k;
int find(int i){
	if(p[i]^i&&find(p[i]))
		d[i]+=d[p[i]];
	return p[i]=p[p[i]];
}
int main(){
	while(scanf("%d%d",&s,&m)&&s){
		for(;s;--s)
			d[p[s]=s]=0;
		while(m--){
			scanf(" %c%d%d",&k,&s,&t);
			i=find(s),j=find(t);
			if(k==‘!‘){
				scanf("%d",&u);
				p[i]=j;
				d[i]=d[t]+u-d[s];
			}else if(i^j)
				puts("UNKNOWN");
			else
				printf("%d\n",d[s]-d[t]);
		}
	}
}

  

以上是关于bzoj4690: Never Wait for Weights 并查集的主要内容,如果未能解决你的问题,请参考以下文章

BZOJ-4690Never Wait For Weights 带权并查集

bzoj4690 Never Wait for Weights

bzoj4690: Never Wait for Weights 并查集

BZOJ4690: Never Wait for Weights

bzoj 4690&&4602

[TypeScript] Use TypeScript’s never Type for Exhaustiveness Checking