标签:als std 素数 false c++ int == include 节点
#include <bits/stdc++.h>
using namespace std;
const int N = 1000;
int n;
int p[N + 1], s[N + 1];                 //数组p用于存储祖先节点,数组s用于存储当前祖先节点下并查集的元素个数
//并查集初始化, 给所有祖先节点标号并且所有并查集的元素个数初始化为1
void init() {
    for (int i = 0; i <= n; i++) {
        p[i] = i;
        s[i] = 1;
    }
}
//查找当前节点的祖先节点
int find(int x) {
    return p[x] == x ? x : find(p[x]);  //如果当前节点是祖先节点则直接返回,否则向上查找祖先节点
}
//合并以x和y为祖先节点的并查集
void add(int i, int j) {
    int x = find(i);
    int y = find(j);
    if (x == y) return;                 //两个并查集不在同一棵树上才能合并
    p[y] = x;                           //将y加到x上, y的祖宗节点变为x
    s[x] += s[y];                       //将原先并查集y上的元素数量也加到并查集x上
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    init();
    //这部分自己根据题目来填
    
    return 0;
}
标签:als std 素数 false c++ int == include 节点
原文地址:https://www.cnblogs.com/xiezeju/p/14458679.html