Deloitte | COGNIZANT | IBM | CAPGEMINI
def find_equal_biomass_cut(input1, input2, input3):
n = input1
biomass = input2
edges = input3
graph = [[] for _ in range(n)]
for u, v in edges:
graph[u].append(v)
graph[v].append(u)
total = sum(biomass)
if total % 2 != 0:
return [-1, -1]
target = total // 2
valid_cuts = []
visited = [False] * n
def dfs(node):
visited[node] = True
subtotal = biomass[node]
for nei in graph[node]:
if not visited[nei]:
sub = dfs(nei)
subtotal += sub
if sub == target:
valid_cuts.append(sorted([node, nei]))
return subtotal
dfs(0)
if not valid_cuts:
return [-1, -1]
valid_cuts.sort()
return valid_cuts[0]
//Equal Biomass Partition✅✅
834 ·