結果
問題 |
No.1639 最小通信路
|
ユーザー |
|
提出日時 | 2022-02-13 14:47:48 |
言語 | TypeScript (5.7.2) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,469 bytes |
コンパイル時間 | 8,325 ms |
コンパイル使用メモリ | 228,476 KB |
実行使用メモリ | 46,204 KB |
最終ジャッジ日時 | 2024-12-31 16:45:58 |
合計ジャッジ時間 | 12,441 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 3 WA * 40 |
ソースコード
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'));