import * as fs from "fs" type findType = (x: number) => number type mergeType = (x: number, y: number) => void type sizeType = (x: number) => number type sameType = (n: number, y: number) => boolean type UnionFindFunc = { find: findType; merge: mergeType; size: sizeType; same: sameType; } type UnionFindType = (n: number) => UnionFindFunc const UnionFind: UnionFindType = n => { const parents: number[] = Array(n).fill(-1) const find: findType = x => { if (parents[x] < 0) return x parents[x] = find(parents[x]) return parents[x] } const merge: mergeType = (x, y) => { x = find(x) y = find(y) if (x === y) return x if (parents[x] > parents[y]) [x, y] = [y, x] parents[x] += parents[y] parents[y] = x } const size: sizeType = x => -parents[x] const same: sameType = (x, y) => find(x) === find(y) return { find, merge, size, same } } type Input = (args: string) => void const main: Input = args => { const input = args.trim().split("\n"); const N = +input.shift() const uf = UnionFind(N + 1) input.forEach(abc => { const [_a, _b, c] = abc.split(" ") let a = Number(_a) let b = Number(_b) if (uf.same(a, b)) return false uf.merge(a, b) if (uf.size(1) === N) { console.log(c) return true } }) } main(fs.readFileSync('/dev/stdin', 'utf8'));