Easy Solution || dfs
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nStore three maximum and two minimum value for each node.Update node maxim
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nStore three maximum and two minimum value for each node.Update node maximum and minimum accroding to its child max and min.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n#include <iostream>\n#include <vector>\n#include <unordered_map>\n#include <algorithm>\n#include <limits.h>\n\nusing namespace std;\n\nclass Solution {\npublic:\n    #define ll long long\n\n    vector<ll> placedCoins(vector<vector<int>>& e, vector<int>& cost) {\n        unordered_map<int, vector<int>> mp;\n        unordered_map<int, pair<int, vector<int>>> node;\n        int n = cost.size();\n\n        // Building adjacency list from edges\n        for (auto edg : e) {\n            int u = edg[0];\n            int v = edg[1];\n            mp[u].push_back(v);\n            mp[v].push_back(u);\n        }\n\n        vector<int> vis(n, false);\n\n        function<pair<int, vector<int>>(int)> dfs = [&](int src) -> pair<int, vector<int>> {\n            vis[src] = true;\n\n            vector<int> mx = {cost[src], INT_MIN, INT_MIN};\n            vector<int> mn = {cost[src], INT_MAX, INT_MAX};\n            int subtree_node=1;\n            for (auto i : mp[src]) {\n                if (!vis[i]) {\n                    pair<int, vector<int>> p = dfs(i);\n                    subtree_node+=p.first;\n\n                    vector<int> child_max = {p.second[0], p.second[1], p.second[2]};\n                    vector<int> child_min = {p.second[3], p.second[4], p.second[5]};\n\n                    mx.insert(mx.end(), child_max.begin(), child_max.end());\n                    mn.insert(mn.end(), child_min.begin(), child_min.end());\n\n                    sort(mx.begin(), mx.end(), greater<int>());\n                    sort(mn.begin(), mn.end());\n\n                    mx.resize(3);\n                    mn.resize(3);\n                }\n            }\n            node[src] = {subtree_node, {mx[0], mx[1], mx[2], mn[0], mn[1], mn[2]}};\n\n            // cout<<"src="<<src<<" N="<<node[src].first<<" => ";\n            // for(auto ele:node[src].second)\n            // cout<<ele<<" ";\n            // cout<<endl;\n            return node[src];\n        };\n       for(int i=0;i<n;i++){\n           if(!vis[i])\n           dfs(i);\n       }\n        \n\n\n        vector<ll> result(n,0);\n\n        for (int i = 0; i < n; ++i) {\n            vector<int> values = node[i].second;\n            if(node[i].first<3){\n               result[i]=1;\n               continue;\n            }\n           \n            ll mx,mx1,mx2;\n            mx1=(ll)values[0]*(ll)values[1]*(ll)values[2];\n            mx2=(ll)values[3]*(ll)values[4]*(ll)max(values[0],values[5]);\n            mx=max(mx1,mx2);\n            \n            if(mx<0)\n            result[i]=0;\n            else\n            result[i]=mx;\n        }\n\n        return result;\n\n    }\n};\n\n\n```
['C++']