結果

問題 No.2316 Freight Train
ユーザー nautnaut
提出日時 2023-06-18 19:36:41
言語 Rust
(1.77.0)
結果
AC  
実行時間 58 ms / 2,000 ms
コード長 3,315 bytes
コンパイル時間 2,623 ms
コンパイル使用メモリ 158,336 KB
実行使用メモリ 7,940 KB
最終ジャッジ日時 2023-09-08 15:42:42
合計ジャッジ時間 8,762 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 55 ms
7,564 KB
testcase_04 AC 26 ms
4,932 KB
testcase_05 AC 21 ms
4,744 KB
testcase_06 AC 5 ms
4,380 KB
testcase_07 AC 29 ms
4,380 KB
testcase_08 AC 42 ms
7,912 KB
testcase_09 AC 37 ms
5,788 KB
testcase_10 AC 35 ms
5,076 KB
testcase_11 AC 43 ms
7,232 KB
testcase_12 AC 47 ms
6,888 KB
testcase_13 AC 57 ms
7,924 KB
testcase_14 AC 57 ms
7,928 KB
testcase_15 AC 56 ms
7,916 KB
testcase_16 AC 57 ms
7,916 KB
testcase_17 AC 57 ms
7,904 KB
testcase_18 AC 58 ms
7,904 KB
testcase_19 AC 57 ms
7,940 KB
testcase_20 AC 57 ms
7,916 KB
testcase_21 AC 58 ms
7,904 KB
testcase_22 AC 58 ms
7,864 KB
testcase_23 AC 32 ms
6,564 KB
testcase_24 AC 41 ms
6,500 KB
testcase_25 AC 34 ms
5,704 KB
testcase_26 AC 29 ms
5,756 KB
testcase_27 AC 15 ms
4,380 KB
testcase_28 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused `Result` that must be used
  --> 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
  --> 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)

warning: 2 warnings emitted

ソースコード

diff #

#![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())
            }
        }
    }
}
0