結果
問題 | No.482 あなたの名は |
ユーザー | mio_h |
提出日時 | 2017-02-10 22:50:15 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 18 ms / 2,000 ms |
コード長 | 3,497 bytes |
コンパイル時間 | 14,046 ms |
コンパイル使用メモリ | 378,236 KB |
実行使用メモリ | 5,752 KB |
最終ジャッジ日時 | 2024-06-09 11:24:40 |
合計ジャッジ時間 | 15,853 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,248 KB |
testcase_02 | AC | 1 ms
5,376 KB |
testcase_03 | AC | 1 ms
5,376 KB |
testcase_04 | AC | 1 ms
5,376 KB |
testcase_05 | AC | 1 ms
5,376 KB |
testcase_06 | AC | 1 ms
5,376 KB |
testcase_07 | AC | 1 ms
5,376 KB |
testcase_08 | AC | 1 ms
5,376 KB |
testcase_09 | AC | 1 ms
5,376 KB |
testcase_10 | AC | 1 ms
5,376 KB |
testcase_11 | AC | 1 ms
5,376 KB |
testcase_12 | AC | 1 ms
5,376 KB |
testcase_13 | AC | 1 ms
5,376 KB |
testcase_14 | AC | 1 ms
5,376 KB |
testcase_15 | AC | 17 ms
5,628 KB |
testcase_16 | AC | 17 ms
5,752 KB |
testcase_17 | AC | 17 ms
5,752 KB |
testcase_18 | AC | 17 ms
5,752 KB |
testcase_19 | AC | 18 ms
5,748 KB |
testcase_20 | AC | 18 ms
5,624 KB |
testcase_21 | AC | 17 ms
5,624 KB |
testcase_22 | AC | 18 ms
5,620 KB |
testcase_23 | AC | 18 ms
5,748 KB |
testcase_24 | AC | 17 ms
5,624 KB |
testcase_25 | AC | 18 ms
5,624 KB |
testcase_26 | AC | 18 ms
5,752 KB |
testcase_27 | AC | 17 ms
5,704 KB |
testcase_28 | AC | 17 ms
5,624 KB |
testcase_29 | AC | 14 ms
5,628 KB |
testcase_30 | AC | 1 ms
5,376 KB |
コンパイルメッセージ
warning: use of deprecated method `std::error::Error::description`: use the Display impl or to_string() --> src/main.rs:108:62 | 108 | Err(why) => panic!("error in read_line: {}", why.description()), | ^^^^^^^^^^^ | = note: `#[warn(deprecated)]` on by default
ソースコード
use std::io::{self, Stdin}; use std::str::{self, FromStr}; use std::error::Error; use std::thread; fn exec() { let mut sc = Scanner::new(); let n: usize = sc.ne(); let k: usize = sc.ne(); let a: Vec<usize> = (0..n).map(|_| sc.ne()).collect(); let mut uf = UnionFind::new(n); for i in 0..n { uf.unite(i, a[i] - 1); } let mut used = vec![false; n]; let mut ans = 0usize; for i in 0..n { let par = uf.root(i); if used[par] { continue; } used[par] = true; let plus = uf.size(par) as usize; ans += plus - 1; } let ok = if k >= ans && (k & 1) == (ans & 1) { "YES" } else { "NO" }; println!("{}", ok); } struct UnionFind { data: Vec<i32>, group: usize, } #[allow(dead_code)] impl UnionFind { fn new(n: usize) -> UnionFind { UnionFind { data: vec![-1i32; n], group: n, } } //経路圧縮のみ fn unite(&mut self, x: usize, y: usize) -> bool { let x = self.root(x); let y = self.root(y); if x == y { return false; } self.data[x] += self.data[y]; self.data[y] = x as i32; self.group -= 1; true } fn root(&mut self, x: usize) -> usize { if self.data[x] < 0 { x } else { let par = self.data[x] as usize; let res = self.root(par); self.data[x] = res as i32; res } } fn same(&mut self, x: usize, y: usize) -> bool { self.root(x) == self.root(y) } fn size(&mut self, x: usize) -> i32 { let par = self.root(x); -self.data[par] } } const DEFAULT_STACK: usize = 16 * 1024 * 1024; fn main() { let builder = thread::Builder::new(); let th = builder.stack_size(DEFAULT_STACK); let handle = th.spawn(|| { exec(); }).unwrap(); let _ = handle.join(); } #[allow(dead_code)] struct Scanner { stdin: Stdin, id: usize, buf: Vec<u8>, } #[allow(dead_code)] impl Scanner { fn new() -> Scanner { Scanner { stdin: io::stdin(), id: 0, buf: Vec::new(), } } fn next_line(&mut self) -> Option<String> { let mut res = String::new(); match self.stdin.read_line(&mut res) { Ok(0) => return None, Ok(_) => Some(res), Err(why) => panic!("error in read_line: {}", why.description()), } } fn next<T: FromStr>(&mut self) -> Option<T> { while self.buf.len() == 0 { self.buf = match self.next_line() { Some(r) => { self.id = 0; r.trim().as_bytes().to_owned() } None => return None, }; } let l = self.id; assert!(self.buf[l] != b' '); let n = self.buf.len(); let mut r = l; while r < n && self.buf[r] != b' ' { r += 1; } let res = match str::from_utf8(&self.buf[l..r]).ok().unwrap().parse::<T>() { Ok(s) => Some(s), Err(_) => panic!("parse error"), }; while r < n && self.buf[r] == b' ' { r += 1; } if r == n { self.buf.clear(); } else { self.id = r; } res } fn ne<T: FromStr>(&mut self) -> T { self.next::<T>().unwrap() } }