You are viewing a single comment's thread. Return to all comments →
Modified DFS with 100% score (same time and space complexity as standard DFS)
def dfs(node, visited, adj_list): visited[node] = True k = 0 cuts = 0 for neigh in adj_list[node]: if not visited[neigh]: count, child_cuts = dfs(neigh, visited, adj_list) cuts += child_cuts if count % 2 == 0: cuts += 1 else: k += count return (k + 1, cuts) def evenForest(t_nodes, t_edges, t_from, t_to): adj_list = [[] for _ in range(t_nodes + 1)] for i in range(t_edges): adj_list[t_from[i]].append(t_to[i]) adj_list[t_to[i]].append(t_from[i]) visited = [False] * (t_nodes + 1) count, cuts = dfs(1, visited, adj_list) return cuts
`
Seems like cookies are disabled on this browser, please enable them to open this website
Even Tree
You are viewing a single comment's thread. Return to all comments →
Modified DFS with 100% score (same time and space complexity as standard DFS)
`