传送门:Ubiquitous Religions

  • 许多次WA,贴上错的代码随时警示
  • 简单没多加修饰的并查集

【WA1】

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

const int maxn = 50005;
int n,m;
int a,b,ans;
int pre[maxn];

void init()
{
    for(int i=1;i<=n;i++){
        pre[i] = i;
    }
}

int findPre(int x){
    if(x==pre[x])    return x;
    findPre(pre[x]);
}

bool isSame(int x,int y)
{
    if(findPre(x)==findPre(y))
        return true;
    return false;
}

void unite(int x,int y)
{
    pre[y] = x;                //y的上级变为x的祖先节点 
}

int main()
{
    int kase = 1; 
    while(scanf("%d%d",&n,&m)==2 && !(n==0&&m==0)){
        init();
        ans = 0;
        for(int i=1;i<=m;i++){
            scanf("%d%d",&a,&b);
            unite(a,b); 
        }
        for(int i=1;i<=n;i++){
            if(i==pre[i])
                ans++;
        }
        printf("Case %d: %d\n",kase++,ans);
    }
    return 0;
} 
View Code

相关文章: