• + 0 comments

    Just another Python solution:

    from functools import reduce
    from operator import xor
    
    def deforestation(n, tree):
        root = build_tree(n,tree)
        return 'Alice' if grundy(root) else 'Bob'
    
    def build_tree(n,edges):
        neighbours = [set() for _ in range(n+1)]
        for u,v in edges:
            neighbours[u].add(v)
            neighbours[v].add(u)
        def get_branch(parent,me):
            return [get_branch(me,c) for c in neighbours[me] if c !=parent]
        return [get_branch(1,c) for c in neighbours[1]]
        
    def grundy(family):
        return reduce(xor, (1+grundy(branch) for branch in family),0)