Easy Beginners Friendly Solution BFS Connected Components
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\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```\nclass Solution {\npublic:\n\n   \n    unordered_map<int,int>mp;\n\n    void bfs(vector<pair<int,int>> adj[],vector<int>&v,int id,int index,vector<int>&vis){\n        vis[index]=1;\n        mp[index]=id;\n\n        queue<int>q;\n        q.push(index);\n       \n\n        while(!q.empty()){\n            int node=q.front();\n            q.pop();\n            vis[node]=1;\n            mp[node]=id;\n\n            for(auto it:adj[node]){\n                int x=it.first;\n                if(vis[x]!=1){\n                    v.push_back(it.second);\n                    q.push(x);\n                }\n            }\n        }\n\n\n    }\n   \n    vector<int> minimumCost(int n, vector<vector<int>>& edges, vector<vector<int>>& query) {\n        \n        map<int,vector<int>>ele;\n        int id=0;\n\n         vector<pair<int,int>>adj[n];\n\n        for(auto it:edges){\n            int a=it[0];\n            int b=it[1];\n            int c=it[2];\n\n            adj[a].push_back({b,c});\n            adj[b].push_back({a,c});\n        }\n\n        vector<int>vis(n,-1);\n\n        for(int i=0;i<n;i++){\n            if(vis[i]==-1){\n                //cout<<"index is"<<" "<<i<<endl;\n                vector<int>v;\n                bfs(adj,v,id,i,vis);\n                //cout<<v.size()<<endl;\n                ele[id]=v;\n                id++;\n            }\n        }\n\n       // cout<<ele.size()<<endl;\n\n       map<int,int>andd;\n\n       for(int i=0;i<id;i++){\n        vector<int>temp=ele[i];\n        if(temp.size()>0){\n            int alpa=temp[0];\n            for(int i=1;i<temp.size();i++){\n                alpa=alpa & temp[i];\n            }\n            andd[i]=alpa;\n        }\n       }\n\n       vector<int>ans;\n\n       for(int i=0;i<query.size();i++){\n        int a=query[i][0];\n        int b=query[i][1];\n\n        if(mp[a]==mp[b]){\n            int beta=andd[mp[a]];\n            ans.push_back(beta);\n        }else{\n            ans.push_back(-1);\n        }\n       }\n       return ans;\n    }\n};\n```
['C++']