Easy  Dfs with map soln in c++
Intuition\nFirst we will find that how many components are there and which node belongs to which component because the minimum cost of travelling from one node 
# Intuition\nFirst we will find that how many components are there and which node belongs to which component because the minimum cost of travelling from one node to another node in component is the bitwise & of all edges in components . If two nodes are not in same component then ans will be -1\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nSo first we will apply dfs , with the help of map , we will store which node belongs to which component.\nAfter that we will Calculate bitwise & of all edges in one component.\nFor that we will traverse all edges , and wherever the node belongs to component we will do Bitwise & on that component.\n\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:O(V + E)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(V*E)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    \n    \n    \n    void dfs(int node,int parent,vector<vector<int>> &adj,vector<int> &vis,int flag,unordered_map<int,int> &mp){\n        vis[node]=1;\n        mp[node]=flag;\n        for (auto it:adj[node]){\n            if(it == parent || vis[it]){\n                continue;\n            }\n            dfs(it,node,adj,vis,flag,mp);\n        }\n    }\n    \n    vector<int> minimumCost(int n, vector<vector<int>>& edges, vector<vector<int>>& query) {\n        int z=edges.size();\n        vector<vector<int>> adj(n,vector<int>(0));\n        for (int i=0;i<z;i++){\n            adj[edges[i][0]].push_back(edges[i][1]);\n            adj[edges[i][1]].push_back(edges[i][0]);\n        }\n        unordered_map<int,int> mp;\n        int flag=1;\n        vector<int> vis(n,0);\n        for(int i=0;i<n;i++){\n            if(!vis[i]){\n                dfs(i,-1,adj,vis,flag,mp);\n                flag++;\n            }\n        }\n        // for(auto it:mp){\n        //     cout<<it.first<<" "<<it.second<<endl;\n        // }\n        vector<int> mp2(flag+1,pow(2,31)-1);\n        for(int i=0;i<z;i++){\n            mp2[mp[edges[i][0]]]= mp2[mp[edges[i][0]]] & edges[i][2];\n        }\n        for(auto it:mp2){\n            cout<<it<<" ";\n        }\n        cout<<endl;\n        vector<int> ans;\n        int op=query.size();\n        for(int i=0;i<op;i++){\n            if(query[i][0]==query[i][1]){\n                ans.push_back(0);\n            }\n            else if(mp[query[i][0]]==mp[query[i][1]]){\n                ans.push_back(mp2[mp[query[i][0]]]);\n            }\n            else{\n                ans.push_back(-1);\n            }\n        }\n        return ans;\n        \n    }\n};\n```
['C++']