/**
* Problem: LeetCode 3558 - Number of Ways to Assign Edge Weights I
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
import java.util.*;
/**
* Problem: LeetCode 3558 - Number of Ways to Assign Edge Weights I
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
class Solution {
private static final int MOD = 1_000_000_007;
public int assignEdgeWeights(int[][] edges) {
int n = edges.length + 1;
List<Integer>[] 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]);
}
// Find maximum depth from root (node 1)
int maxDepth = 0;
boolean[] vis = new boolean[n + 1];
Queue<int[]> q = new LinkedList<>();
q.offer(new int[]{1, 0});
vis[1] = true;
while (!q.isEmpty()) {
int[] cur = q.poll();
int node = cur[0];
int depth = cur[1];
maxDepth = Math.max(maxDepth, depth);
for (int nxt : graph[node]) {
if (!vis[nxt]) {
vis[nxt] = true;
q.offer(new int[]{nxt, depth + 1});
}
}
}
// Number of odd-sum assignments on a path of length maxDepth
long ans = 1;
for (int i = 1; i < maxDepth; i++) {
ans = (ans * 2) % MOD;
}
return (int) ans;
}
}
"""
Problem: LeetCode 3558 - Number of Ways to Assign Edge Weights I
Difficulty: Medium
Language: Python
Author: CodeByTushu
"""
from collections import deque
from typing import List
class Solution:
def assignEdgeWeights(self, edges: List[List[int]]) -> int:
MOD = 1_000_000_007
n = len(edges) + 1
graph = [[] for _ in range(n + 1)]
for u, v in edges:
graph[u].append(v)
graph[v].append(u)
# Find maximum depth from root (node 1)
max_depth = 0
vis = [False] * (n + 1)
vis[1] = True
q = deque([(1, 0)])
while q:
node, depth = q.popleft()
max_depth = max(max_depth, depth)
for nxt in graph[node]:
if not vis[nxt]:
vis[nxt] = True
q.append((nxt, depth + 1))
# Number of odd-sum assignments on a path of length maxDepth
# Equal to 2^(max_depth - 1) % MOD
ans = 1
for _ in range(1, max_depth):
ans = (ans * 2) % MOD
return ans
/**
* Problem: LeetCode 3558 - Number of Ways to Assign Edge Weights I
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
/**
* Problem: LeetCode 3558 - Number of Ways to Assign Edge Weights I
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
class Solution {
private:
static const int MOD = 1000000007;
public:
int assignEdgeWeights(vector<vector<int>>& edges) {
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]);
}
// Find maximum depth from root (node 1)
int maxDepth = 0;
vector<bool> vis(n + 1, false);
queue<pair<int, int>> q;
q.push({1, 0});
vis[1] = true;
while (!q.empty()) {
auto [node, depth] = q.front();
q.pop();
maxDepth = max(maxDepth, depth);
for (int nxt : graph[node]) {
if (!vis[nxt]) {
vis[nxt] = true;
q.push({nxt, depth + 1});
}
}
}
// Number of odd-sum assignments on a path of length maxDepth
long long ans = 1;
for (int i = 1; i < maxDepth; i++) {
ans = (ans * 2) % MOD;
}
return static_cast<int>(ans);
}
};
/**
* Problem: LeetCode 3558 - Number of Ways to Assign Edge Weights I
* Difficulty: Medium
* Language: JavaScript
* Author: CodeByTushu
*/
var assignEdgeWeights = function(edges) {
const MOD = 1000000007;
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);
}
// Find maximum depth from root (node 1)
let maxDepth = 0;
const vis = new Uint8Array(n + 1);
const q = [[1, 0]];
vis[1] = 1;
let head = 0;
while (head < q.length) {
const [node, depth] = q[head++];
maxDepth = Math.max(maxDepth, depth);
for (const nxt of graph[node]) {
if (!vis[nxt]) {
vis[nxt] = 1;
q.push([nxt, depth + 1]);
}
}
}
// Number of odd-sum assignments on a path of length maxDepth
let ans = 1;
for (let i = 1; i < maxDepth; i++) {
ans = (ans * 2) % MOD;
}
return ans;
};
TIME COMPLEXITY
O(N)
SPACE COMPLEXITY
O(N)
TOPICS
Breadth-First Search / Depth-First Search
Combinatorics
Math
Tree