C++ Detailed, Clear & Beats Aprox.100%
\n\n\n# Intuition\nThe Idea is you will notice that the answer is the number of all nodes you have to visit multiplied by 2 (except for zero)!!\n\nBecause to ge
![Screenshot 2023-01-11 070807.png](https://assets.leetcode.com/users/images/ad3cd8b1-de43-4132-879a-8742f498bdb3_1673413704.0949535.png)\n\n\n# Intuition\nThe Idea is you will notice that the answer is the number of all nodes you have to visit multiplied by 2 (except for zero)!!\n\nBecause to get to any point you have to go there and then come back, 2 moves per any visited node except for zero !\n\nbecause you already start at zero, you don\'t go there then come back.\n\n# Approach\nOur goal is to determine the nodes we NEED to visit.\n\nWe check every node which has apples in it then go mark all the nodes in the path needed to go to node-zero starting at that node\n\nThe answer is all the mustVisited nodes * 2 (excluding node-zero as mentioned)\n\n# Complexity\n- Time complexity: O (n + number of paths)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n\n    int minTime(int n, vector<vector<int>>& edges, vector<bool>& hasApple) {\n\n        // we choose the least-in-number neighbour because to follow\n        // the path from some point to zero\n        // you always have to go to the least neighbour in number\n        // intilaize to max becasue we need to minimize that number\n        // with new edges added\n        vector<int> leastNeighbour(n, INT32_MAX);\n\n        // for each edge, we conclude the least-in-number neighbour\n        // for the two nodes in edge\n        for (auto & i : edges) {\n            leastNeighbour[i[0]] = min(i[1], leastNeighbour[i[0]]);\n            leastNeighbour[i[1]] = min(i[0], leastNeighbour[i[1]]);\n\n        }\n\n        // here we store the nodes we NEED to visit\n        // initialized to false so later on when we follow the path\n        // to node-zero we terminate if\n        // we find a start of a previously visited path thus...\n        // saving time for not tracing the same path twice\n        // basically... if node 3 is connected to node 2 and we know\n        // that we visited node 2 then we don\'t need to trace\n        // to node-zero , only trace the path until node 2 (all nodes\n        // from 2 to 0 were already checked before)\n        vector<bool>mustVisit(n ,false);\n\n        // we go from last to start because checking the longer paths\n        // makes it faster for us to mark other nodes\n        int j = 0;\n        for (int i = n - 1; i > 0; --i) {\n            if(hasApple[i]){\n                j = i;\n                // if it has apple then mark its path by marking the\n                // point then going to the least neighbour\n                // repeat until reach zero-node or\n                // a node we previously visited\n                while(not mustVisit[j] && j != 0){\n                    mustVisit[j] = true;\n                    j = leastNeighbour[j];\n                }\n            }\n        }\n\n        int cntr = 0;\n        for (auto i : mustVisit) {\n            cntr += int(i);\n        }\n\n        return  cntr*2;\n    }\n};\n```\n\nUpvote if you like it :)
['C++']