結果

問題 No.1420 国勢調査 (Easy)
ユーザー koba-e964koba-e964
提出日時 2021-10-15 09:57:24
言語 Rust
(1.77.0)
結果
AC  
実行時間 103 ms / 2,000 ms
コード長 2,941 bytes
コンパイル時間 1,689 ms
コンパイル使用メモリ 190,964 KB
実行使用メモリ 16,452 KB
最終ジャッジ日時 2023-10-17 19:35:02
合計ジャッジ時間 8,376 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 57 ms
11,780 KB
testcase_03 AC 58 ms
11,844 KB
testcase_04 AC 57 ms
11,820 KB
testcase_05 AC 57 ms
11,840 KB
testcase_06 AC 58 ms
11,812 KB
testcase_07 AC 55 ms
9,776 KB
testcase_08 AC 56 ms
11,784 KB
testcase_09 AC 55 ms
11,544 KB
testcase_10 AC 55 ms
11,544 KB
testcase_11 AC 54 ms
9,472 KB
testcase_12 AC 12 ms
5,692 KB
testcase_13 AC 19 ms
5,688 KB
testcase_14 AC 12 ms
5,688 KB
testcase_15 AC 18 ms
5,688 KB
testcase_16 AC 18 ms
5,692 KB
testcase_17 AC 19 ms
5,692 KB
testcase_18 AC 18 ms
5,692 KB
testcase_19 AC 19 ms
5,688 KB
testcase_20 AC 19 ms
5,692 KB
testcase_21 AC 19 ms
5,692 KB
testcase_22 AC 98 ms
16,448 KB
testcase_23 AC 95 ms
16,452 KB
testcase_24 AC 103 ms
16,452 KB
testcase_25 AC 102 ms
16,448 KB
testcase_26 AC 97 ms
16,452 KB
testcase_27 AC 80 ms
13,608 KB
testcase_28 AC 79 ms
13,344 KB
testcase_29 AC 75 ms
13,612 KB
testcase_30 AC 80 ms
13,348 KB
testcase_31 AC 79 ms
12,920 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::{Write, BufWriter};
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes.by_ref().map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

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

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => { ($(read_value!($next, $t)),*) };
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };
    ($next:expr, usize1) => (read_value!($next, usize) - 1);
    ($next:expr, [ $t:tt ]) => {{
        let len = read_value!($next, usize);
        read_value!($next, [$t; len])
    }};
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

trait Change { fn chmax(&mut self, x: Self); fn chmin(&mut self, x: Self); }
impl<T: PartialOrd> Change for T {
    fn chmax(&mut self, x: T) { if *self < x { *self = x; } }
    fn chmin(&mut self, x: T) { if *self > x { *self = x; } }
}

fn main() {
    // In order to avoid potential stack overflow, spawn a new thread.
    let stack_size = 104_857_600; // 100 MB
    let thd = std::thread::Builder::new().stack_size(stack_size);
    thd.spawn(|| solve()).unwrap().join().unwrap();
}

fn dfs(v: usize, g: &[Vec<(usize, i64)>], vis: &mut [bool], dp: &mut [i64], x: i64) -> Result<(), ()> {
    if vis[v] {
        return if x == dp[v] {
            Ok(())
        } else {
            Err(())
        };
    }
    vis[v] = true;
    dp[v] = x;
    for &(w, c) in &g[v] {
        dfs(w, g, vis, dp, x ^ c)?;
    }
    Ok(())
}

fn solve() {
    let out = std::io::stdout();
    let mut out = BufWriter::new(out.lock());
    macro_rules! puts {($($format:tt)*) => (let _ = write!(out,$($format)*););}
    input! {
        n: usize, m: usize,
        aby: [(usize1, usize1, i64); m],
    }
    let mut g = vec![vec![]; n];
    for &(a, b, y) in &aby {
        g[a].push((b, y));
        g[b].push((a, y));
    }
    let mut vis = vec![false; n];
    let mut dp = vec![0; n];
    for i in 0..n {
        if !vis[i] {
            if dfs(i, &g, &mut vis, &mut dp, 0).is_err() {
                puts!("-1\n");
                return;
            }
        }
    }
    for i in 0..n {
        puts!("{}\n", dp[i]);
    }
}
0