跳到正文

算法·理论

FAc 算法模版新版

FlowerAccepted约 2 分钟
原文

这是 2025年11月10日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年1月13日
篇幅
约 2 分钟

版本

  1. v2当前
  2. v1

缺省源

C++
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;
#define inlfc __attribute__((always_inline)) inline
typedef pair<int, int> pii;
typedef long long ll;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    return 0;
}

并查集

C++
struct Dsu {
    int f[MAXN], n;
    ll s[MAXN];
    inlfc void init(int _n) {
        n = _n;
        for (int i = 1; i <= n; i ++) {
            f[i] = i;
            s[i] = 1;
        }
    }
    inlfc int find(int x) {
        while (x != f[x]) {
            f[x] = f[f[x]];
            x = f[x];
        }
        return x;
    }
    inlfc void merge(int x, int y) {
        x = find(x);
        y = find(y);
        if (s[x] < s[y]) {
            swap(x, y);
        }
        f[y] = x;
        s[x] += s[y];
    }
    inlfc bool check(int x, int y) {
        return find(x) == find(y);
    }
};