結果

問題 No.697 池の数はいくつか
ユーザー taotao54321taotao54321
提出日時 2018-09-29 22:57:42
言語 Rust
(1.77.0)
結果
AC  
実行時間 458 ms / 6,000 ms
コード長 2,455 bytes
コンパイル時間 976 ms
コンパイル使用メモリ 163,284 KB
実行使用メモリ 98,724 KB
最終ジャッジ日時 2024-04-25 20:20:30
合計ジャッジ時間 5,731 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 0 ms
6,944 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 0 ms
6,944 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 1 ms
6,944 KB
testcase_12 AC 1 ms
6,944 KB
testcase_13 AC 1 ms
6,944 KB
testcase_14 AC 1 ms
6,944 KB
testcase_15 AC 1 ms
6,940 KB
testcase_16 AC 1 ms
6,940 KB
testcase_17 AC 0 ms
6,940 KB
testcase_18 AC 1 ms
6,944 KB
testcase_19 AC 1 ms
6,944 KB
testcase_20 AC 1 ms
6,944 KB
testcase_21 AC 0 ms
6,940 KB
testcase_22 AC 1 ms
6,940 KB
testcase_23 AC 1 ms
6,940 KB
testcase_24 AC 52 ms
12,788 KB
testcase_25 AC 53 ms
12,620 KB
testcase_26 AC 53 ms
12,672 KB
testcase_27 AC 53 ms
12,784 KB
testcase_28 AC 52 ms
12,608 KB
testcase_29 AC 341 ms
98,572 KB
testcase_30 AC 456 ms
98,680 KB
testcase_31 AC 344 ms
98,632 KB
testcase_32 AC 456 ms
98,672 KB
testcase_33 AC 456 ms
98,628 KB
testcase_34 AC 458 ms
98,724 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(non_snake_case)]

#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut tokens = $s.split_whitespace();
        input_inner! { tokens, $($r)* }
    };

    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut res = String::new();
            ::std::io::stdin().read_to_string(&mut res).unwrap();
            res
        };
        let mut tokens = s.split_whitespace();
        input_inner! { tokens, $($r)* }
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($tokens:expr)  => {};
    ($tokens:expr,) => {};

    ($tokens:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($tokens, $t);
        input_inner! { $tokens $($r)* }
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($tokens:expr, ( $($t:tt),* )) => {
        ( $(read_value!($tokens, $t)),* )
    };

    ($tokens:expr, [ $t:tt; $len:expr ]) => {
        (0..$len).map(|_| read_value!($tokens, $t)).collect::<Vec<_>>()
    };

    ($tokens:expr, chars) => {
        read_value!($tokens, String).chars().collect::<Vec<_>>()
    };

    ($tokens:expr, usize1) => {
        read_value!($tokens, usize) - 1
    };

    ($tokens:expr, $t:ty) => {
        $tokens.next().unwrap().parse::<$t>().expect("parse error")
    };
}

struct UnionFind {
    v: Vec<usize>,
}

impl UnionFind {
    fn new(size: usize) -> Self {
        let mut v = vec![0usize; size];
        for i in 0..size {
            v[i] = i;
        }
        Self {
            v,
        }
    }

    fn root(&mut self, x: usize) -> usize {
        let parent = self.v[x];
        if parent == x { return x; }
        let res = self.root(parent);
        self.v[x] = res;
        res
    }

    fn unite(&mut self, x: usize, y: usize) {
        let rootx = self.root(x);
        let rooty = self.root(y);
        self.v[rooty] = rootx;
    }
}

fn main() {
    input! {
        H: usize,
        W: usize,
        //M: [[u8; W]; H],
        M: [u8; W*H],
    }

    let mut uf = UnionFind::new(H*W);
    for y in 0..H { for x in 0..W {
        let idx = W*y + x;
        if x < W-1 && M[idx] == M[idx+1] {
            uf.unite(idx, idx+1);
        }
        if y < H-1 && M[idx] == M[idx+W] {
            uf.unite(idx, idx+W);
        }
    }}

    let mut ans = 0;
    for i in 0..H*W {
        if i == uf.v[i] && M[i] == 1 {
            ans += 1;
        }
    }
    println!("{}", ans);
}
0