hdu-3926 Hand in Hand 并查集

题目链接


1.最大度为2.说明这个图可能有多个连通分量,每个连通分量要么是环,要么是链。

2.然后遍历每个连通分量,记录该连通分量的结点个数,以及该连通分量是环还是链。

3.将第一个图按照结点个数排序(若子结点个数相同,则对链先排序)

4.将第二个图按照步骤三排序

5.比较排序后,2个图是否每个元素都相等。若相等,则相似。



#include <stdio.h>
#include <string.h>
#include <iostream>
#include <queue>
#include <vector>
#include <math.h>
#include <algorithm>
using namespace std;
typedef __int64 LL;
const int maxn = 10005;
const int Mod = 1000000007;
int n1,m1,n2,m2;
int p1[maxn],p2[maxn];
struct node
{
	int son;
	bool ring;
}G1[maxn],G2[maxn];
bool cmp( node a,node b )
{
	if( a.son == b.son )
		return a.ring < b.ring;
	else
		return a.son < b.son;
}
int find( int x,int p[] )
{
	return x == p[x]?x:p[x] = find( p[x],p );
}
void merge( int u,int v,int p[],node G[] )
{
	int x = find( u,p );
	int y = find( v,p );
	if( x == y )  //为环
		G[x].ring = true;
	else
	{
		if( G[x].son >= G[y].son )   //结点相加
		{
			p[y] = x;
			G[x].son += G[y].son;
		}
		else
		{
			p[x] = y;
			G[y].son += G[x].son;
		}
	}
}
bool Compare()
{
	sort( G1+1,G1+n1+1,cmp );
	sort( G2+1,G2+n2+1,cmp );
	for( int i = 1; i <= n1; i ++ )
	{
		if( G1[i].son != G2[i].son || ( G1[i].son == G2[i].son && G1[i].ring != G2[i].ring ) )
			return false;
	}
	return true;
}
int main()
{
	#ifndef ONLINE_JUDGE     
	freopen("data.txt","r",stdin);     
	#endif 
	int cas,u,v,c = 1;
	scanf("%d",&cas);
	while( cas -- )
	{
		int flag = 1;
		scanf("%d%d",&n1,&m1);
		for( int i = 1; i <= n1; i ++ )
		{
			G1[i].son = 1;  G2[i].son = 1;
			G1[i].ring = false; G2[i].ring = false;
			p1[i] = p2[i] = i;
		}
		for( int i = 1; i <= m1; i ++ )
		{
			scanf("%d%d",&u,&v);
			merge( u,v,p1,G1 );
		}
		scanf("%d%d",&n2,&m2);
		if( m1 != m2 )	flag = false;
		for( int i = 1; i <= m2; i ++ ){
			scanf("%d%d",&u,&v);
			if( flag )
				merge( u,v,p2,G2 );
		}
		flag = Compare();
		if( flag )
			printf("Case #%d: YES\n", c ++ );
		else
			printf("Case #%d: NO\n", c ++ );
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值