solution using dsu and heap
IntuitionApproachComplexity

Time complexity:
O(n)


Space complexity:
O(n)

Code
# Intuition


# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
   O(n)

- Space complexity:
  O(n)
# Code
```cpp []
class dsu {
private:
    vector<int> rank, parent, size;

public:
    dsu(int n) {
        rank.resize(n + 1, 0);
        parent.resize(n + 1, 0);
        iota(parent.begin(), parent.end(), 0);
        size.resize(n + 1, 0);
    };
    int findpar(int u) {

        if (u == parent[u])
            return u;
        else
            return parent[u] = findpar(parent[u]);
    }
    void unionbysize(int u, int v) {

        int ulp_u = findpar(u);
        int ulp_v = findpar(v);
        if (ulp_u == ulp_v)
            return;
        if (size[ulp_u] < size[ulp_v]) {
            parent[ulp_u] = ulp_v;
            size[ulp_v] += size[ulp_u];
        } else {
            parent[ulp_v] = ulp_u;
            size[ulp_u] += size[ulp_v];
        }
    }
};

class Solution {
public:
    int minMaxWeight(int n, vector<vector<int>>& edges, int threshold) {
        // dsu
        dsu ds(n);
        vector graph(n, vector<pair<int,int>>());
        // reverse the graph
        for (auto& p : edges) {
            int u = p[0], v = p[1], w = p[2];
            graph[v].push_back(make_pair(w, u));
        }
        // sort according to weight 
        for (int i = 0; i < n; ++i)
            sort(graph[i].begin(), graph[i].end());
        // in -> indegree
        vector in(n, 0);
        // make a min heap
        priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>,
                       greater<tuple<int, int, int>>>
            pq;
        // tuple -> [weight, parent, child]
        vector<int> vis(n, 0);

        int ans = 0;
        for (auto& [w, v]: graph[0])
            pq.push(make_tuple(w, 0, v));
        while (pq.size()) {
            auto [ww, node, v] = pq.top();
            pq.pop();

            if (in[v] + 1 <= threshold && ds.findpar(v) != ds.findpar(node)) {
                ds.unionbysize(node, v);
                ans = max(ans, ww);
                in[v]++;
                vis[v] = 1;
                vis[node] = 1;
                for (auto& e : graph[v])
                    pq.push(make_tuple(e.first, v, e.second));
            }
        }
    
    for (int i = 0; i < n; ++i)
        if (vis[i] == 0)
            return -1;
    return ans;
}
}
;
```
['Graph', 'Heap (Priority Queue)', 'C++']