Blog

Twoja wymarzona praca? Lets Git IT.
Interaktywna platforma przygotowująca do rozmów technicznych dla nowoczesnych programistów.

XGitHub

Platforma

  • Kategorie

Zasoby

  • Blog
  • O aplikacji
  • FAQ
  • Sugestie

Prawne

  • Polityka prywatności
  • Regulamin

© 2025 LetsGit.IT. Wszelkie prawa zastrzeżone.

LetsGit.IT/Kategorie/Struktury danych
Struktury danychhard

Jaki problem rozwiązuje Union-Find (DSU)?

Tagi
#union-find#dsu#kruskal#connectivity
Wróć do kategoriiPrzejdź do quizu

Odpowiedź

Union-Find (DSU) utrzymuje rozłączne zbiory operacjami find(x) (reprezentant) i union(a,b) (scalenie). Używa się go do spójności i w algorytmie Kruskala; z path compression + union by rank operacje są prawie O(1) amortyzowane.

class DSU {
  private parent: number[];

  constructor(n: number) {
    this.parent = Array.from({ length: n }, (_, i) => i);
  }

  find(x: number): number {
    if (this.parent[x] === x) return x;
    this.parent[x] = this.find(this.parent[x]);
    return this.parent[x];
  }

  union(a: number, b: number) {
    const ra = this.find(a);
    const rb = this.find(b);
    if (ra !== rb) this.parent[rb] = ra;
  }
}

Powiązane pytania

Algorytmy
Kruskal vs Prim: jaka jest główna różnica i typowe struktury danych?
#mst#kruskal#prim
Algorytmy
Kruskal vs Prim dla MST — czym się różnią?
#mst#kruskal#prim