結果

問題 No.1639 最小通信路
ユーザー irumo8202irumo8202
提出日時 2022-02-13 14:47:48
言語 TypeScript
(5.4.3)
結果
WA  
実行時間 -
コード長 1,469 bytes
コンパイル時間 7,145 ms
コンパイル使用メモリ 251,388 KB
実行使用メモリ 49,548 KB
最終ジャッジ日時 2023-09-11 15:29:56
合計ジャッジ時間 12,460 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
41,644 KB
testcase_01 AC 77 ms
41,596 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 76 ms
41,456 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 77 ms
41,552 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 79 ms
41,556 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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'));
0