Untitled

 avatar
unknown
plain_text
4 years ago
851 B
7
Indexable
class Solution {
public:
    unordered_map<int,vector<int>>adj;
    int dfs(int node,vector<bool>& hasApple,vector<int>&vis){
        if(vis[node])
            return 0;
        int result=0;
        vis[node]=1;
        for(int i=0;i<adj[node].size();i++){
            result+=dfs(adj[node][i],hasApple,vis);
        }
        if(result==0&&!hasApple[node]){
            return 0;
        }
        
        if(node==0)
            return result;
        return result+2;
    }
    int minTime(int n, vector<vector<int>>& edges, vector<bool>& hasApple) {
        
        vector<int>vis(n,0);
        for(int i=0;i<edges.size();i++){
            adj[edges[i][0]].push_back(edges[i][1]);
            adj[edges[i][1]].push_back(edges[i][0]);
        }
        /*
        0->1,2
        1->4,5
          */  
        return dfs(0,hasApple,vis);
    }
};
Editor is loading...