結果

問題 No.483 マッチ並べ
ユーザー phsplsphspls
提出日時 2023-01-12 20:11:35
言語 Rust
(1.77.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,166 bytes
コンパイル時間 2,558 ms
コンパイル使用メモリ 148,292 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-25 01:33:04
合計ジャッジ時間 3,687 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,384 KB
testcase_09 AC 1 ms
4,384 KB
testcase_10 AC 1 ms
4,384 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,384 KB
testcase_17 AC 1 ms
4,384 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,384 KB
testcase_20 AC 1 ms
4,384 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,384 KB
testcase_23 AC 1 ms
4,384 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 1 ms
4,384 KB
testcase_27 AC 1 ms
4,384 KB
testcase_28 AC 1 ms
4,384 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 1 ms
4,380 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 1 ms
4,384 KB
testcase_39 AC 1 ms
4,384 KB
testcase_40 AC 1 ms
4,380 KB
testcase_41 AC 1 ms
4,380 KB
testcase_42 AC 1 ms
4,380 KB
testcase_43 AC 1 ms
4,384 KB
testcase_44 AC 1 ms
4,380 KB
testcase_45 AC 1 ms
4,380 KB
testcase_46 AC 1 ms
4,384 KB
testcase_47 AC 1 ms
4,380 KB
testcase_48 AC 2 ms
4,384 KB
testcase_49 AC 1 ms
4,384 KB
testcase_50 AC 2 ms
4,384 KB
testcase_51 AC 1 ms
4,384 KB
testcase_52 AC 1 ms
4,380 KB
testcase_53 AC 2 ms
4,384 KB
testcase_54 AC 1 ms
4,384 KB
testcase_55 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: field `n` is never read
 --> Main.rs:5:5
  |
4 | struct UnionFind {
  |        --------- field in this struct
5 |     n: usize,
  |     ^
  |
  = note: `#[warn(dead_code)]` on by default

warning: 1 warning emitted

ソースコード

diff #

const SIZE: usize = 1e4 as usize;

struct UnionFind {
    n: usize,
    parents: Vec<usize>,
}

impl UnionFind {
    fn new(n: usize) -> Self {
        UnionFind {
            n: n,
            parents: (0..n).collect(),
        }
    }

    fn equiv(&mut self, a: usize, b: usize) -> bool {
        self.find(a) == self.find(b)
    }
    
    fn unite(&mut self, a: usize, b: usize) {
        if self.equiv(a, b) { return; }
        let (a, b) = (a.min(b), a.max(b));
        let x = self.parents[a];
        let y = self.parents[b];
        self.parents[y] = self.parents[x];
    }

    fn find(&mut self, a: usize) -> usize {
        if self.parents[a] == a { return a; }
        let p = self.find(self.parents[a]);
        self.parents[a] = p;
        p
    }
}

fn main() {
    let mut n = String::new();
    std::io::stdin().read_line(&mut n).ok();
    let n: usize = n.trim().parse().unwrap();
    let items = (0..n).map(|_| {
            let mut temp = String::new();
            std::io::stdin().read_line(&mut temp).ok();
            let temp: Vec<usize> = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
            (temp[0].min(temp[2]), temp[1].min(temp[3]), temp[0].max(temp[2]), temp[1].max(temp[3]))
        })
        .collect::<Vec<_>>();

    let mut uf = UnionFind::new(SIZE);
    let mut size = vec![1usize; SIZE];
    let mut linecnt = vec![0usize; SIZE];
    for &(r0, c0, r1, c1) in items.iter() {
        let idx0 = (r0-1) * 100 + (c0-1);
        let idx1 = (r1-1) * 100 + (c1-1);
        let p0 = uf.find(idx0);
        let p1 = uf.find(idx1);
        if p0 == p1 {
            linecnt[p0] += 1;
            continue;
        }
        uf.unite(p0, p1);
        let p = uf.find(p0);
        if p == p0 {
            size[p0] += size[p1];
            size[p1] = 0;
            linecnt[p0] += linecnt[p1]+1;
            linecnt[p1] = 0;
        } else {
            size[p1] += size[p0];
            size[p0] = 0;
            linecnt[p1] += linecnt[p0]+1;
            linecnt[p0] = 0;
        }
    }
    if (0..SIZE).any(|i| size[i] < linecnt[i]) {
        println!("NO");
    } else {
        println!("YES");
    }
}
0