結果

問題 No.1639 最小通信路
ユーザー irumo8202
提出日時 2022-02-13 13:28:18
言語 TypeScript
(5.7.2)
結果
WA  
実行時間 -
コード長 1,303 bytes
コンパイル時間 8,299 ms
コンパイル使用メモリ 231,408 KB
実行使用メモリ 46,452 KB
最終ジャッジ日時 2024-12-31 16:45:54
合計ジャッジ時間 12,378 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 3 WA * 40
権限があれば一括ダウンロードができます

ソースコード

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