youngyangyang04 / leetcode-master-comment

用来做评论区
0 stars 0 forks source link

[Vssue]kamacoder/图论并查集理论基础.md #130

Open youngyangyang04 opened 2 months ago

youngyangyang04 commented 2 months ago

https://www.programmercarl.com/kamacoder/%E5%9B%BE%E8%AE%BA%E5%B9%B6%E6%9F%A5%E9%9B%86%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80.html

Du1in9 commented 1 month ago
static private int[] father;

static private void init(int n) {
    father = new int[n + 1];
    for (int i = 1; i <= n; i++) father[i] = i;
}

static private int find(int u) {
    if (u == father[u]) return u;
    else return find(father[u]);
}

static private void join(int u, int v) {
    u = find(u);
    v = find(v);
    if (u != v) father[v] = u;
}
chaofengdev commented 1 week ago

这里讲的不太行,举的例子也不够好。其实这里的秩不仅仅指树的高度,而是一种抽象表示。这里甚至可以是树的节点数。