并查集

一、题目

输入以整数T(1<=T<=25)开始,表示测试用例的数量。接下来是T个测试用例。每个测试用例以两个整数N和M(1<=N,M<=1000)开头。N表示朋友的数量,从1到N标记朋友,然后M行跟随。每一行包含两个整数A和B(A!=B),意思是朋友A和朋友B互相认识。两个案子之间会有一个空行。不认识的朋友不在一张桌子,求至少需要多少桌子。

二、解析

  • 找到x的掌门人
int find(int x) {
    if (x == fa[x]) return x;
    return fa[x] = find(fa[x]);//路径压缩
}
  • 合并x和y所在的门派
void unite(int x, int y) {
    int fx = find(x);
    int fy = find(y);
    if (fx == fy) return;
    fa[fx] = fy;
}

  • 查看x和y的门派是否相同
bool check(int x, int y) {
    int fx = find(x);
    int fy = find(y);
    if (fx == fy) return true;
    return false;
}

全部代码

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string>
#include<string.h>
#include<algorithm>
#include <vector>

using namespace std;
#define ll long long
#define sc(x) scanf("%d",&x)
#define scc(x, y) scanf("%d%d",&x,&y)
#define p(x) printf("%d\n",x)
#define m(x, y) (x+y)>>1
#define l(x) x<<1
#define r(x) x<<1|1
const int maxn = 1e5 + 6;
int fa[maxn];
int size[maxn];
int n;

//找到x的掌门人
int find(int x) {
    if (x == fa[x]) return x;
    return fa[x] = find(fa[x]);//路径压缩
}

//合并x和y所在的门派
void unite(int x, int y) {
    int fx = find(x);
    int fy = find(y);
    if (fx == fy) return;
    fa[fx]=fy;
}

//查看x和y的门派是否相同
bool check(int x, int y) {
    int fx = find(x);
    int fy = find(y);
    if (fx == fy) return true;
    return false;
}

int main() {
    int t;
    sc(t);
    int m;
    while (t--) {
        scc(n, m);
        //初始化
        for (int i = 1; i <= n; i++) fa[i] = i;
        int ans = 0;//ans是门派的数量
        while (m--) {
            int u, h;
            scc(u, h);
            unite(u, h);
        }
        //查询门派数量
        for (int i = 1; i <= n; i++) if (fa[i] == i) ans++;
        p(ans);
    }

}

按秩合并

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string>
#include<string.h>
#include<algorithm>
#include <vector>

using namespace std;
#define ll long long
#define sc(x) scanf("%d",&x)
#define scc(x, y) scanf("%d%d",&x,&y)
#define p(x) printf("%d\n",x)
#define m(x, y) (x+y)>>1
#define l(x) x<<1
#define r(x) x<<1|1
const int maxn = 1e5 + 6;
int fa[maxn];
int size[maxn];
int n;

//找到x的掌门人
int find(int x) {
    if (x == fa[x]) return x;
    return fa[x] = find(fa[x]);//路径压缩
}

//合并x和y所在的门派
void unite(int x, int y) {
    int fx = find(x);
    int fy = find(y);
    if (fx == fy) return;
    //按秩合并
    if (size[fx] < size[fy]) fa[fx] = fy, size[fy] += size[fx];
    else fa[fy] = fx, size[fx] += size[fy];
}

//查看x和y的门派是否相同
bool check(int x, int y) {
    int fx = find(x);
    int fy = find(y);
    if (fx == fy) return true;
    return false;
}

int main() {
    int t;
    sc(t);
    int m;
    while (t--) {
        scc(n, m);
        //初始化
        for (int i = 1; i <= n; i++) fa[i] = i, size[i] = 1;
        int ans = 0;//ans是门派的数量
        while (m--) {
            int u, h;
            scc(u, h);
            unite(u, h);
        }
        //查询门派数量
        for (int i = 1; i <= n; i++) if (fa[i] == i) ans++;
        p(ans);
    }

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值