Simple Solution
Code
# Code
```cpp []
class Solution {
public:
    // Perform DFS to mark reachable nodes under the given weight constraint
    void dfs(int node, vector<vector<pair<int, int>>>& adj, vector<bool>& done, int maxValue) {
        done[node] = true; // Mark the current node as visited
        for (auto& x : adj[node]) {
            // Traverse only edges with weight <= maxValue and unvisited nodes
            if (x.second <= maxValue && !done[x.first]) {
                dfs(x.first, adj, done, maxValue);
            }
        }
    }

    // Check if the graph is impossible to connect under any circumstances
    bool checkForImpossible(int n, vector<vector<pair<int, int>>>& adj) {
        vector<bool> done(n, false); // Track visited nodes
        dfs(0, adj, done, 1e6);      // Perform DFS with the maximum possible weight
        for (int i = 0; i < n; i++) {
            if (!done[i]) return true; // If any node is not reachable, return true
        }
        return false; // All nodes are reachable
    }

    // Main function to find the minimum maximum weight
    int minMaxWeight(int n, vector<vector<int>>& edges, int threshold) {
        // Step 1: Build adjacency list for the graph
        vector<vector<pair<int, int>>> adj(n);
        for (auto& edge : edges) {
            adj[edge[1]].push_back({edge[0], edge[2]});
        }

        // Step 2: Check if the graph is impossible to connect
        if (checkForImpossible(n, adj)) return -1;

        // Step 3: Binary search over weight range [1, 1e6]
        int low = 1, high = 1e6, result = -1;
        while (low <= high) {
            int mid = low + (high - low) / 2; // Calculate mid-point
            vector<bool> done(n, false);      // Reset visited nodes for each iteration
            dfs(0, adj, done, mid);           // Perform DFS with the current max weight
            bool reachable = true;

            // Check if all nodes are reachable
            for (int i = 0; i < n; i++) {
                if (!done[i]) {
                    reachable = false;
                    break;
                }
            }

            // If reachable, update result and search for a smaller max weight
            if (reachable) {
                result = mid;
                high = mid - 1;
            } else {
                low = mid + 1; // Otherwise, search for a larger max weight
            }
        }

        return result; // Return the minimum maximum weight
    }
};


```
['C++']