結果
問題 | No.1420 国勢調査 (Easy) |
ユーザー | koba-e964 |
提出日時 | 2021-10-15 09:57:24 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 55 ms / 2,000 ms |
コード長 | 2,941 bytes |
コンパイル時間 | 11,174 ms |
コンパイル使用メモリ | 401,676 KB |
実行使用メモリ | 19,068 KB |
最終ジャッジ日時 | 2024-09-17 16:47:03 |
合計ジャッジ時間 | 15,732 ms |
ジャッジサーバーID (参考情報) |
judge6 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,816 KB |
testcase_01 | AC | 1 ms
6,820 KB |
testcase_02 | AC | 37 ms
10,752 KB |
testcase_03 | AC | 35 ms
10,624 KB |
testcase_04 | AC | 34 ms
10,624 KB |
testcase_05 | AC | 33 ms
10,812 KB |
testcase_06 | AC | 33 ms
10,624 KB |
testcase_07 | AC | 32 ms
10,752 KB |
testcase_08 | AC | 32 ms
10,624 KB |
testcase_09 | AC | 32 ms
12,328 KB |
testcase_10 | AC | 32 ms
10,624 KB |
testcase_11 | AC | 31 ms
10,240 KB |
testcase_12 | AC | 8 ms
6,944 KB |
testcase_13 | AC | 11 ms
6,940 KB |
testcase_14 | AC | 8 ms
6,944 KB |
testcase_15 | AC | 12 ms
6,944 KB |
testcase_16 | AC | 12 ms
6,944 KB |
testcase_17 | AC | 12 ms
6,944 KB |
testcase_18 | AC | 12 ms
6,940 KB |
testcase_19 | AC | 11 ms
6,940 KB |
testcase_20 | AC | 11 ms
6,944 KB |
testcase_21 | AC | 11 ms
6,944 KB |
testcase_22 | AC | 51 ms
18,904 KB |
testcase_23 | AC | 53 ms
17,872 KB |
testcase_24 | AC | 55 ms
17,828 KB |
testcase_25 | AC | 54 ms
17,684 KB |
testcase_26 | AC | 49 ms
19,068 KB |
testcase_27 | AC | 41 ms
14,592 KB |
testcase_28 | AC | 41 ms
14,408 KB |
testcase_29 | AC | 43 ms
14,464 KB |
testcase_30 | AC | 41 ms
14,336 KB |
testcase_31 | AC | 41 ms
13,824 KB |
ソースコード
#[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]); } }