zoukankan      html  css  js  c++  java
  • poj 1703 Finde them, Catch them! (并查集)

    跟2492基本一样,不多说。

    code:

    #include<cstdio>
    using namespace std ;
    int f[100005] ;
    int r[100005] ;
    int n, m ;
    int find_Set(int x){
        int temp ;
        if(x==f[x]){
            return x ;
        }
        temp = f[x] ;
        f[x] = find_Set(temp) ;
        r[x] = (r[x]+r[temp]) % 2 ;//保持r[x]相对于根节点的稳定
        return f[x] ;
    }
    void Union(int x, int y, int fx, int fy){
        f[fy] = fx ;
        r[fy] = (r[x]+r[y]+1) % 2 ;
    }
    int main(){
        int t, i, j, x, y, fx, fy ;
        char c ;
        scanf("%d", &t) ;
        while(t--){
            scanf("%d%d", &n, &m) ;
            for(i=0; i<=n; i++){
                f[i] = i ;
                r[i] = 0 ;
            }
            getchar() ;
            while(m--){
                scanf("%c%d%d", &c, &x, &y) ;
                getchar() ;
                fx = find_Set(x) ;
                fy = find_Set(y) ;
                if(c=='D'){
                    Union(x, y, fx, fy) ;
                }
                if(c=='A'){
                    if(fx==fy){
                        if(r[x]==r[y])
                            printf("In the same gang.\n") ;
                        else
                            printf("In different gangs.\n") ;
                    }
                    else
                        printf("Not sure yet.\n") ;
                }
            }
        }
        return 0 ;

  • 相关阅读:
    IE表单拦截器
    使用网站作为WCF服务宿主的几种做法
    Javascript执行效率小结
    Ajax无刷新技术实现省市县三级联动下拉菜单Asp.Net
    序列化DataContractSerializer
    变化多端的列表
    腾讯微信
    Mac OS 和 iOS 操作系统架构
    程序员远离你的细节思维
    ObjectiveC概述
  • 原文地址:https://www.cnblogs.com/xiaolongchase/p/2332475.html
Copyright © 2011-2022 走看看