zoukankan      html  css  js  c++  java
  • 并查集——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;
    } 
    
  • 相关阅读:
    Spring Boot面试杀手锏————自动配置原理
    session在什么时候创建,以及session一致性问题
    IaaS、PaaS、SaaS、DaaS都是什么?现在怎么样了?终于有人讲明白了
    FaaS,未来的后端服务开发之道
    架构师必须了解的30条设计原则
    vuejs2.0使用Sortable.js实现的拖拽功能( 转)
    Spring配置中的"classpath:"与"classpath*:"的区别研究(转)
    Java静态类
    CGLIB(Code Generation Library)详解
    hive--udf函数(开发-4种加载方式)
  • 原文地址:https://www.cnblogs.com/xzxl/p/7337678.html
Copyright © 2011-2022 走看看