【并查集】 ZOJ 3789 Gears

涉及删点,带权的并查集,想清楚每种类型的并查集就好做了。。

#include <iostream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <climits>
#define maxn 600005
#define eps 1e-5
#define mod 1000000007
#define INF 1e9
#define lowbit(x) (x&(-x))
#define min(a, b) (a>b ? b : a)
#define max(a, b) (a>b ? a : b)
#define PI acos(-1)
typedef long long LL;
using namespace std;

int pos[maxn], f[maxn];
int id[maxn], cnt[maxn];
int n, m;
char s[10];
void init(void)
{
	int i;
	for(i=0;i<maxn;i++){
		f[i]=i;
		id[i]=i;
		pos[i]=1;
		cnt[i]=1;
	}
}
int find(int x)
{
	int fa;
	if(x!=f[x]) fa=find(f[x]);
	else fa=x;
	pos[x]=(pos[f[x]]+pos[x])%2;
	f[x]=fa;
	return fa;
}
void debug(void)
{
	int i;
	for(i=1;i<=4;i++)
		printf("%d\n", pos[i]);
}
void solve(void)
{
	int top=n+1, a, b, aa, bb, tmp1, tmp2, tmp;
	while(m--){
		scanf("%s",s);
		if(s[0]=='S'){
			scanf("%d",&tmp);
			printf("%d\n", cnt[find(id[tmp])]);
			continue;
		}
		if(s[0]=='D'){
			scanf("%d",&tmp);
			cnt[find(id[tmp])]--;
			id[tmp]=top++;
			continue;
		}
		if(s[0]=='L'){
			scanf("%d%d",&a,&b);
			aa=find(id[a]);
			bb=find(id[b]);
			if(aa==bb) continue;
			else{
				cnt[aa]+=cnt[bb];
				cnt[bb]=0;
				f[bb]=aa;
				if(pos[id[a]]==pos[id[b]]) pos[bb]^=1;
			}
			continue;
		}
		scanf("%d%d",&a,&b);
		aa=find(id[a]);
		bb=find(id[b]);
		if(aa!=bb){
			printf("Unknown\n");
			continue;
		}
		tmp1=(pos[aa]-pos[id[a]]+2)%2;
		tmp2=(pos[bb]-pos[id[b]]+2)%2;
		if(tmp1==tmp2) printf("Same\n");
		else printf("Different\n");
	}
}
int main(void)
{
	while(scanf("%d%d",&n,&m)!=EOF){
		init();
		solve();
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值