O(n.log(n^2)) Time Solution || Commented Code
Complexity
Time complexity: O(n.logn2)
Space complexity: O(n.logn2)
Code
click the upvote btn :)
# Complexity
- Time complexity: $O(n.\log n^2)$
- Space complexity: $O(n.\log n^2)$
# Code
```cpp
void dfs(int node, vector<int> &visited, vector<vector<pair<int, int>>> &graph, int &maxWeight){
    visited[node] = 1;
    for (int i = 0; i < graph[node].size(); ++i){
        if (!visited[graph[node][i].first] && graph[node][i].second <= maxWeight){ 
            dfs(graph[node][i].first, visited, graph, maxWeight);
        }
    }
}

//only checks if all nodes can be visited from 0; threshold irrelevant
bool ifPossible(int &maxWeight, vector<vector<pair<int, int>>> &graph){ irrelevent 
    vector<int> visited(graph.size(), 0);
    dfs(0, visited, graph, maxWeight);
    for (int i = 0; i < graph.size(); ++i){ if (!visited[i]){ return 0; } }
    return 1;
}

int minMaxWeight(int n, vector<vector<int>> &edges, int threshold){
    vector<vector<pair<int, int>>> graph(n); //stores graph with reversed edges
    for (int i = 0; i < edges.size(); ++i){ graph[edges[i][1]].push_back({edges[i][0], edges[i][2]}); }
    
    vector<int> weights; //stores sorted weights for binarySearch
    for (int i = 0; i < edges.size(); ++i){ weights.push_back(edges[i][2]); }
    sort(weights.begin(), weights.end());

    //lowerBound on maxWeight
    int l = 0, r = weights.size() - 1;
    if (!ifPossible(weights[r], graph)){ return -1; }
    while (l < r){
        int mid = l + (r - l)/2;

        if (ifPossible(weights[mid], graph)){ r = mid; }
        else { l = mid + 1; }
    }

    return weights[r];
}
```
>click the upvote btn :)

['Binary Search', 'Depth-First Search', 'Graph', 'C++']