Tanky WooRSS

HDOJ 1213 How Many Tables

18 Aug 2010
这篇博客是从旧博客 WordPress 迁移过来,内容可能存在转换异常。

题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1213


这题粗略一看好熟悉,原来和 POJ 2524 Ubiquitous Religions 一模一样,就是输出时不用输出Case情况计数。Orz

// Author: Tanky Woo
// HDOJ 1213
// Accepted 1213 0MS 200K 1004 B C++ Tanky Woo 
#include 
using namespace std;

#define MAX 100001

// father[x]表示x的父节点
int father[MAX];
// rank[x]表示x的秩
int rank[MAX];

// 初始化
void Make_Set(int n)
{
    for(int i=1; i<=n; ++i)
    {
        father[i] = i;
        rank[i] = 0;
    }
}

// 查找
int Find_Set(int x)
{
    if(x != father[x])
        return Find_Set(father[x]);
    return x;
}

// 合并
void Union(int x, int y)
{
    x = Find_Set(x);
    y = Find_Set(y);
    if(x == y)  // x,y在同一个集合
        return;
    if(rank[x] > rank[y])
        father[y] = x;
    else if(rank[x] < rank[y])
        father[x] = y;
    else
    {
        rank[y]++;
        father[x] = y;
    }
}

int main()
{
    freopen("input.txt", "r", stdin);
    int nCases;
    int a, b;
    scanf("%d", &nCases;);
    while(nCases--)
    {
        int N, M;
        scanf("%d %d", &N;, &M;);
        Make_Set(N);
        for(int i=0; i<M; ++i)
        {
            scanf("%d %d", &a;, &b;);
            a = Find_Set(a);
            b = Find_Set(b);
            if(a != b)
            {
                Union(a, b);
                N--;
            }
        }
        printf("%d\n", N);
    }
    return 0;
}