#3786

Total Sum of Interaction Cost in Tree Groups

candidate master · 1345 · lc hard +32 · 53.6% accepted · 68 likes · top 45%

Description

Given an undirected tree of n nodes (0 to n-1) with edges edges[i] = [ui, vi] and a group label array group, define the interaction cost of two same-group nodes as the number of edges on their unique tree path, and return the total interaction cost summed over all unordered pairs (u, v) with u != v and group[u] == group[v].

Code

1
2
3