C# Solution for Minimum Cost Walk In Weighted Graph Problem
IntuitionThe problem requires finding the minimum cost walk between nodes in an undirected weighted graph where the cost is defined as the bitwise AND of edge w
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem requires finding the minimum cost walk between nodes in an undirected weighted graph where the cost is defined as the bitwise AND of edge weights along the walk.
To efficiently answer multiple queries, we need to precompute and store information that allows us to answer queries in constant time O(1).

# Approach
<!-- Describe your approach to solving the problem. -->
Step 1: Graph Representation (Adjacency List)
	•	We represent the given graph using an adjacency list (Dictionary<int, List<(int, int)>>), where each node stores a list of (neighbor, weight) pairs.

Step 2: Identify Connected Components using BFS
	•	Since the graph is undirected, we first find connected components.
	•	We assign a component ID to each node using BFS and store the result in an array component[], where component[i] represents the component ID of node i.
	•	Each connected component is processed separately.

Step 3: Precompute Minimum AND-Cost for Each Component
	•	Within each connected component, we compute the minimum AND-cost between every pair of nodes.
	•	We use BFS from every node inside a component to compute the minimum bitwise AND cost between nodes.
	•	These values are stored in a dictionary componentMinAnd[compId], allowing quick lookup.

Step 4: Answer Queries in O(1)
	•	For each query [si, ti]:
	1.	If si and ti belong to different components, return -1.
	2.	Otherwise, return the precomputed minimum AND-cost.

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
Let:
N = number of nodes (≤ 10^5)
E = number of edges (≤ 10^5)
Q = number of queries (≤ 10^5)

Graph Construction - O(E) - Adjacency list creation.
Finding Connected Components (BFS) - O(N + E) - Every node and edge is visited once.
Multi-Source BFS for AND Computation - O(N + E) - Similar to shortest path BFS traversal.
Answering Queries - O(Q) - Each query is resolved in O(1).
Total Complexity - O(N + E + Q) - Efficient for large inputs.

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

Graph Representation - O(N + E) - Adjacency list.
Component Mapping - O(N) - Stores which component each node belongs to.
AND-Cost Storage - O(N) - Minimum AND-costs for each component.
Queue for BFS - O(N) - Worst case if all nodes are processed.
Total Complexity - O(N + E) - Efficient for large inputs.

# Code
```csharp []
public class Solution {
    public int[] MinimumCost(int n, int[][] edges, int[][] query) {
        var graph = new Dictionary<int, List<(int, int)>>();
        for (int i = 0; i < n; i++) 
            graph[i] = new List<(int, int)>();
        
        foreach (var edge in edges) {
            int u = edge[0], v = edge[1], w = edge[2];
            graph[u].Add((v, w));
            graph[v].Add((u, w));
        }

        int[] component = new int[n];  
        Array.Fill(component, -1);
        var componentMinAnd = new List<Dictionary<int, int>>(); 
        int compId = 0;

        for (int i = 0; i < n; i++) {
            if (component[i] == -1) {
                var minAndCost = new Dictionary<int, int>();
                BFSComputeAnd(graph, i, component, compId, minAndCost);
                componentMinAnd.Add(minAndCost);
                compId++;
            }
        }
        int[] result = new int[query.Length];
        for (int i = 0; i < query.Length; i++) {
            int s = query[i][0], t = query[i][1];
            if (component[s] != component[t]) {
                result[i] = -1;
            } else {
                result[i] = componentMinAnd[component[s]].GetValueOrDefault(t, -1);
            }
        }
        return result;
    }
    private void BFSComputeAnd(Dictionary<int, List<(int, int)>> graph, int start, int[] component, int compId, Dictionary<int, int> minAndCost) {
        Queue<(int, int)> queue = new Queue<(int, int)>();
        queue.Enqueue((start, -1));
        component[start] = compId;
        minAndCost[start] = int.MaxValue;

        while (queue.Count > 0) {
            var (node, andValue) = queue.Dequeue();

            foreach (var (next, weight) in graph[node]) {
                int newAnd = (andValue == -1) ? weight : (andValue & weight);

                if (!minAndCost.ContainsKey(next) || newAnd < minAndCost[next]) {
                    minAndCost[next] = newAnd;
                    component[next] = compId;
                    queue.Enqueue((next, newAnd));
                }
            }
        }
    }
}
```
['C#']