Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int res = 0;
- int minTime(int n, vector<vector<int>>& edges, vector<bool>& hasApple) {
- vector<vector<int>> graph(n);
- for(auto edge: edges){
- graph[edge[0]].push_back(edge[1]);
- graph[edge[1]].push_back(edge[0]);
- }
- vector<bool> vis(n, false);
- dfs(0, graph, hasApple, vis);
- return res;
- }
- int dfs(int node, vector<vector<int>>& graph, vector<bool>& hasApple, vector<bool>& vis){
- int exists = 0;
- vis[node] = true;
- for(auto child: graph[node])
- if(!vis[child])
- exists += dfs(child, graph, hasApple, vis);
- res += exists*2;
- if(hasApple[node] || exists)
- return 1;
- return 0;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement