You are given an unrooted weighted tree with n vertices representing servers numbered from 0 to n - 1, an array edges where edges[i] = [ai, bi, weighti] represents a bidirectional edge between vertices ai and bi of weight weighti. You are also given an integer signalSpeed.
Two servers a and b are connectable through a server c if:
a < b, a != c and b != c.
The distance from c to a is divisible by signalSpeed.
The distance from c to b is divisible by signalSpeed.
The path from c to b and the path from c to a do not share any edges.
Return an integer arraycountof lengthnwherecount[i]is thenumber of server pairs that are connectable throughthe serveri.
Input: edges =[[0,1,1],[1,2,5],[2,3,13],[3,4,9],[4,5,2]], signalSpeed =1Output: [0,4,6,6,4,0]Explanation: Since signalSpeed is1, count[c]is equal to the number of pairs of paths that start at c and do not share any edges.In the case of the given path graph, count[c]is equal to the number of servers to the left of c multiplied by the servers to the right of c.
Input: edges =[[0,6,3],[6,5,3],[0,3,1],[3,2,7],[3,1,6],[3,4,2]], signalSpeed =3Output: [2,0,0,0,0,0,2]Explanation: Through server 0, there are 2 pairs of connectable servers:(4,5) and (4,6).Through server 6, there are 2 pairs of connectable servers:(4,5) and (0,5).It can be shown that no two servers are connectable through servers other than 0 and 6.
For each server c, the connectable pairs (a, b) must be in different subtrees of c, and the path from c to a and c to b must each have length divisible by signalSpeed. We can use DFS to count, for each subtree, the number of nodes at each distance mod signalSpeed, and then count valid pairs by combining counts from different subtrees.
classSolution {
publicint[]countPairsOfConnectableServers(int n, int[][] edges, int signalSpeed) {
List<int[]>[] g =new List[n];
for (int i = 0; i < n; i++) g[i]=new ArrayList<>();
for (int[] e : edges) {
g[e[0]].add(newint[]{e[1], e[2]});
g[e[1]].add(newint[]{e[0], e[2]});
}
int[] ans =newint[n];
for (int c = 0; c < n; c++) {
List<Integer> counts =new ArrayList<>();
for (int[] vw : g[c]) {
counts.add(dfs(g, vw[0], c, vw[1], signalSpeed));
}
int total = 0;
for (int i = 0; i < counts.size(); i++) {
for (int j = i+1; j < counts.size(); j++) {
total += counts.get(i) * counts.get(j);
}
}
ans[c]= total;
}
return ans;
}
privateintdfs(List<int[]>[] g, int u, int p, int d, int signalSpeed) {
int cnt = d % signalSpeed == 0 ? 1 : 0;
for (int[] vw : g[u]) {
if (vw[0]!= p) cnt += dfs(g, vw[0], u, d + vw[1], signalSpeed);
}
return cnt;
}
}
classSolution {
funcountPairsOfConnectableServers(n: Int, edges: Array<IntArray>, signalSpeed: Int): IntArray {
val g = Array(n) { mutableListOf<Pair<Int, Int>>() }
for (e in edges) {
g[e[0]].add(e[1] to e[2])
g[e[1]].add(e[0] to e[2])
}
val ans = IntArray(n)
fundfs(u: Int, p: Int, d: Int): Int {
var cnt = if (d % signalSpeed ==0) 1else0for ((v, w) in g[u]) if (v != p) cnt += dfs(v, u, d + w)
return cnt
}
for (c in0 until n) {
val counts = mutableListOf<Int>()
for ((v, w) in g[c]) counts.add(dfs(v, c, w))
var total = 0for (i in counts.indices) for (j in i+1 until counts.size) total += counts[i] * counts[j]
ans[c] = total
}
return ans
}
}
classSolution:
defcountPairsOfConnectableServers(self, n: int, edges: list[list[int]], signalSpeed: int) -> list[int]:
from collections import defaultdict
g = [[] for _ in range(n)]
for a, b, w in edges:
g[a].append((b, w))
g[b].append((a, w))
defdfs(u, p, d):
cnt =1if d % signalSpeed ==0else0for v, w in g[u]:
if v != p:
cnt += dfs(v, u, d + w)
return cnt
ans = [0] * n
for c in range(n):
counts = [dfs(v, c, w) for v, w in g[c]]
total =0for i in range(len(counts)):
for j in range(i+1, len(counts)):
total += counts[i] * counts[j]
ans[c] = total
return ans