hdu-3926 Hand in Hand 并查集

来源:互联网 发布:淘宝怎么设置支持花呗 编辑:程序博客网 时间:2024/05/16 14:48

题目链接


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;elsereturn 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 ++ );elseprintf("Case #%d: NO\n", c ++ );}return 0;}


0 0