并查集--HDU - 3926 Hand in Hand

Description

In order to get rid of Conan, Kaitou KID disguises himself as a teacher in the kindergarten. He knows kids love games and works out a new game called "hand in hand". 

Initially kids run on the playground randomly. When Kid says "stop", kids catch others' hands immediately. One hand can catch any other hand randomly. It's weird to have more than two hands get together so one hand grabs at most one other hand. After kids stop moving they form a graph. 

Everybody takes a look at the graph and repeat the above steps again to form another graph. Now Kid has a question for his kids: "Are the two graph isomorphism?" 
 

Input

The first line contains a single positive integer T( T <= 100 ), indicating the number of datasets. 
There are two graphs in each case, for each graph: 
first line contains N( 1 <= N <= 10^4 ) and M indicating the number of kids and connections. 
the next M lines each have two integers u and v indicating kid u and v are "hand in hand". 
You can assume each kid only has two hands. 
 

Output

For each test case: output the case number as shown and "YES" if the two graph are isomorphism or "NO" otherwise. 
 

Sample Input

    
    
2 3 2 1 2 2 3 3 2 3 2 2 1 3 3 1 2 2 3 3 1 3 1 1 2
 

Sample Output

    
    
Case #1: YES Case #2: NO


分析:并查集求每个连通分量结点数

1、最大度为2,说明有连通分量,每个连通分量可能是环

2、每次合并结点时更新连通分量结点个数并判断是否为环

3、将两个图对应的连通分量分别按结点个数排序(若结点数相同则链先于环)

4、比较排序结果是否一致(题中所述为两图是否相似,即结点个数是否相等及是否存在环)


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
const int maxn = 10000 + 10;
struct graph { int nodes; bool ring; };
int p1[maxn], p2[maxn];
graph g1[maxn], g2[maxn];

int find_set(int x, int* p)
{
	return x==p[x]?x:(p[x]=find_set(p[x], p));
}

void union_set(int u, int v, int* p, graph* g)
{
    u = find_set(u, p);
    v = find_set(v, p);
    if(u == v) {
		g[u].ring = g[v].ring = true; //如果u、v的根结点相同则是同一分量,形成环
    } else {
        p[u] = v;
        g[v].nodes += g[u].nodes;    //如果是不同分量则合并,并且更新u、v的结点数
        g[u].nodes = 1; g[u].ring = false;
    }
}

bool cmp(const graph& a, const graph& b)
{
	if(a.nodes == b.nodes) return a.ring;
	return a.nodes > b.nodes;
}

int main()
{
	int T, kase = 0;
	scanf("%d", &T);
	while(T--) {
		int n1, n2, m1, m2;
		scanf("%d%d", &n1, &m1);
		for(int i=0; i<=n1; i++) {     //预处理
			p1[i] = p2[i] = i;
			g1[i].nodes = g2[i].nodes = 1;
			g1[i].ring = g2[i].ring = false;
		}
		for(int i=0; i<m1; i++) {
            int u, v;
            scanf("%d%d", &u, &v);
            union_set(u, v, p1, g1);
		}
		scanf("%d%d", &n2, &m2);
		bool flag = (n1==n2 && m1==m2);
		for(int i=0; i<m2; i++) {
			int u, v;
			scanf("%d%d", &u, &v);
			if(flag) union_set(u, v, p2, g2);
		}
        if(flag) {
            sort(g1, g1+n1+1, cmp);
            sort(g2, g2+n1+1, cmp);
            for(int i=0; i<=n1; i++)
				if(g1[i].nodes!=g2[i].nodes || g1[i].ring!=g2[i].ring) {
					flag = false; break;
				}
        }
        printf("Case #%d: %s\n", ++kase, flag?"YES":"NO");
	}
    return 0;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值