Implementation Using C#
\npublic class Solution {\n    public int MinTime(int n, int[][] edges, IList<bool> hasApple) {\n        var adj = new Dictionary<int, List<int>>();\n        fo
```\npublic class Solution {\n    public int MinTime(int n, int[][] edges, IList<bool> hasApple) {\n        var adj = new Dictionary<int, List<int>>();\n        foreach (int[] edge in edges) {\n            int a = edge[0];\n            int b = edge[1];\n            if (!adj.ContainsKey(a)) {\n                adj[a] = new List<int>();\n            }\n            if (!adj.ContainsKey(b)) {\n                adj[b] = new List<int>();\n            }            \n            adj[a].Add(b);\n            adj[b].Add(a);\n        }\n        return DFS(0, -1, adj, hasApple);        \n    }\n\n    public int DFS(int node, int parent, Dictionary<int, List<int>> adj, IList<bool> hasApple) {\n        if (!adj.ContainsKey(node)) {\n            return 0;\n        }\n\n        int totalTime = 0;\n        foreach (int child in adj[node]) {\n            if (child == parent) {\n                continue;\n            }\n            int childTime = DFS(child, node, adj, hasApple);\n            // childTime > 0 indicates subtree of child has apples. Since the root node of the\n            // subtree does not contribute to the time, even if it has an apple, we have to check it\n            // independently.\n            if (childTime > 0 || hasApple[child]) {\n                totalTime += childTime + 2;\n            }\n        }\n        return totalTime;\n    }\n}\n```
['Depth-First Search', 'C#']