Approach
Breadth-first search
For Total Sum of Interaction Cost in Tree Groups, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.
- Model each valid configuration as a state and each legal move as an edge.
- Seed the queue with the starting state and mark it immediately.
- Expand each state once, recording distance or reachability for unseen neighbours.
Code notes
- 133 lines of Python from the credited upstream file total-sum-of-interaction-cost-in-tree-groups.py.
- The implementation visibly relies on sequence storage, hash lookup.
- No explicit loop blocks detected, together with recursive traversal.
Complexity
Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
123 45class Solution(object):6 def interactionCosts(self, n, edges, group):7 """8 :type n: int9 :type edges: List[List[int]]10 :type group: List[int]11 :rtype: int12 """13 def bfs():14 order, parent = [0], [-1]*len(adj)15 for u in order:16 for v in adj[u]:17 if v == parent[u]:18 continue19 parent[v] = u20 order.append(v)21 return order, parent22 23 adj = [[] for _ in xrange(n)]24 for u, v in edges:25 adj[u].append(v)26 adj[v].append(u)27 mx = max(group)28 total = [0]*mx29 for x in group:30 total[x-1] += 131 result = 032 order, parent = bfs()33 cnt = [[0]*mx for _ in xrange(n)]34 for u in reversed(order):35 cnt[u][group[u]-1] += 136 for v in adj[u]:37 if u != parent[v]:38 continue39 for k in xrange(len(cnt[v])):40 result += cnt[v][k]*(total[k]-cnt[v][k])41 cnt[u][k] += cnt[v][k]42 return result43 44 454647import collections48 49 5051class Solution2(object):52 def interactionCosts(self, n, edges, group):53 """54 :type n: int55 :type edges: List[List[int]]56 :type group: List[int]57 :rtype: int58 """59 def bfs():60 order, parent = [0], [-1]*len(adj)61 for u in order:62 for v in adj[u]:63 if v == parent[u]:64 continue65 parent[v] = u66 order.append(v)67 return order, parent68 69 adj = [[] for _ in xrange(n)]70 for u, v in edges:71 adj[u].append(v)72 adj[v].append(u)73 total = collections.defaultdict(int)74 for x in group:75 total[x] += 176 result = 077 order, parent = bfs()78 cnt = [collections.defaultdict(int) for _ in xrange(n)]79 for u in reversed(order):80 cnt[u][group[u]] += 181 for v in adj[u]:82 if u != parent[v]:83 continue84 for k, c in cnt[v].iteritems():85 result += c*(total[k]-c)86 if len(cnt[v]) > len(cnt[u]):87 cnt[u], cnt[v] = cnt[v], cnt[u]88 for k, c in cnt[v].iteritems():89 cnt[u][k] += c90 cnt[v].clear()91 return result92 93 949596import collections97 98 99100class Solution3(object):101 def interactionCosts(self, n, edges, group):102 """103 :type n: int104 :type edges: List[List[int]]105 :type group: List[int]106 :rtype: int107 """108 def dfs(u, p):109 cnt = collections.defaultdict(int)110 cnt[group[u]] += 1111 for v in adj[u]:112 if v == p:113 continue114 new_cnt = dfs(v, u)115 for k, c in new_cnt.iteritems():116 result[0] += c*(total[k]-c)117 if len(new_cnt) > len(cnt):118 cnt, new_cnt = new_cnt, cnt119 for k, c in new_cnt.iteritems():120 cnt[k] += c121 return cnt122 123 adj = [[] for _ in xrange(n)]124 for u, v in edges:125 adj[u].append(v)126 adj[v].append(u)127 total = collections.defaultdict(int)128 for x in group:129 total[x] += 1130 result = [0]131 dfs(0, -1)132 return result[0]133