Language: C++

#include <iostream>
#include <vector>
#include <deque>
using namespace std;

class UnionFind {/*{{{*/
  private:
    vector<int> parent;
    int root(int x)
    {
      if (parent[x] < 0) {
        return x;
      } else {
        return parent[x] = root(parent[x]);
      }
    }

  public:
    UnionFind(int size) : parent(size, -1) {}

    bool find(int x, int y)
    {
      return root(x) == root(y);
    }

    void union_set(int x, int y)
    {
      x = root(x);
      y = root(y);
      if (x != y) {
        if (parent[y] < parent[x]) {
          swap(x, y);
        }
        /* assert(parent[x] <= parent[y]); */
        parent[x] += parent[y];
        parent[y] = x;
      }
    }

    int count(int x)
    {
      return -parent[root(x)];
    }
};/*}}}*/

struct edge {
  int u, v, w;
};

struct cmp {
  int lower, upper;
  cmp(int l, int u) : lower(l), upper(u) {}
  bool operator()(const edge& lhs, const edge& rhs) const
  {
    int l = min(abs(lhs.w - lower), abs(lhs.w - upper));
    int r = min(abs(rhs.w - lower), abs(rhs.w - upper));
    return l < r;
  }
};

int solve(int N, deque<edge> edges)
{
  int ans = numeric_limits<int>::max();
  for (deque<edge>::iterator it(edges.begin()); it != edges.end(); ++it) {
    const edge e = *it;
    if (it->u != 0 && it->v != 0) {
      continue;
    }
    UnionFind uf(N);
    uf.union_set(it->u, it->v);
    it = edges.erase(it);

    deque<edge> v(edges);
    int lower = e.w, upper = e.w;
    sort(v.begin(), v.end(), cmp(lower, upper));
    for (int i = 0; i < N-2; i++) {
      while (true) {
        if (v.empty()) {
          goto FAIL;
        }
        const edge e = v.front();
        v.pop_front();
        if (!uf.find(e.u, e.v)) {
          uf.union_set(e.u, e.v);
          lower = min(e.w, lower);
          upper = max(e.w, upper);
          sort(v.begin(), v.end(), cmp(lower, upper));
          break;
        }
      }
    }
    ans = min(ans, upper - lower);
FAIL:

    it = edges.insert(it, e);
  }
  return ans < numeric_limits<int>::max() ? ans : -1;
}

int main()
{
  int n, m;
  while (cin >> n >> m && n != 0) {
    deque<edge> edges;
    for (int i = 0; i < m; i++) {
      edge e;
      cin >> e.u >> e.v >> e.w;
      e.u--;  e.v--;
      edges.push_back(e);
    }
    cout << solve(n, edges) << endl;
  }
  return 0;
}

Compiler output:
a.cc: In function 'int solve(int, std::deque<edge>)':
a.cc:63:13: error: 'numeric_limits' was not declared in this scope
   63 |   int ans = numeric_limits<int>::max();
      |             ^~~~~~~~~~~~~~
a.cc:63:28: error: expected primary-expression before 'int'
   63 |   int ans = numeric_limits<int>::max();
      |                            ^~~
a.cc:75:5: error: 'sort' was not declared in this scope; did you mean 'short'?
   75 |     sort(v.begin(), v.end(), cmp(lower, upper));
      |     ^~~~
      |     short
a.cc:97:31: error: expected primary-expression before 'int'
   97 |   return ans < numeric_limits<int>::max() ? ans : -1;
      |                               ^~~
a.cc:97:31: error: expected ';' before 'int'
   97 |   return ans < numeric_limits<int>::max() ? ans : -1;
      |                               ^~~
      |                               ;
a.cc:97:34: error: expected unqualified-id before '>' token
   97 |   return ans < numeric_limits<int>::max() ? ans : -1;
      |                                  ^