結果
問題 | No.2316 Freight Train |
ユーザー | naut3 |
提出日時 | 2023-06-18 19:36:41 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 59 ms / 2,000 ms |
コード長 | 3,315 bytes |
コンパイル時間 | 17,490 ms |
コンパイル使用メモリ | 378,432 KB |
実行使用メモリ | 7,936 KB |
最終ジャッジ日時 | 2024-06-26 08:43:52 |
合計ジャッジ時間 | 18,495 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,248 KB |
testcase_02 | AC | 1 ms
5,248 KB |
testcase_03 | AC | 55 ms
7,552 KB |
testcase_04 | AC | 26 ms
5,376 KB |
testcase_05 | AC | 21 ms
5,376 KB |
testcase_06 | AC | 5 ms
5,376 KB |
testcase_07 | AC | 28 ms
5,376 KB |
testcase_08 | AC | 42 ms
7,808 KB |
testcase_09 | AC | 37 ms
5,760 KB |
testcase_10 | AC | 34 ms
5,376 KB |
testcase_11 | AC | 44 ms
7,296 KB |
testcase_12 | AC | 46 ms
6,656 KB |
testcase_13 | AC | 56 ms
7,936 KB |
testcase_14 | AC | 57 ms
7,936 KB |
testcase_15 | AC | 57 ms
7,936 KB |
testcase_16 | AC | 56 ms
7,936 KB |
testcase_17 | AC | 57 ms
7,936 KB |
testcase_18 | AC | 56 ms
7,808 KB |
testcase_19 | AC | 56 ms
7,936 KB |
testcase_20 | AC | 57 ms
7,936 KB |
testcase_21 | AC | 59 ms
7,936 KB |
testcase_22 | AC | 57 ms
7,936 KB |
testcase_23 | AC | 31 ms
6,400 KB |
testcase_24 | AC | 39 ms
6,400 KB |
testcase_25 | AC | 33 ms
5,760 KB |
testcase_26 | AC | 28 ms
5,760 KB |
testcase_27 | AC | 15 ms
5,376 KB |
testcase_28 | AC | 1 ms
5,376 KB |
コンパイルメッセージ
warning: unused `Result` that must be used --> src/main.rs:37:13 | 37 | writeln!(out, "Yes"); | ^^^^^^^^^^^^^^^^^^^^ | = note: this `Result` may be an `Err` variant, which should be handled = note: `#[warn(unused_must_use)]` on by default = note: this warning originates in the macro `writeln` (in Nightly builds, run with -Z macro-backtrace for more info) warning: unused `Result` that must be used --> src/main.rs:39:13 | 39 | writeln!(out, "No"); | ^^^^^^^^^^^^^^^^^^^ | = note: this `Result` may be an `Err` variant, which should be handled = note: this warning originates in the macro `writeln` (in Nightly builds, run with -Z macro-backtrace for more info)
ソースコード
#![allow(non_snake_case, unused_imports)] use std::io::{self, prelude::*}; use std::str; fn main() { let (stdin, stdout) = (io::stdin(), io::stdout()); let mut scan = Scanner::new(stdin.lock()); let mut out = io::BufWriter::new(stdout.lock()); macro_rules! input { ($T: ty) => { scan.token::<$T>() }; } let N = input!(usize); let Q = input!(usize); let mut uf = unionfind::new(N); for i in 0..N { let p = input!(isize); if p == -1 { continue; } let q = p as usize - 1; uf.unite(i, q); } for _ in 0..Q { let a = input!(usize) - 1; let b = input!(usize) - 1; if uf.issame(a, b) { writeln!(out, "Yes"); } else { writeln!(out, "No"); } } } pub mod unionfind { pub struct UnionFind { n: usize, parent: Vec<usize>, rank: Vec<usize>, size: Vec<usize>, } /// 大きさ n のUnionFindを生成する. pub fn new(n: usize) -> UnionFind { let uf = UnionFind { n: n, parent: vec![n; n], rank: vec![0; n], size: vec![1; n], }; uf } impl UnionFind { /// x と y が同じ集合に含まれるかを検索する. pub fn issame(&mut self, x: usize, y: usize) -> bool { return self.root(x) == self.root(y); } /// x と y が含まれる集合をそれぞれ合併する. pub fn unite(&mut self, mut x: usize, mut y: usize) { x = self.root(x); y = self.root(y); if x != y { if self.rank[x] < self.rank[y] { std::mem::swap(&mut x, &mut y); } self.parent[y] = x; if self.rank[x] == self.rank[y] { self.rank[x] += 1; } self.size[x] += self.size[y]; } } /// x が含まれる集合の大きさを求める. pub fn size(&mut self, x: usize) -> usize { let r = self.root(x); return self.size[r]; } fn root(&mut self, x: usize) -> usize { if self.parent[x] == self.n { return x; } else { self.parent[x] = self.root(self.parent[x]); return self.parent[x]; } } } } struct Scanner<R> { reader: R, buf_str: Vec<u8>, buf_iter: str::SplitWhitespace<'static>, } impl<R: BufRead> Scanner<R> { fn new(reader: R) -> Self { Self { reader, buf_str: vec![], buf_iter: "".split_whitespace(), } } fn token<T: str::FromStr>(&mut self) -> T { loop { if let Some(token) = self.buf_iter.next() { return token.parse().ok().expect("Failed parse"); } self.buf_str.clear(); self.reader .read_until(b'\n', &mut self.buf_str) .expect("Failed read"); self.buf_iter = unsafe { let slice = str::from_utf8_unchecked(&self.buf_str); std::mem::transmute(slice.split_whitespace()) } } } }