• 并查集——poj2524(入门)


    传送门: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
    ",kase++,ans);
        }
        return 0;
    } 
    View Code

    【WA2】

    #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]){  
            pre[x] = findPre(pre[x]);  
        }  
        return 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
    ",kase++,ans);
        }
        return 0;
    } 
    View Code

    【WA3】

    #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)
    {
        x = findPre(x);
        y = findPre(y);
        if(x==y)    return;
        ans--;
        pre[y] = x;                //y的上级变为x的祖先节点 
    }
    
    int main()
    {
        int kase = 1; 
        while(scanf("%d%d",&n,&m)==2 && !(n==0&&m==0)){
            init();
            ans = n;
            for(int i=1;i<=m;i++){
                scanf("%d%d",&a,&b);
                unite(a,b); 
            }
            printf("Case %d: %d
    ",kase++,ans);
        }
        return 0;
    } 
    View Code

    【AC】

    #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]){  
            pre[x] = findPre(pre[x]);  
        }  
        return pre[x]; 
    }
    
    bool isSame(int x,int y)
    {
    	if(findPre(x)==findPre(y))
    		return true;
    	return false;
    }
    
    void unite(int x,int y)
    {
    	x = findPre(x);
    	y = findPre(y);
    	if(x==y)	return;
    	ans--;
    	pre[y] = x;				//y的上级变为x的祖先节点 
    }
    
    int main()
    {
    	int kase = 1; 
    	while(scanf("%d%d",&n,&m)==2 && !(n==0&&m==0)){
    		init();
    		ans = n;
    		for(int i=1;i<=m;i++){
    			scanf("%d%d",&a,&b);
    			unite(a,b); 
    		}
    		printf("Case %d: %d
    ",kase++,ans);
    	}
    	return 0;
    } 
    
  • 相关阅读:
    格式刷的一小步,原型工具的一大步
    精益设计,敏捷开发,一个都不能少
    慢工出细活,Facebook点赞按钮设计中的门道
    5个范例告诉你什么是自适应网页设计
    用户体验设计5大目标
    poj1251Jungle Roads(最小生成树)
    hdu2222Keywords Search
    hdu2328Corporate Identity
    hdu1238Substrings
    hdu4763Theme Section
  • 原文地址:https://www.cnblogs.com/xzxl/p/7337678.html
Copyright © 2020-2023  润新知