Python (DFS + set)
Intuition\n Describe your first thoughts on how to solve this problem. \nDFS\n# Approach\n Describe your approach to solving the problem. \nwe will use dfs to s
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nDFS\n# Approach\n<!-- Describe your approach to solving the problem. -->\nwe will use dfs to solve this question. keep track of the path in dfs. \nwe will also use a set data structure to keep only unique path of the nodes which are apples\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(ElogV)$$\nE - no. of edges\nV - no. of vertices\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(E)$$ \n# Code\n```\nfrom collections import defaultdict\nclass Solution:\n    def minTime(self, n: int, edges: List[List[int]], hasApple: List[bool]) -> int:\n        def dfs(source, visited, adj, path):\n            nonlocal unique\n            path.append(source)\n            # print(path)\n            if hasApple[source]:\n                for i in range(len(path)-1,0,-1):\n                    if (path[i-1], path[i]) not in unique:\n                        unique.add((path[i-1], path[i]))\n                    else:\n                        break\n            for nbr in adj[source]:\n                if nbr not in visited:\n                    visited.add(nbr)\n                    dfs(nbr, visited, adj, path.copy())\n        path = []\n        unique = set()\n        adj = defaultdict(list)\n        for edge in edges:\n            u, v = edge\n            adj[u] += [v]\n            adj[v] += [u]\n        visited = set()\n        visited.add(0)\n        dfs(0, visited, adj, path)\n        # print(unique)\n        return 2 * len(unique)\n\n\n```
['Python3']