• + 0 comments
    def treeLength(tree, node, count):
        if tree.get(node):
            for n in tree[node]:
                count = treeLength(tree, n, count + 1)
        return count
    
    def evenForest(t_nodes, t_edges, t_from, t_to):
        graph = {}
        for i in range(t_edges):
            if t_to[i] in graph:
                graph[t_to[i]].append(t_from[i])
            else:
                graph[t_to[i]] = [t_from[i]]
        count = 0
        for e in graph:
            for n in graph[e]:
                length = treeLength(graph, n, 1)
                if not length % 2:
                    count += 1
        return count