并查集

  • 处理区间合并查询问题
  • 合并集合
  • 询问两个元素是否一个集合
    AcWing 836模板:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>

using namespace std;

const int N = 1e5 + 10;
int p[N];//祖宗节点
int n, m;

int find(int x){
    if(p[x] != x) p[x] = find(p[x]);
    return p[x];
}

int main(){
    scanf("%d%d",&n,&m);
    for(int i = 1; i <= n; i ++) p[i] = i;
    char ch[2];//避免换行问题
    int a, b;
    while(m --){
        scanf("%s%d%d",&ch,&a,&b);
        if(ch[0] == 'M') p[find(a)] = find(b);
        else{
            if(find(a) == find(b)) puts("Yes");
            else puts("No");
        }
    }
    return 0;
}

栗子 (ACWing 837):
题意
代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>

using namespace std;

const int N = 1e5 + 10;
int p[N], s[N];//祖宗节点
int n, m;

int find(int x){
    if(x != p[x]) p[x] = find(p[x]);
    return p[x];
}

int main(){
    scanf("%d%d",&n,&m);
    for(int i = 1; i <= n; i ++) p[i] = i, s[i] = 1;
    string op;//避免换行空格的奇怪问题
    int a, b;
    while(m --){
        cin >> op;
        if(op == "C"){//合并a, b
            scanf("%d%d",&a,&b);
            if(find(a) != find(b)){
                s[find(b)] += s[find(a)];//注意先加再合并
                p[find(a)] = find(b);
            }
        }
        else if(op == "Q1"){//询问a, b
            scanf("%d%d",&a,&b);
            if(find(a) == find(b)) puts("Yes");
            else puts("No");
        }
        else{//a中所在连通块数量
            scanf("%d",&a);
            printf("%d\n",s[find(a)]);
        }
    }
    return 0;
}
  1. x  y 是同一类动物 
    
  2. x 捕食 y
    

询问k次,有几句假话
具体见代码:

#include <bits/stdc++.h>

using namespace std;

const int N = 5e5 + 10;

int n, k;//n种动物,k个询问
int p[N * 3];
int ans;

int find(int x){//查询
    if(x != p[x]) p[x] = find(p[x]);
    return p[x];
}

void merge(int x, int y){//合并
    if(find(x) == find(y)) return ;
    p[find(x)] = y;
}

int main(){
    scanf("%d%d",&n,&k);
    for(int i = 1; i <= 3 * n; i ++) p[i] = i;
    int d, x, y;
    while(k --){
        scanf("%d%d%d",&d,&x,&y);
        if((d == 2 && x == y) || x > n || y > n) {
            ans ++;
            continue;
        }
        if(d == 1) {
            if(find(x) == find(y + n) || find(x) == find(y + 2 * n)){//同类不能捕食,出现反捕食
                ans ++;
                continue;
            }
            else {//3种动物
                merge(x, y);//A类
                merge(x + n, y + n);//B类
                merge(x + 2 * n, y + 2 * n);//C类
            }
        }
        else{
            if(find(x) == find(y) || find(x) == find(y + n * 2)){//同类和反捕食
                ans ++;
                continue;
            }
            else{
                merge(x, y + n);//A->B 
                merge(x + n, y + 2 * n);//B->C;
                merge(x + 2 * n, y);//C->A
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值