結果
| 問題 |
No.1420 国勢調査 (Easy)
|
| コンテスト | |
| ユーザー |
Strorkis
|
| 提出日時 | 2021-03-05 22:56:34 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 57 ms / 2,000 ms |
| コード長 | 1,774 bytes |
| コンパイル時間 | 16,170 ms |
| コンパイル使用メモリ | 379,324 KB |
| 実行使用メモリ | 13,952 KB |
| 最終ジャッジ日時 | 2024-10-07 04:17:55 |
| 合計ジャッジ時間 | 16,395 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 30 |
ソースコード
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);
}
Strorkis