結果

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

ソースコード

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.forEach(abc => {
        const [_a, _b, c] = abc.split(" ").map(x => BigInt(x))
        let a = Number(_a)
        let b = Number(_b)
        if (uf.same(a, b)) return
        uf.merge(a, b)
        if (uf.size(1) == N) console.log()
    })
}

main(fs.readFileSync('/dev/stdin', 'utf8'));
0