結果

問題 No.2325 Skill Tree
ユーザー 👑 zeronosu77108zeronosu77108
提出日時 2023-05-28 14:28:09
言語 Rust
(1.77.0)
結果
AC  
実行時間 495 ms / 3,000 ms
コード長 3,891 bytes
コンパイル時間 2,570 ms
コンパイル使用メモリ 154,820 KB
実行使用メモリ 55,764 KB
最終ジャッジ日時 2023-08-27 09:54:01
合計ジャッジ時間 19,135 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 214 ms
21,340 KB
testcase_08 AC 126 ms
19,496 KB
testcase_09 AC 235 ms
27,180 KB
testcase_10 AC 160 ms
27,712 KB
testcase_11 AC 231 ms
30,388 KB
testcase_12 AC 416 ms
54,804 KB
testcase_13 AC 414 ms
54,016 KB
testcase_14 AC 433 ms
54,284 KB
testcase_15 AC 427 ms
53,992 KB
testcase_16 AC 430 ms
55,312 KB
testcase_17 AC 428 ms
54,492 KB
testcase_18 AC 410 ms
55,560 KB
testcase_19 AC 418 ms
53,964 KB
testcase_20 AC 418 ms
55,000 KB
testcase_21 AC 410 ms
54,032 KB
testcase_22 AC 417 ms
54,012 KB
testcase_23 AC 415 ms
54,256 KB
testcase_24 AC 417 ms
54,028 KB
testcase_25 AC 424 ms
54,236 KB
testcase_26 AC 420 ms
55,332 KB
testcase_27 AC 491 ms
55,180 KB
testcase_28 AC 489 ms
55,184 KB
testcase_29 AC 495 ms
54,456 KB
testcase_30 AC 479 ms
55,048 KB
testcase_31 AC 472 ms
55,516 KB
testcase_32 AC 466 ms
55,764 KB
testcase_33 AC 457 ms
54,652 KB
testcase_34 AC 468 ms
54,996 KB
testcase_35 AC 472 ms
55,292 KB
testcase_36 AC 473 ms
55,500 KB
testcase_37 AC 479 ms
54,364 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused import: `std::cmp::Reverse`
 --> Main.rs:1:5
  |
1 | use std::cmp::Reverse;
  |     ^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: 1 warning emitted

ソースコード

diff #

use std::cmp::Reverse;
use std::collections::VecDeque;

fn main() {
    let mut sc = Scanner::new();
    let t = 1;
    for _ in 0..t { solve(&mut sc); }
}

fn solve(sc:&mut Scanner) {
    let n = sc.usize();
    let la = (1..n).map(|_| (sc.usize(), sc.usize1())).collect::<Vec<_>>();

    let mut g = vec![vec![]; n];
    for (i, &(l, a)) in la.iter().enumerate() {
        g[a].push((i+1, l));
    }

    let dist = bfs(0, n, &g);

    let mut d = dist.iter().filter_map(|&x| x).collect::<Vec<_>>();
    d.sort();

    let q = sc.usize();
    for _ in 0..q {
        let t = sc.u8();
        match t {
            1 => {
                let x = sc.usize();
                let index = upper_bound(&d, x);
                println!("{}", index);
            }
            2 => {
                let y = sc.usize1();
                if let Some(dist) = dist[y] {
                    println!("{}", dist);
                } else {
                    println!("-1");
                }
            }
            _ => unreachable!()
        }
    }
}

fn upper_bound<T: Ord>(v: &Vec<T>, x: T) -> usize {
    let mut l = 0;
    let mut r = v.len();
    while l < r {
        let m = (l + r) / 2;
        if v[m] <= x {
            l = m + 1;
        } else {
            r = m;
        }
    }
    l
}

fn bfs(s:usize, n:usize, g:&Vec<Vec<(usize, usize)>>) -> Vec<Option<usize>> {
    let mut q = VecDeque::new(); q.push_back(s);
    let mut dist = vec![None; n];
    dist[s] = Some(0);

    while let Some(v) = q.pop_front() {
        for &(u, c) in g[v].iter() {
            if dist[u] >= dist[v].max(Some(c)) { continue }
            dist[u] = dist[v].max(Some(c));
            q.push_back(u);
        }
    }

    dist
}

struct Scanner { s : std::collections::VecDeque<String> } #[allow(unused)] impl Scanner { fn new() -> Self { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); Self { s : s.split_whitespace().map(|s| s.to_string()).collect() } } fn reload(&mut self) -> () { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); self.s = s.split_whitespace().map(|s| s.to_string()).collect(); } fn usize(&mut self) -> usize { self.input() } fn usize1(&mut self) -> usize { self.input::<usize>() - 1 } fn isize(&mut self) -> isize { self.input() } fn i32(&mut self) -> i32 { self.input() } fn i64(&mut self) -> i64 { self.input() } fn i128(&mut self) -> i128 { self.input() } fn u8(&mut self) -> u8 { self.input() } fn u32(&mut self) -> u32 { self.input() } fn u64(&mut self) -> u64 { self.input() } fn u128(&mut self) -> u128 { self.input() } fn edge(&mut self) -> (usize, usize) { (self.usize1(), self.usize1()) } fn edges(&mut self, m : usize) -> Vec<(usize, usize)> { let mut e = Vec::with_capacity(m); for _ in 0..m { e.push(self.edge()); } e } fn wedge<T:std::str::FromStr>(&mut self) -> (usize, usize, T) { (self.usize1(), self.usize1(), self.input()) } fn wedges<T:std::str::FromStr>(&mut self, m : usize) -> Vec<(usize, usize, T)> { let mut e = Vec::with_capacity(m); for _ in 0..m { e.push(self.wedge()); } e } fn input<T>(&mut self) -> T where T: std::str::FromStr { if self.s.is_empty() { self.reload(); } if let Some(head) = self.s.pop_front() { head.parse::<T>().ok().unwrap() } else { panic!() } } fn tuple<T, U>(&mut self) -> (T, U) where T: std::str::FromStr, U: std::str::FromStr { (self.input(), self.input()) } fn vec<T>(&mut self, n: usize) -> Vec<T> where T: std::str::FromStr { if self.s.is_empty() { self.reload(); } self.s.drain(..n).map(|s| s.parse::<T>().ok().unwrap() ).collect::<Vec<T>>() } fn nvec<T>(&mut self) -> Vec<T> where T: std::str::FromStr { let n : usize = self.input(); self.vec(n) } fn chars(&mut self) -> Vec<char> { let s : String = self.input(); s.chars().collect() } fn bytes(&mut self) -> Vec<u8> { let s : String = self.input(); s.bytes().collect() } }
0