POJ 2524 Ubiquitous Religions

并查集水题,无槽点……

#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>

#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <string>
#include <bitset>
#include <memory>
#include <complex>
#include <numeric>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <ctype.h>
#include <locale.h>

using namespace std;

#pragma pack(4)

const double  eps = 1e-8;
const double   pi = acos(-1.0);
const int     inf = 0x7f7f7f7f;

#define loop(a,n)                            \
    for(int i=0;n>i;i++)                     \
        cout<<a[i]<<(i!=n-1?' ':'\n')
#define loop2(a,n,m)                         \
    for(int i=0;n>i;i++)                     \
        for(int j=0;m>j;j++)                 \
            cout<<a[i][j]<<(j!=m-1?' ':'\n')

#define   at(a,i) ((a)&(1<<(i)))
#define   nt(a,i) ((a)^(1<<(i)))
#define set1(a,i) ((a)|(1<<(i)))
#define set0(a,i) ((a)&(~(1<<(i))))

#define cmp(a,b) (fabs((a)-(b))<eps?0:(((a)-(b))>eps?+1:-1))

#define lmax(a,b) ((a)>(b)?(a):(b))
#define lmin(a,b) ((a)<(b)?(a):(b))
#define fmax(a,b) (cmp(a,b)>0?(a):(b))
#define fmin(a,b) (cmp(a,b)<0?(a):(b))

const int MAXV = 50002;

class UFS
{
private:
    int Pa[MAXV];
public:
    void clear(int l,int r)
    {
        for(int i=l;i<=r;i++) Pa[i]=i;
    }
    int Find(int u)
    {
        return Pa[u]=(Pa[u]==u?u:Find(Pa[u]));
    }
    void Union(int u,int v)
    {
        Pa[Find(u)]=Find(v);
    }
}ufs;

int n,m,u,v,times=1,ans[MAXV];

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("Ubiquitous Religions.txt","r",stdin);
    #else
    #endif

    while(scanf("%d %d",&n,&m),n||m)
    {
        ufs.clear(1,n);
        while(m--)
        {
            scanf("%d %d",&u,&v);
            ufs.Union(u,v);
        }
        for(int i=1;n>=i;i++)
        {
            ans[i-1]=ufs.Find(i);
        }
        printf("Case %d: %d\n",times++,(sort(ans,ans+n),unique(ans,ans+n)-ans));
    }

    return 0;
}

你可能感兴趣的:(POJ 2524 Ubiquitous Religions)