并查集专题

并查集专题

Wireless Network

POJ - 2236

思路

并查集裸题,路径压缩+判断每一个是否满足距离小于等于d的条件,这里可以不sqrt,判断与d*d的大小即可

C++(AC)

#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<vector>
#include<cmath> 
#include<map> 
#define ll long long
#define pii pair<int,int>
#define mpr make_pair
#define inf 0x3f3f3f3f 
#define INF 0x3f3f3f3f3f3f3f3f
#define clr(x) memset(x,0,sizeof x)
#define rep2(i, x) for(register int i = h[x]; i; i = e[i].nxt)
#define rep(i,x,y) if((x)<=(y))for(register int i=(x);i<=(y);i++)
#define rrep(i,x,y) if((x)>=(y))for(register int i=(x);i>=(y);i--)
#define endl "\n" 
#define TT() int t;cin>>t;while(t--) 
#define PI acos(-1)
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const int mod=1e9+7;
using namespace std;
int pre[10000];
int find(int a)
{
	if (pre[a]!=a)
	  return pre[a]=find(pre[a]);
	else
	  return a;
}

int uni(int a,int b)
{
	a=find(a);
	b=find(b);
	if (a!=b)
	  pre[a]=b;
}
	
int main(){
	std::ios::sync_with_stdio(true),cin.tie(0),cout.tie(0);
	int n,m,a[2000],tot=0,d,x[2000],y[2000],p,q;
	char c;
	cin>>n>>d;
	rep(i,1,n)
	{
		cin>>x[i]>>y[i];
		pre[i]=i;
	}
	getchar();
	while (cin>>c)
	{
		if (c=='O')
		{
			cin>>p;
		 	a[++tot]=p;
		 	rep(i,1,tot-1)
		 	{
		 		if ((x[a[i]]-x[p])*(x[a[i]]-x[p])+(y[a[i]]-y[p])*(y[a[i]]-y[p])<=d*d)
		 		  uni(a[i],p);
		 	}
	    }
	    else
	    {
	    	cin>>p>>q;
	    	if (find(p)==find(q))
	    		cout<<"SUCCESS"<<endl;
	    	else
	    		cout<<"FAIL"<<endl;
	    }
	    getchar();
	}
	return 0;
}

The Suspects

POJ - 1611

思路

记录结点数的并查集,板子见yjy的板子

C++(AC)

#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<vector>
#include<cmath> 
#include<map> 
#define ll long long
#define pii pair<int,int>
#define mpr make_pair
#define inf 0x3f3f3f3f 
#define INF 0x3f3f3f3f3f3f3f3f
#define clr(x) memset(x,0,sizeof x)
#define rep2(i, x) for(register int i = h[x]; i; i = e[i].nxt)
#define rep(i,x,y) if((x)<=(y))for(register int i=(x);i<=(y);i++)
#define rrep(i,x,y) if((x)>=(y))for(register int i=(x);i>=(y);i--)
#define endl "\n" 
#define TT() int t;cin>>t;while(t--) 
#define PI acos(-1)
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const int mod=1e9+7;
using namespace std;
int pre[50010];
int find(int x)
{
	if (pre[x]<0)
	  return x;
	else
	  return pre[x]=find(pre[x]);
}

int uni(int x,int y)
{
	x=find(x);
	y=find(y);
	if (x!=y)
	{
	  if (pre[x]<pre[y]){
		pre[x]+=pre[y];
		pre[y]=x;
	  }
	  else
	  {
	  	pre[y]+=pre[x];
	  	pre[x]=y;
	  }
    }
}
	
int main(){
	std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	int n,m,k,a[50010];
	while (true)
	{
	cin>>n>>m;
	rep(i,0,n)
	  pre[i]=-1;
	if (n==0 and m==0)
	  break;
	rep(i,1,m)
	{
		cin>>k;
		rep(j,1,k)
		  cin>>a[j];
		rep(j,2,k)
		  uni(a[j-1],a[j]);	
	}
    cout<<-pre[find(0)]<<endl;
	}
	return 0;
}

posted @ 2020-09-23 20:47  IamIron-Man  阅读(116)  评论(0)    收藏  举报