結果

問題 No.2780 The Bottle Imp
ユーザー naut3naut3
提出日時 2024-06-07 21:49:20
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 1,620 bytes
コンパイル時間 16,463 ms
コンパイル使用メモリ 379,116 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-08 10:29:10
合計ジャッジ時間 14,227 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 ms
5,248 KB
testcase_01 AC 0 ms
5,248 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 0 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 8 ms
5,376 KB
testcase_08 AC 7 ms
5,376 KB
testcase_09 AC 7 ms
5,376 KB
testcase_10 AC 7 ms
5,376 KB
testcase_11 AC 7 ms
5,376 KB
testcase_12 AC 11 ms
5,376 KB
testcase_13 AC 11 ms
5,376 KB
testcase_14 AC 5 ms
5,376 KB
testcase_15 AC 5 ms
5,376 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 6 ms
5,376 KB
testcase_19 AC 6 ms
5,376 KB
testcase_20 AC 6 ms
5,376 KB
testcase_21 WA -
testcase_22 AC 3 ms
5,376 KB
testcase_23 AC 5 ms
5,376 KB
testcase_24 AC 6 ms
5,376 KB
testcase_25 AC 9 ms
5,376 KB
testcase_26 AC 6 ms
5,376 KB
testcase_27 AC 5 ms
5,376 KB
testcase_28 AC 6 ms
5,376 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 9 ms
5,376 KB
testcase_32 AC 4 ms
5,376 KB
testcase_33 AC 10 ms
5,376 KB
testcase_34 AC 10 ms
5,376 KB
testcase_35 AC 4 ms
5,376 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 AC 3 ms
5,376 KB
testcase_39 AC 8 ms
5,376 KB
testcase_40 AC 9 ms
5,376 KB
testcase_41 WA -
testcase_42 AC 1 ms
5,376 KB
testcase_43 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(non_snake_case, unused_imports)]
use proconio::{fastout, input, marker::*};

#[fastout]
fn main() {
    input! {
        N: usize,
    }

    let mut uf = UnionFind::new(N);

    for i in 0..N {
        input! {
            M: usize,
            A: [Usize1; M],
        }

        for a in A {
            uf.unite(a, i);
        }
    }

    for i in 0..N {
        if !uf.is_same(i, 0) {
            println!("No");
            return;
        }
    }

    println!("Yes");
}

pub struct UnionFind {
    data: Vec<i32>,
}

impl UnionFind {
    pub fn new(size: usize) -> Self {
        return Self {
            data: vec![-1; size],
        };
    }

    fn _find(&mut self, v: usize) -> usize {
        assert!(v < self.data.len());
        if self.data[v] < 0 {
            return v;
        }

        self.data[v] = self._find(self.data[v] as usize) as i32;
        return self.data[v] as usize;
    }

    pub fn is_same(&mut self, u: usize, v: usize) -> bool {
        assert!(v < self.data.len() && u < self.data.len());
        self._find(u) == self._find(v)
    }

    pub fn unite(&mut self, mut a: usize, mut b: usize) -> () {
        assert!(a < self.data.len() && b < self.data.len());
        a = self._find(a);
        b = self._find(b);

        if a == b {
            return;
        }
        if self.data[a] > self.data[b] {
            (a, b) = (b, a);
        }

        self.data[a] += self.data[b];
        self.data[b] = a as i32;
    }

    pub fn size(&mut self, mut v: usize) -> i32 {
        assert!(v < self.data.len());
        v = self._find(v);
        -self.data[v]
    }
}
0