C++ Solution
Code\n\nclass Solution {\npublic:\n    vector<int> g[100005];\n    vector<bool> hasApples;\n\n    int dfs(int u, int par) {\n        int totalTime = 0, childTim
# Code\n```\nclass Solution {\npublic:\n    vector<int> g[100005];\n    vector<bool> hasApples;\n\n    int dfs(int u, int par) {\n        int totalTime = 0, childTime = 0;\n        for(auto v: g[u]) {\n            if(v == par) continue;\n            childTime = dfs(v, u);\n            if(childTime or hasApples[v]) totalTime += childTime + 2;\n        }\n        return totalTime;\n    }\n\n    int minTime(int n, vector<vector<int>>& edges, vector<bool>& hasApple) {\n        hasApples = hasApple;\n        for(auto x: edges) {\n            g[x[0]].push_back(x[1]);\n            g[x[1]].push_back(x[0]);\n        }\n        int ans = dfs(0, -1);\n        return ans;\n    }\n};\n```
['C++']