class UnionFind: def __init__(self, n): self.n = n self.par = [-1] * n self.group_ = n def find(self, x): if self.par[x] < 0: return x lst = [] while self.par[x] >= 0: lst.append(x) x = self.par[x] for y in lst: self.par[y] = x return x def unite(self, x, y): x = self.find(x) y = self.find(y) if x == y: return False if self.par[x] > self.par[y]: x, y = y, x self.par[x] += self.par[y] self.par[y] = x self.group_ -= 1 return True def size(self, x): return -self.par[self.find(x)] def same(self, x, y): return self.find(x) == self.find(y) @property def group(self): return self.group_ class WeightedLCA: def __init__(self, n, edges, e, ope, root=0): self.n = n self.e = e self.ope = ope self.logn = (n - 1).bit_length() self.root = root self.depth = [-1] * self.n self.parent = [[-1] * n for _ in range(self.logn)] self.weight = [[-1] * n for _ in range(self.logn)] self.dfs(edges) self.doubling() def dfs(self, edges): stack = [self.root] self.depth[self.root] = 0 while stack: pos = stack.pop() for npos, w in edges[pos]: if self.depth[npos] != -1: continue self.depth[npos] = self.depth[pos] + 1 self.parent[0][npos] = pos self.weight[0][npos] = w stack.append(npos) def doubling(self): for i in range(1, self.logn): for j in range(self.n): if self.parent[i - 1][j] != -1: p = self.parent[i - 1][j] self.parent[i][j] = self.parent[i - 1][p] self.weight[i][j] = self.ope(self.weight[i - 1][j], self.weight[i - 1][p]) def get(self, u, v): if self.depth[v] < self.depth[u]: u, v = v, u du = self.depth[u] dv = self.depth[v] ret = self.e for i in range(self.logn): if (dv - du) >> i & 1: ret = self.ope(ret, self.weight[i][v]) v = self.parent[i][v] if u == v: return ret, u for i in range(self.logn - 1, -1, -1): pu = self.parent[i][u] pv = self.parent[i][v] if pu != pv: ret = self.ope(ret, self.weight[i][u]) ret = self.ope(ret, self.weight[i][v]) u, v = pu, pv ret = self.ope(ret, self.weight[0][u]) ret = self.ope(ret, self.weight[0][v]) u = self.parent[0][u] return ret, u n, k, C = map(int, input().split()) E = [list(map(int, input().split())) for _ in range(k)] W = [e[2] * n + i for i, e in enumerate(E)] W.sort() nE = [] for w in W: i = w % n nE.append(E[i]) edges = [[] for _ in range(n)] UF = UnionFind(n) tot = 0 for u, v, w, _ in nE: u -= 1 v -= 1 if UF.unite(u, v): edges[u].append((v, w)) edges[v].append((u, w)) tot += w if UF.group == 1: break lca = WeightedLCA(n, edges, 0, lambda x, y: max(x, y)) ans = -1 for u, v, w, p in nE: u -= 1 v -= 1 nc = tot + w - lca.get(u, v)[0] if nc <= C: ans = max(ans, p) print(ans)