|
| 1 | +/** |
| 2 | + * @param {number} n |
| 3 | + * @param {number[][]} edges |
| 4 | + * @param {number[][]} queries |
| 5 | + * @return {number[]} |
| 6 | + */ |
| 7 | +const findMedian = (n, edges, queries) => { |
| 8 | + const graph = {} |
| 9 | + for (const [u, v, w] of edges) { |
| 10 | + if (!graph[u]) graph[u] = {} |
| 11 | + if (!graph[v]) graph[v] = {} |
| 12 | + graph[u][v] = w |
| 13 | + graph[v][u] = w |
| 14 | + } |
| 15 | + const lca = new LCA(graph) |
| 16 | + return queries.map(([u, v]) => lca.median(u, v)) |
| 17 | +} |
| 18 | + |
| 19 | +class LCA { |
| 20 | + constructor(graph, root = 0) { |
| 21 | + this.graph = graph |
| 22 | + this.ancestors = {} |
| 23 | + this.timeIn = {} |
| 24 | + this.timeOut = {} |
| 25 | + this.timer = 0 |
| 26 | + this.depth = {} |
| 27 | + this.dist = {} |
| 28 | + this.dfs(root, root, [root]) |
| 29 | + } |
| 30 | + |
| 31 | + dfs(v, parent, path, d = 0) { |
| 32 | + this.timer += 1 |
| 33 | + this.timeIn[v] = this.timer |
| 34 | + let up = 1 |
| 35 | + while (path.length >= up) { |
| 36 | + if (!this.ancestors[v]) this.ancestors[v] = [] |
| 37 | + this.ancestors[v].push(path[path.length - up]) |
| 38 | + up *= 2 |
| 39 | + } |
| 40 | + this.depth[v] = path.length |
| 41 | + this.dist[v] = d |
| 42 | + path.push(v) |
| 43 | + for (const [u, w] of Object.entries(this.graph[v])) { |
| 44 | + if (u != parent) { |
| 45 | + this.dfs(+u, v, path, d + w) |
| 46 | + } |
| 47 | + } |
| 48 | + path.pop() |
| 49 | + this.timer += 1 |
| 50 | + this.timeOut[v] = this.timer |
| 51 | + } |
| 52 | + |
| 53 | + isAncestor(u, v) { |
| 54 | + return ( |
| 55 | + this.timeIn[u] <= this.timeIn[v] && this.timeOut[u] >= this.timeOut[v] |
| 56 | + ) |
| 57 | + } |
| 58 | + |
| 59 | + lca(u, v) { |
| 60 | + if (this.isAncestor(u, v)) { |
| 61 | + return u |
| 62 | + } else if (this.isAncestor(v, u)) { |
| 63 | + return v |
| 64 | + } |
| 65 | + let d = this.ancestors[u].length - 1 |
| 66 | + while (d >= 0) { |
| 67 | + d = Math.min(d, this.ancestors[u].length - 1) |
| 68 | + if (!this.isAncestor(this.ancestors[u][d], v)) { |
| 69 | + u = this.ancestors[u][d] |
| 70 | + } |
| 71 | + d -= 1 |
| 72 | + } |
| 73 | + return this.ancestors[u][0] |
| 74 | + } |
| 75 | + |
| 76 | + distance(u, v) { |
| 77 | + const a = this.lca(u, v) |
| 78 | + if (a === u || a === v) { |
| 79 | + return Math.abs(this.dist[v] - this.dist[u]) |
| 80 | + } |
| 81 | + return this.dist[u] - this.dist[a] + (this.dist[v] - this.dist[a]) |
| 82 | + } |
| 83 | + |
| 84 | + findNodeDist(u, distance, mode = 0) { |
| 85 | + let d = this.ancestors[u].length - 1 |
| 86 | + let m = u |
| 87 | + while (d >= 0) { |
| 88 | + d = Math.min(d, this.ancestors[u].length - 1) |
| 89 | + const v = this.ancestors[u][d] |
| 90 | + if (this.dist[v] >= distance) { |
| 91 | + m = v |
| 92 | + u = this.ancestors[u][d] |
| 93 | + } |
| 94 | + d -= 1 |
| 95 | + } |
| 96 | + if (mode === 0 || this.dist[m] === distance) { |
| 97 | + return m |
| 98 | + } |
| 99 | + return this.ancestors[m][0] |
| 100 | + } |
| 101 | + |
| 102 | + median(u, v) { |
| 103 | + const goal = this.distance(u, v) / 2 |
| 104 | + const a = this.lca(u, v) |
| 105 | + if (u === a) { |
| 106 | + return this.findNodeDist(v, goal + this.dist[u]) |
| 107 | + } else if (v === a) { |
| 108 | + return this.findNodeDist(u, goal + this.dist[v], 1) |
| 109 | + } else { |
| 110 | + const d = this.distance(a, u) |
| 111 | + if (d >= goal) { |
| 112 | + return this.findNodeDist(u, d - goal + this.dist[a], 1) |
| 113 | + } else { |
| 114 | + return this.findNodeDist(v, goal - d + this.dist[a]) |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments