結果
| 問題 |
No.2316 Freight Train
|
| コンテスト | |
| ユーザー |
naut3
|
| 提出日時 | 2023-06-18 19:36:41 |
| 言語 | Rust (1.83.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 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 26 |
コンパイルメッセージ
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())
}
}
}
}
naut3