poj 1703 , hdu 1729 , poj 1182 并查集偏移量应用

http://poj.org/problem?id=1703  Find them, Catch them

 

偏移量:维护所有集合的根的偏移量都为0。在集合合并时,若两集合对应偏移量不同,改变被合并集合根的偏移量,而集合内部的偏移量暂不改变,在以后查询某点偏移量时,先调用find函数,在find函数中递归更新偏移量值。

 

union中集合合并时,把y所在集合放到x所在集合,若x与y是同类,则y应该加上的值是(x-y), 同样合并后,y所在的集合的根也应该加上(x-y)


find函数中, 查找时, g[p[x]] 集合合并前值为0, 合并后增加了 g[p[x]], 故 g[x] 也应该加上g[p[x]]

 

 

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<algorithm>
using namespace std;
int a[100010], g[100010], p[100010], m, n;
int find( int x){
    if( x==p[x]) return p[x];
    int t= find(p[x]);
    g[x]= ( g[x]+ g[p[x]]+ 2)%2;
    p[x]= t;
    return p[x];
}
void union_xyA( int x, int y){
    int i, j, k, px, py;
    px= find( x), py= find( y);
    if( px== py){
        if( g[x]== g[y]) printf("In the same gang.\n");
        else printf("In different gangs.\n");
    }
    else printf("Not sure yet.\n");
}
void union_xyD(int x, int y){
    int i, j, k, px, py;
    px= find( x), py= find(y);
    if( px!= py){
        g[py]= (g[x]-g[y]+5)%2;
        p[py]= px;
    }
}
int main(){
   // freopen("1.txt", "r", stdin);
    int T, x, y, i;
    char temp[5];
    scanf("%d", &T);
    while( T--){
        scanf("%d%d", &n, &m);
        memset( g, 0, sizeof( g));
        for( i=0; i<=n; i++)
            p[i]= i;
        while( m--){
            scanf("%s%d%d", temp, &x, &y);
            //cout<<temp<<" "<<x<<" "<<y<<endl;
            if( temp[0]=='A') union_xyA(x, y);
            else union_xyD(x, y);
        }
    }
    return 0;
}


 

 

http://acm.hdu.edu.cn/showproblem.php?pid=1829  A Bug's Life

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn= 2100;
int m, n, p[maxn], off[maxn];
int find(int x){
    if( p[x]== x) return x;
    int t= find(p[x]);
    off[x]= ( off[x]+ off[p[x]]+ 2)%2;
    p[x]= t;
    return p[x];
}
int union_xy(int x, int y){
    int px, py;
    px= find( x);
    py= find( y);
    if( px== py){
        if( off[x] == off[y]) return 1;
    }
    else{
        off[py]= (off[x]- off[y]+ 5)%2;
        p[py]= px;
    }
    return 0;
}
int main(){
   // freopen("1.txt", "r", stdin);
    int i, j, T, x, y, flag, text=1;
    scanf("%d", &T);
    while( T-- ){
        scanf("%d%d", &m, &n);
        for( i=0; i<=m; i++)
            p[i]= i;
        memset( off, 0 , sizeof( off));
        flag= 0;
        while( n--){
            scanf("%d%d", &x, &y);
            if( !flag) flag= union_xy( x, y); 
            else continue;
        }
        printf("Scenario #%d:\n", text++);
        if( flag) printf("Suspicious bugs found!\n\n");
        else printf("No suspicious bugs found!\n\n");
    }
    return 0;
}


 

 

http://poj.org/problem?id=1182 食物链

 

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn= 50001;
int n, k, ans, p[maxn], off[maxn];
int find( int x){
    if( p[x]== x)
        return x;
    int t= find( p[x]);
    off[x]= (off[x]+off[p[x]])%3 ;
    p[x]= t;
    return p[x];
}
void union_xy(int x, int y, int a, int b, int offset){
    if( offset==1){
        p[y]= x;
        off[y]= (off[a] - off[b] +3 ) %3;
    }
    else{
        p[y]= x;
        off[y]= (off[a]- off[b] + 2)%3;
    }
}
void scan(){
    int a, b, d, pa, pb;
    scanf("%d%d%d", &d, &a, &b);
    if( a>n || b>n ||( d==2 && a==b))
        ans++;
    else{
        pa= find( a);
        pb= find( b);
       // cout<<pa<<" "<<pb<<endl;
        if( pa==pb){
          //  cout<<off[pa]<<" "<<off[pb]<<" "<<endl;
           // cout<<off[a]<<" "<<off[b]<<" "<<endl;
            if( d== 1 && (off[a]-off[b] +3 )%3 !=0)
                ans++;
            else if( d==2 && (off[a]-off[b]+3)%3 !=1)
                ans++;
        }
        else
            union_xy( pa, pb, a, b, d);
    }
}
int main()
{
  //  freopen("1.txt","r",stdin);
    int  i;
    ans= 0;
    scanf("%d%d", &n, &k);
    for( i=1; i<=n; i++)
        p[i]= i;
    memset( off, 0, sizeof( off));
    for( i=0; i<k; i++){
        scan();
    }
    printf("%d\n", ans);
    return 0;
}


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值