|
1 | 1 | import { lowerBound } from "./lowerBound.ts";
|
2 | 2 |
|
3 | 3 | export default class DistanceLimitedPathsExist {
|
4 |
| - snaps: [number, number][][]; |
5 |
| - father: number[]; |
6 |
| - rank: number[]; |
7 |
| - changed: Set<number>; |
| 4 | + #snaps: [number, number][][]; |
| 5 | + #father: number[]; |
| 6 | + #rank: number[]; |
| 7 | + #changed: Set<number>; |
8 | 8 | constructor(n: number, edgeList: number[][]) {
|
9 |
| - this.snaps = Array(n) |
| 9 | + this.#snaps = Array(n) |
10 | 10 | .fill(0)
|
11 | 11 | .map((_, i) => [[0, i]]);
|
12 |
| - this.father = [...Array(n).keys()]; |
13 |
| - this.rank = Array(n).fill(0); |
14 |
| - this.changed = new Set(); |
| 12 | + this.#father = [...Array(n).keys()]; |
| 13 | + this.#rank = Array(n).fill(0); |
| 14 | + this.#changed = new Set(); |
15 | 15 | edgeList.sort((a, b) => a[2] - b[2]);
|
16 | 16 | let curLen = 0;
|
17 | 17 | for (const e of edgeList) {
|
18 | 18 | if (e[2] > curLen) {
|
19 |
| - for (const node of this.changed) { |
20 |
| - this.snaps[node].push([curLen, this.father[node]]); |
| 19 | + for (const node of this.#changed) { |
| 20 | + this.#snaps[node].push([curLen, this.#father[node]]); |
21 | 21 | }
|
22 |
| - this.changed.clear(); |
| 22 | + this.#changed.clear(); |
23 | 23 | curLen = e[2];
|
24 | 24 | }
|
25 |
| - this.union(e[0], e[1]); |
| 25 | + this.#union(e[0], e[1]); |
26 | 26 | }
|
27 | 27 | }
|
28 |
| - union(a: number, b: number) { |
29 |
| - let fa = this.findFather(a); |
30 |
| - let fb = this.findFather(b); |
| 28 | + #union(a: number, b: number) { |
| 29 | + let fa = this.#findFather(a); |
| 30 | + let fb = this.#findFather(b); |
31 | 31 |
|
32 | 32 | if (fa != fb) {
|
33 |
| - if (this.rank[fa] >= this.rank[fb]) { |
| 33 | + if (this.#rank[fa] >= this.#rank[fb]) { |
34 | 34 | [fa, fb] = [fb, fa];
|
35 | 35 | }
|
36 |
| - this.father[fa] = fb; |
37 |
| - this.rank[fb] = Math.max(this.rank[fb], this.rank[fa] + 1); |
38 |
| - this.changed.add(fa); |
| 36 | + this.#father[fa] = fb; |
| 37 | + this.#rank[fb] = Math.max(this.#rank[fb], this.#rank[fa] + 1); |
| 38 | + this.#changed.add(fa); |
39 | 39 | }
|
40 | 40 | }
|
41 | 41 | query(p: number, q: number, limit: number): boolean {
|
42 |
| - return this.findSnapRoot(p, limit) === this.findSnapRoot(q, limit); |
| 42 | + return this.#findSnapRoot(p, limit) === this.#findSnapRoot(q, limit); |
43 | 43 | }
|
44 |
| - findSnapRoot(node: number, limit: number): number { |
| 44 | + #findSnapRoot(node: number, limit: number): number { |
45 | 45 | const index =
|
46 | 46 | lowerBound(
|
47 | 47 | 0,
|
48 |
| - this.snaps[node].length, |
49 |
| - (i: number) => this.snaps[node][i][0] - limit |
| 48 | + this.#snaps[node].length, |
| 49 | + (i: number) => this.#snaps[node][i][0] - limit |
50 | 50 | ) - 1;
|
51 |
| - const f = this.snaps[node][index][1]; |
| 51 | + const f = this.#snaps[node][index][1]; |
52 | 52 |
|
53 | 53 | if (f == node) return f;
|
54 |
| - else return this.findSnapRoot(f, limit); |
| 54 | + else return this.#findSnapRoot(f, limit); |
55 | 55 | }
|
56 |
| - findFather(b: number) { |
| 56 | + #findFather(b: number) { |
57 | 57 | let a = b;
|
58 |
| - while (this.father[a] !== a) { |
59 |
| - a = this.father[a]; |
| 58 | + while (this.#father[a] !== a) { |
| 59 | + a = this.#father[a]; |
60 | 60 | }
|
61 |
| - if (this.father[b] != a) { |
62 |
| - this.changed.add(b); |
| 61 | + if (this.#father[b] != a) { |
| 62 | + this.#changed.add(b); |
63 | 63 | }
|
64 |
| - this.father[b] = a; |
| 64 | + this.#father[b] = a; |
65 | 65 | return a;
|
66 | 66 | }
|
67 | 67 | }
|
0 commit comments