POJ 1703 Find them, Catch them(带权并查集)

55 篇文章 2 订阅
27 篇文章 0 订阅

数组开太小会time error。

用cin cout也会time error。

 /*
 qq:1239198605
 ctgu_yyf
        */

#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<cstring>
#include<algorithm>
#include<cmath>
#define ll long long
using namespace std;
int par[100005*2];
int rank[100005*2];
int n,m; 

void init(int n)
{
	for(int i=1;i<=n;i++)
	{
	par[i]=i;
	rank[i]=0;
	}
}

int find(int x)
{
	if(par[x]==x)
	return x;
	else
	return par[x]=find(par[x]);
}

void unite(int x,int y)
{
	x=find(x);
	y=find(y);
	if(x==y)
	return ;
	
	if(rank[x]<rank[y])
	par[x]=y;
	
	else
	{
		par[y]=x;
		if(rank[x]==rank[y])
		rank[x]++;
	}
}

bool same(int x,int y)
{
	return find(x)==find(y);
}

int main()
{
   //ios::sync_with_stdio(false);
   int t;
   scanf("%d",&t);
   while(t--)
   {
   	scanf("%d%d",&n,&m);
   	
   	init(n*2);
   	
   	char c[2];
   	int u,v;
   	for(int i=0;i<m;i++)
   	{
   		scanf("%s",c);
   		scanf("%d%d",&u,&v);
   		if(c[0]=='A')
   		{
   				if(same(u,v)||same(u+n,v+n))
   				cout<<"In the same gang."<<endl;
   				else if(same(u,v+n)||same(u+n,v))
   				cout<<"In different gangs."<<endl;
   				else
   				cout<<"Not sure yet."<<endl;
   			
		}
		
		if(c[0]=='D')
		{
			unite(u,v+n);
			unite(v,u+n);
		}
   			
	}
   	
   	
   }
   


return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值