結果

問題 No.1639 最小通信路
ユーザー irumo8202irumo8202
提出日時 2022-02-13 13:28:18
言語 TypeScript
(5.4.3)
結果
WA  
実行時間 -
コード長 1,303 bytes
コンパイル時間 7,179 ms
コンパイル使用メモリ 254,840 KB
実行使用メモリ 48,672 KB
最終ジャッジ日時 2023-09-11 15:27:37
合計ジャッジ時間 12,307 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
41,456 KB
testcase_01 AC 76 ms
41,540 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 77 ms
41,652 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 76 ms
41,448 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 76 ms
42,396 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"

class UnionFind {
    private parents: number[]
    constructor(size: number) {
        this.parents = Array(size).fill(-1)
    }

    public find(x: number): number {
        if (this.parents[x] < 0) return x
        this.parents[x] = this.find(this.parents[x])
        return this.parents[x]
    }

    public merge(x: number, y: number): void {
        x = this.find(x)
        y = this.find(y)
        if (x == y) return

        if (this.parents[x] > this.parents[y]) {
            [x, y] = [y, x]
        }
        this.parents[x] += this.parents[y]
        this.parents[y] = x
    }

    public size(x: number): number {
        return -this.parents[x]
    }

    public same(x: number, y: number): boolean {
        return this.find(x) === this.find(y)
    }
}

type Input = (args: string) => void

const main: Input = args => {
    const input = args.trim().split("\n");
    const N = +input.shift()
    const uf = new UnionFind(N + 1);

    input.some(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