Simple DSU Solution
Intuition\nAnd is a montonous function so if there is a path between two nodes we would like to traverse as many edges as we can to decrease overall and value.\
# Intuition\nAnd is a montonous function so if there is a path between two nodes we would like to traverse as many edges as we can to decrease overall and value.\n\n# Approach\nUsing this property of AND we know that we have to cover all the paths. But how can we do it???\nThat\'s a good question.\nIDEA: Think About DSU.\nWHY??\nIt will tell us whether the nodes given in the query array belong to same component or not. If they do we will use map to fetch AND value associated with the all paths in that component. And if these node belong to different component then we can simply say that there is no path and hence -1 will be it.\n\n# Complexity\n- Time complexity:\nO(nlong)\n\n- Space complexity:\nO(N)\n\n# Code\n```\n#define N  200005\nint parent[N];\nint S[N];\n// class Dsu()\nvoid init(int n)\n{\n    memset(S,1,sizeof(S));\n    for(int i=0;i<n;i++)\n    {\n        parent[i]=i;\n    }\n}\nint Parent(int node)\n{\n    if(parent[node]==node)return node;\n    return parent[node]=Parent(parent[node]);\n}\nvoid Union(int a,int b)\n{\n    if(Parent(a)==Parent(b))return;\n    if(S[a]<S[b])swap(a,b);\n    //this will help us to decrease the depth\n    S[a]+=S[b];\n    int ul=Parent(b);\n    parent[ul]=a;\n\n}\nclass Solution {\npublic:\n    vector<int> minimumCost(int n, vector<vector<int>>& edges, vector<vector<int>>& query) {\n        // by direct traversal it can\'t be solved we have to do something clever to get the answer\n        // what can be done\n        // vector<pair<int,int>>adj[n];\n        init(n);\n        for(auto i:edges)\n        {\n           int s=i[0];int d=i[1];\n            Union(s,d);\n        }\n        //got it\n        map<int,int>mp;\n        //it store and val for all the components and then we are done\n        for(int i=0;i<edges.size();i++)\n        {\n            if(Parent(edges[i][0])==Parent(edges[i][1]))\n            {\n                int p=Parent(edges[i][0]);\n                if(mp.find(p)!=mp.end())\n                {\n                    mp[p]&=edges[i][2];\n                }\n                else mp[p]=edges[i][2];\n            }\n        }\n        vector<int>arr;\n        int c=0;\n        for(auto i:query)\n        {\n            int s=i[0];\n            int d=i[1];\n            // if(c==2)\n            // {\n            //     cout<<Parent(s)<<" "<<Parent(d)<<endl;\n            // }\n            if(Parent(s)==Parent(d))\n            arr.push_back(mp[Parent(s)]);\n            else arr.push_back(-1);\n            c++;\n        }\n        return arr;\n        \n        \n        // return arr;\n    }\n};\n```
['C++']