結果

問題 No.1420 国勢調査 (Easy)
ユーザー StrorkisStrorkis
提出日時 2021-03-05 22:56:34
言語 Rust
(1.77.0)
結果
AC  
実行時間 54 ms / 2,000 ms
コード長 1,774 bytes
コンパイル時間 814 ms
コンパイル使用メモリ 170,812 KB
実行使用メモリ 13,952 KB
最終ジャッジ日時 2024-04-16 10:56:41
合計ジャッジ時間 4,155 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 20 ms
9,472 KB
testcase_03 AC 21 ms
9,344 KB
testcase_04 AC 20 ms
9,216 KB
testcase_05 AC 20 ms
9,344 KB
testcase_06 AC 21 ms
9,344 KB
testcase_07 AC 19 ms
9,344 KB
testcase_08 AC 19 ms
9,344 KB
testcase_09 AC 19 ms
9,472 KB
testcase_10 AC 19 ms
9,472 KB
testcase_11 AC 19 ms
9,344 KB
testcase_12 AC 8 ms
6,400 KB
testcase_13 AC 12 ms
6,272 KB
testcase_14 AC 7 ms
6,272 KB
testcase_15 AC 12 ms
6,272 KB
testcase_16 AC 11 ms
6,400 KB
testcase_17 AC 13 ms
6,272 KB
testcase_18 AC 13 ms
6,400 KB
testcase_19 AC 13 ms
6,400 KB
testcase_20 AC 13 ms
6,272 KB
testcase_21 AC 13 ms
6,400 KB
testcase_22 AC 46 ms
13,952 KB
testcase_23 AC 50 ms
13,952 KB
testcase_24 AC 54 ms
13,952 KB
testcase_25 AC 49 ms
13,952 KB
testcase_26 AC 47 ms
13,952 KB
testcase_27 AC 33 ms
13,696 KB
testcase_28 AC 32 ms
13,824 KB
testcase_29 AC 30 ms
13,696 KB
testcase_30 AC 31 ms
13,824 KB
testcase_31 AC 33 ms
13,824 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fn run<'a, F: FnMut() -> &'a str, W: std::io::Write>(scan: &mut F, writer: &mut W) {
    macro_rules! scan {
        ([$t:tt; $n:expr]) => ((0..$n).map(|_| scan!($t)).collect::<Vec<_>>());
        (($($t:tt),*)) => (($(scan!($t)),*));
        (Usize1) => (scan!(usize) - 1);
        (Bytes) => (scan().as_bytes().to_vec());
        ($t:ty) => (scan().parse::<$t>().unwrap());
    }
    macro_rules! println {
        ($($arg:tt)*) => (writeln!(writer, $($arg)*).ok());
    }

    let (n, m) = scan!((usize, usize));

    let mut graph = vec![vec![]; n];
    for _ in 0..m {
        let (a, b) = scan!((Usize1, Usize1));
        let y = scan!(u32);
        graph[a].push((b, y));
        graph[b].push((a, y));
    }

    let mut ans = vec![!0; n];
    let mut stack = Vec::new();
    for i in 0..n {
        if ans[i] < !0 { continue; }
        ans[i] = 0;
        stack.push(i);
        while let Some(from) = stack.pop() {
            let x = ans[from];
            for &(to, y) in &graph[from] {
                if ans[to] < !0 {
                    if x ^ y != ans[to] {
                        println!("-1");
                        return;
                    }
                } else {
                    ans[to] = x ^ y;
                    stack.push(to);
                }
            }
        }    
    }

    for ans in ans {
        println!("{}", ans);
    }
}

fn main() {
    let ref mut buf = Vec::new();
    std::io::Read::read_to_end(&mut std::io::stdin(), buf).ok();
    let mut scanner = unsafe {
        std::str::from_utf8_unchecked(buf).split_ascii_whitespace()
    };
    let ref mut scan = || scanner.next().unwrap();

    let stdout = std::io::stdout();
    let ref mut writer = std::io::BufWriter::new(stdout.lock());

    run(scan, writer);
}
0