give undirected tree consist n vertex number 0 n1 apple vertex you spend 1 second walk edge tree return minimum time second spend collect apple tree start vertex 0 come vertexthe edge undirected tree give array edge edgesi ai bi mean exist edge connect vertex ai bi additionally boolean array hasapple hasapplei true mean vertex apple apple Example 1input n 7 edge 010214152326 hasapple falsefalsetruefalsetruetruefalseoutput 8 Explanation the figure represent give tree red vertex apple one optimal path collect apple show green arrow Example 2input n 7 edge 010214152326 hasapple falsefalsetruefalsefalsetruefalseOutput 6explanation the figure represent give tree red vertex apple one optimal path collect apple show green arrow Example 3Input n 7 edge 010214152326 hasapple falsefalsefalsefalsefalsefalsefalseoutput 0 Constraints1 n 105edgeslength n 1edgesilength 20 ai bi n 1fromi toihasapplelength n