🖥

[C++] Union-Find 기본 함수

망록 2023. 8. 23.
#include <iostream>
using namespace std;

int F(int a){
    if (a == parent[a])
        return a;
    return parent[a] = F(parent[a]);
}
void U(int a, int b){
    int rootA = F(a);
    int rootB = F(b);
    if (rootA == rootB)
        return;
    
    parent[rootB] = rootA;
    cnt[rootA] += cnt[rootB];
    cnt[rootB] = 0;
}

댓글