Total Sum of Interaction Cost in Tree Groups Problem
Description
LeetCode Problem 3786.
You are given an integer n and an undirected tree with n nodes numbered from 0 to n - 1. This is represented by a 2D array edges of length n - 1, where edges[i] = [u_i, v_i] indicates an undirected edge between nodes u_i and v_i.
You are also given an integer array group of length n, where group[i] denotes the group label assigned to node i.
- Two nodes
uandvare considered part of the same group ifgroup[u] == group[v]. - The interaction cost between
uandvis defined as the number of edges on the unique path connecting them in the tree.
Return an integer denoting the sum of interaction costs over all unordered pairs (u, v) with u != v such that group[u] == group[v].
Example 1:
Input: n = 3, edges = [[0,1],[1,2]], group = [1,1,1]
Output: 4
Explanation:

All nodes belong to group 1. The interaction costs between the pairs of nodes are:
- Nodes (0, 1): 1
- Nodes (1, 2): 1
- Nodes (0, 2): 2
Thus, the total interaction cost is 1 + 1 + 2 = 4.
Example 2:
Input: n = 3, edges = [[0,1],[1,2]], group = [3,2,3]
Output: 2
Explanation:
- Nodes 0 and 2 belong to group 3. The interaction cost between this pair is 2.
- Node 1 belongs to a different group and forms no valid pair. Therefore, the total interaction cost is 2.
Example 3:
Input: n = 4, edges = [[0,1],[0,2],[0,3]], group = [1,1,4,4]
Output: 3
Explanation:

Nodes belonging to the same groups and their interaction costs are:
- Group 1: Nodes (0, 1): 1
- Group 4: Nodes (2, 3): 2
Thus, the total interaction cost is 1 + 2 = 3.
Example 4:
Input: n = 2, edges = [[0,1]], group = [9,8]
Output: 0
Explanation:
All nodes belong to different groups and there are no valid pairs. Therefore, the total interaction cost is 0.
Constraints:
1 <= n <= 10^5edges.length == n - 1edges[i] = [u_i, v_i]0 <= u_i, v_i <= n - 1group.length == n1 <= group[i] <= 20- The input is generated such that
edgesrepresents a valid tree.
Solution
Approach: Count Pairs Across Each Edge
The distance between two nodes is the number of edges on their path, so the total over all same-group pairs is the sum, over every edge, of the number of same-group pairs whose path uses that edge.
Root the tree at node 0. Cutting the edge between a node v and its parent splits the tree into the subtree of v and the rest. For a group k with total[k] nodes, of which cnt[v][k] lie in the subtree of v, exactly cnt[v][k] * (total[k] - cnt[v][k]) pairs of group k are separated by that edge. Summing this over the (at most 20) groups and over all edges gives the answer.
To get cnt[v][k] for every node, list the nodes in BFS order and process them in reverse, adding each node's counts to its parent's. This avoids deep recursion on path-shaped trees.
The answer can reach about 5 * 1014, so use 64-bit integers.
Complexity: O(20 * n) time and O(20 * n) extra space.
class Solution:
def interactionCosts(self, n: int, edges: List[List[int]], group: List[int]) -> int:
g = [[] for _ in range(n)]
for u, v in edges:
g[u].append(v)
g[v].append(u)
# BFS order from node 0, so a parent always comes before its children
parent = [-1] * n
order = [0]
for u in order:
for v in g[u]:
if v != parent[u]:
parent[v] = u
order.append(v)
total = [0] * 21
cnt = [[0] * 21 for _ in range(n)] # cnt[v][k]: nodes of group k in the subtree of v
for v in range(n):
total[group[v]] += 1
cnt[v][group[v]] = 1
ans = 0
for v in reversed(order):
if parent[v] >= 0:
c = cnt[v]
# every same-group pair separated by the edge (v, parent) crosses it once
ans += sum(c[k] * (total[k] - c[k]) for k in range(1, 21))
p = cnt[parent[v]]
for k in range(1, 21):
p[k] += c[k]
return ans