/**
* Problem: LeetCode 3559 - Number of Ways to Assign Edge Weights II
* Difficulty: Hard
* Language: Java
* Author: CodeByTushu
*/
import java.util.*;
/**
* Problem: LeetCode 3559 - Number of Ways to Assign Edge Weights II
* Difficulty: Hard
* Language: Java
* Author: CodeByTushu
*/
class Solution {
static final int MOD = 1_000_000_007;
static final int LOG = 17;
List<Integer>[] graph;
int[][] up;
int[] depth;
public int[] assignEdgeWeights(int[][] edges, int[][] queries) {
int n = edges.length + 1;
graph = new ArrayList[n + 1];
for (int i = 1; i <= n; i++) {
graph[i] = new ArrayList<>();
}
for (int[] e : edges) {
graph[e[0]].add(e[1]);
graph[e[1]].add(e[0]);
}
up = new int[n + 1][LOG + 1];
depth = new int[n + 1];
dfs(1, 0);
for (int j = 1; j <= LOG; j++) {
for (int i = 1; i <= n; i++) {
up[i][j] = up[up[i][j - 1]][j - 1];
}
}
int maxDepth = n;
long[] pow2 = new long[maxDepth + 1];
pow2[0] = 1;
for (int i = 1; i <= maxDepth; i++) {
pow2[i] = (pow2[i - 1] * 2) % MOD;
}
int[] ans = new int[queries.length];
for (int i = 0; i < queries.length; i++) {
int u = queries[i][0];
int v = queries[i][1];
int lca = getLCA(u, v);
int len = depth[u] + depth[v] - 2 * depth[lca];
if (len == 0) {
ans[i] = 0;
} else {
ans[i] = (int) pow2[len - 1];
}
}
return ans;
}
private void dfs(int node, int parent) {
up[node][0] = parent;
for (int nxt : graph[node]) {
if (nxt == parent) continue;
depth[nxt] = depth[node] + 1;
dfs(nxt, node);
}
}
private int getLCA(int u, int v) {
if (depth[u] < depth[v]) {
int temp = u;
u = v;
v = temp;
}
int diff = depth[u] - depth[v];
for (int j = LOG; j >= 0; j--) {
if (((diff >> j) & 1) == 1) {
u = up[u][j];
}
}
if (u == v) return u;
for (int j = LOG; j >= 0; j--) {
if (up[u][j] != up[v][j]) {
u = up[u][j];
v = up[v][j];
}
}
return up[u][0];
}
}
"""
Problem: LeetCode 3559 - Number of Ways to Assign Edge Weights II
Difficulty: Hard
Language: Python
Author: CodeByTushu
"""
from typing import List
class Solution:
def assignEdgeWeights(self, edges: List[List[int]], queries: List[List[int]]) -> List[int]:
MOD = 1_000_000_007
LOG = 17
n = len(edges) + 1
graph = [[] for _ in range(n + 1)]
for u, v in edges:
graph[u].append(v)
graph[v].append(u)
up = [[0] * (LOG + 1) for _ in range(n + 1)]
depth = [0] * (n + 1)
# Iterative BFS to prevent recursion depth exceeded on deep trees
queue = [1]
visited = [False] * (n + 1)
visited[1] = True
head = 0
while head < len(queue):
node = queue[head]
head += 1
for nxt in graph[node]:
if not visited[nxt]:
visited[nxt] = True
depth[nxt] = depth[node] + 1
up[nxt][0] = node
queue.append(nxt)
for j in range(1, LOG + 1):
for i in range(1, n + 1):
up[i][j] = up[up[i][j - 1]][j - 1]
pow2 = [1] * (n + 1)
for i in range(1, n + 1):
pow2[i] = (pow2[i - 1] * 2) % MOD
def get_lca(u: int, v: int) -> int:
if depth[u] < depth[v]:
u, v = v, u
diff = depth[u] - depth[v]
for j in range(LOG, -1, -1):
if (diff >> j) & 1:
u = up[u][j]
if u == v:
return u
for j in range(LOG, -1, -1):
if up[u][j] != up[v][j]:
u = up[u][j]
v = up[v][j]
return up[u][0]
ans = []
for u, v in queries:
lca = get_lca(u, v)
length = depth[u] + depth[v] - 2 * depth[lca]
if length == 0:
ans.append(0)
else:
ans.append(pow2[length - 1])
return ans
/**
* Problem: LeetCode 3559 - Number of Ways to Assign Edge Weights II
* Difficulty: Hard
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
#include <algorithm>
using namespace std;
/**
* Problem: LeetCode 3559 - Number of Ways to Assign Edge Weights II
* Difficulty: Hard
* Language: C++
* Author: CodeByTushu
*/
class Solution {
static const int MOD = 1000000007;
static const int LOG = 17;
public:
vector<int> assignEdgeWeights(vector<vector<int>>& edges, vector<vector<int>>& queries) {
int n = edges.size() + 1;
vector<vector<int>> graph(n + 1);
for (const auto& e : edges) {
graph[e[0]].push_back(e[1]);
graph[e[1]].push_back(e[0]);
}
vector<vector<int>> up(n + 1, vector<int>(LOG + 1, 0));
vector<int> depth(n + 1, 0);
auto dfs = [&](auto& self, int node, int parent) -> void {
up[node][0] = parent;
for (int nxt : graph[node]) {
if (nxt == parent) continue;
depth[nxt] = depth[node] + 1;
self(self, nxt, node);
}
};
dfs(dfs, 1, 0);
for (int j = 1; j <= LOG; j++) {
for (int i = 1; i <= n; i++) {
up[i][j] = up[up[i][j - 1]][j - 1];
}
}
vector<long long> pow2(n + 1, 1);
for (int i = 1; i <= n; i++) {
pow2[i] = (pow2[i - 1] * 2) % MOD;
}
auto getLCA = [&](int u, int v) -> int {
if (depth[u] < depth[v]) {
swap(u, v);
}
int diff = depth[u] - depth[v];
for (int j = LOG; j >= 0; j--) {
if ((diff >> j) & 1) {
u = up[u][j];
}
}
if (u == v) return u;
for (int j = LOG; j >= 0; j--) {
if (up[u][j] != up[v][j]) {
u = up[u][j];
v = up[v][j];
}
}
return up[u][0];
};
int q = queries.size();
vector<int> ans(q);
for (int i = 0; i < q; i++) {
int u = queries[i][0];
int v = queries[i][1];
int lca = getLCA(u, v);
int len = depth[u] + depth[v] - 2 * depth[lca];
if (len == 0) {
ans[i] = 0;
} else {
ans[i] = pow2[len - 1];
}
}
return ans;
}
};
/**
* Problem: LeetCode 3559 - Number of Ways to Assign Edge Weights II
* Difficulty: Hard
* Language: JavaScript
* Author: CodeByTushu
*/
var assignEdgeWeights = function(edges, queries) {
const MOD = 1000000007;
const LOG = 17;
const n = edges.length + 1;
const graph = Array.from({ length: n + 1 }, () => []);
for (const [u, v] of edges) {
graph[u].push(v);
graph[v].push(u);
}
const up = Array.from({ length: n + 1 }, () => new Int32Array(LOG + 1));
const depth = new Int32Array(n + 1);
// Iterative BFS to prevent call stack overflow on deep trees
const queue = [1];
const visited = new Uint8Array(n + 1);
visited[1] = 1;
let head = 0;
while (head < queue.length) {
const node = queue[head++];
for (const nxt of graph[node]) {
if (!visited[nxt]) {
visited[nxt] = 1;
depth[nxt] = depth[node] + 1;
up[nxt][0] = node;
queue.push(nxt);
}
}
}
for (let j = 1; j <= LOG; j++) {
for (let i = 1; i <= n; i++) {
up[i][j] = up[up[i][j - 1]][j - 1];
}
}
const pow2 = new Int32Array(n + 1);
pow2[0] = 1;
for (let i = 1; i <= n; i++) {
pow2[i] = (pow2[i - 1] * 2) % MOD;
}
const getLCA = (u, v) => {
if (depth[u] < depth[v]) {
let temp = u;
u = v;
v = temp;
}
const diff = depth[u] - depth[v];
for (let j = LOG; j >= 0; j--) {
if ((diff >> j) & 1) {
u = up[u][j];
}
}
if (u === v) return u;
for (let j = LOG; j >= 0; j--) {
if (up[u][j] !== up[v][j]) {
u = up[u][j];
v = up[v][j];
}
}
return up[u][0];
};
const ans = new Array(queries.length);
for (let i = 0; i < queries.length; i++) {
const u = queries[i][0];
const v = queries[i][1];
const lca = getLCA(u, v);
const len = depth[u] + depth[v] - 2 * depth[lca];
if (len === 0) {
ans[i] = 0;
} else {
ans[i] = pow2[len - 1];
}
}
return ans;
};
TIME COMPLEXITY
O((N + Q) log N)
SPACE COMPLEXITY
O(N log N + Q)
TOPICS
Binary Lifting
Breadth-First Search / Depth-First Search
Combinatorics
Math
Tree
Tree / Binary Tree