-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
106 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { EulerTour } from "../euler_tour/main.ts"; | ||
|
||
export class LCA extends EulerTour { | ||
#dup: number[][] = []; | ||
constructor( | ||
edges: ([number, number] | [number, number, number])[], | ||
root = 0, | ||
) { | ||
const len = edges.length + 1; | ||
super(edges, root); | ||
let p: number[] = []; | ||
for (let i = 0; i < len; i += 1) { | ||
p.push(this.parent(i)); | ||
} | ||
this.#dup.push(p); | ||
if (len === 1) return; | ||
while (1) { | ||
const np: number[] = []; | ||
let count = 0; | ||
for (let i = 0; i < len; i += 1) { | ||
const v = p[p[i]] ?? -1; | ||
if (v !== -1) count += 1; | ||
np.push(v); | ||
} | ||
if (count === 0) break; | ||
this.#dup.push(np); | ||
p = np; | ||
} | ||
this.#dup.reverse(); | ||
} | ||
|
||
/** | ||
* 頂点`u`と頂点`v`の最小共通祖先を求める | ||
* @param u 1つめの頂点の番号 | ||
* @param v 2つめの頂点の番号 | ||
* @returns 最小共通祖先の番号 | ||
*/ | ||
lca (u: number, v: number): number { | ||
if (this.inSubtree(u, v)) return u; | ||
let y = u; | ||
for (const p of this.#dup) { | ||
if (p[y] === -1) continue; | ||
if (!this.inSubtree(p[y], v)) y = p[y]; | ||
} | ||
return this.parent(y); | ||
} | ||
|
||
/** | ||
* 根から頂点`v`への距離を返す | ||
* @param v 対象の頂点 | ||
* @returns 根から頂点`v`への距離 | ||
*/ | ||
cost (v: number): number; | ||
/** | ||
* 頂点`u`から頂点`v`への距離を返す | ||
* @param u 1つめの頂点 | ||
* @param v 2つめの頂点 | ||
* @returns 頂点`u`,`v`間の距離 | ||
*/ | ||
cost (u: number, v: number): number; | ||
cost (u: number, v?: number): number { | ||
if (!v) { // vは0 or undefined.どちらも根からの距離 | ||
return super.cost(u); | ||
} | ||
return super.cost(u) + super.cost(v) - 2 * super.cost(this.lca(u, v)); | ||
} | ||
} |