結果
問題 | No.2325 Skill Tree |
ユーザー | akakimidori |
提出日時 | 2023-05-28 14:57:46 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 151 ms / 3,000 ms |
コード長 | 7,339 bytes |
コンパイル時間 | 15,684 ms |
コンパイル使用メモリ | 377,644 KB |
実行使用メモリ | 37,888 KB |
最終ジャッジ日時 | 2024-06-08 06:36:34 |
合計ジャッジ時間 | 23,041 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 0 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,248 KB |
testcase_02 | AC | 1 ms
5,248 KB |
testcase_03 | AC | 1 ms
5,248 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 | 25 ms
7,936 KB |
testcase_08 | AC | 32 ms
14,592 KB |
testcase_09 | AC | 34 ms
12,288 KB |
testcase_10 | AC | 56 ms
22,528 KB |
testcase_11 | AC | 50 ms
18,048 KB |
testcase_12 | AC | 117 ms
33,792 KB |
testcase_13 | AC | 116 ms
33,792 KB |
testcase_14 | AC | 114 ms
33,792 KB |
testcase_15 | AC | 115 ms
33,664 KB |
testcase_16 | AC | 108 ms
33,792 KB |
testcase_17 | AC | 107 ms
33,536 KB |
testcase_18 | AC | 106 ms
33,536 KB |
testcase_19 | AC | 107 ms
33,536 KB |
testcase_20 | AC | 115 ms
33,536 KB |
testcase_21 | AC | 108 ms
33,536 KB |
testcase_22 | AC | 103 ms
34,176 KB |
testcase_23 | AC | 102 ms
34,176 KB |
testcase_24 | AC | 101 ms
34,176 KB |
testcase_25 | AC | 102 ms
34,304 KB |
testcase_26 | AC | 109 ms
34,176 KB |
testcase_27 | AC | 146 ms
37,504 KB |
testcase_28 | AC | 141 ms
37,248 KB |
testcase_29 | AC | 141 ms
37,376 KB |
testcase_30 | AC | 142 ms
37,376 KB |
testcase_31 | AC | 142 ms
37,376 KB |
testcase_32 | AC | 138 ms
36,992 KB |
testcase_33 | AC | 133 ms
37,120 KB |
testcase_34 | AC | 133 ms
36,992 KB |
testcase_35 | AC | 145 ms
37,888 KB |
testcase_36 | AC | 142 ms
37,632 KB |
testcase_37 | AC | 151 ms
37,760 KB |
コンパイルメッセージ
warning: unused import: `std::collections::*` --> src/main.rs:2:5 | 2 | use std::collections::*; | ^^^^^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default
ソースコード
use std::io::Write; use std::collections::*; fn run() { input! { n: usize, p: [(u32, usize1); n - 1], q: usize, ask: [(u8, u32); q], } let mut dsu = DSU::new(n); let mut g = vec![vec![]; n]; for (i, &(l, a)) in p.iter().enumerate() { dsu.unite(i + 1, a); g[a].push((i + 1, l)); g[i + 1].push((a, l)); } let mut topo = vec![0]; let mut need = vec![1; n]; for i in 0..dsu.size(0) { let v = topo[i]; for (u, w) in g[v].clone() { need[u] = need[v].max(w); topo.push(u); g[u].retain(|p| p.0 != v); } } let mut x = (0..n).filter(|v| dsu.same(0, *v)).map(|v| need[v]).collect::<Vec<_>>(); x.sort(); let out = std::io::stdout(); let mut out = std::io::BufWriter::new(out.lock()); for (op, w) in ask { if op == 1 { let ans = x.upper_bound(&w); writeln!(out, "{}", ans).ok(); } else { let w = w as usize - 1; let ans = if dsu.same(w, 0) { need[w] as i32 } else { -1 }; writeln!(out, "{}", ans).ok(); } } } fn main() { run(); } // ---------- begin input macro ---------- // reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 #[macro_export] macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); input_inner!{iter, $($r)*} }; ($($r:tt)*) => { let s = { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); s }; let mut iter = s.split_whitespace(); input_inner!{iter, $($r)*} }; } #[macro_export] macro_rules! input_inner { ($iter:expr) => {}; ($iter:expr, ) => {}; ($iter:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($iter, $t); input_inner!{$iter $($r)*} }; } #[macro_export] macro_rules! read_value { ($iter:expr, ( $($t:tt),* )) => { ( $(read_value!($iter, $t)),* ) }; ($iter:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>() }; ($iter:expr, chars) => { read_value!($iter, String).chars().collect::<Vec<char>>() }; ($iter:expr, bytes) => { read_value!($iter, String).bytes().collect::<Vec<u8>>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } // ---------- end input macro ---------- //---------- begin union_find ---------- pub struct DSU { p: Vec<i32>, } impl DSU { pub fn new(n: usize) -> DSU { assert!(n < std::i32::MAX as usize); DSU { p: vec![-1; n] } } pub fn init(&mut self) { self.p.iter_mut().for_each(|p| *p = -1); } pub fn root(&self, mut x: usize) -> usize { assert!(x < self.p.len()); while self.p[x] >= 0 { x = self.p[x] as usize; } x } pub fn same(&self, x: usize, y: usize) -> bool { assert!(x < self.p.len() && y < self.p.len()); self.root(x) == self.root(y) } pub fn unite(&mut self, x: usize, y: usize) -> Option<(usize, usize)> { assert!(x < self.p.len() && y < self.p.len()); let mut x = self.root(x); let mut y = self.root(y); if x == y { return None; } if self.p[x] > self.p[y] { std::mem::swap(&mut x, &mut y); } self.p[x] += self.p[y]; self.p[y] = x as i32; Some((x, y)) } pub fn parent(&self, x: usize) -> Option<usize> { assert!(x < self.p.len()); let p = self.p[x]; if p >= 0 { Some(p as usize) } else { None } } pub fn sum<F>(&self, mut x: usize, mut f: F) -> usize where F: FnMut(usize), { while let Some(p) = self.parent(x) { f(x); x = p; } x } pub fn size(&self, x: usize) -> usize { assert!(x < self.p.len()); let r = self.root(x); (-self.p[r]) as usize } } //---------- end union_find ---------- // ---------- begin super slice ---------- pub trait SuperSlice { type Item; fn lower_bound(&self, key: &Self::Item) -> usize where Self::Item: Ord; fn lower_bound_by<F>(&self, f: F) -> usize where F: FnMut(&Self::Item) -> std::cmp::Ordering; fn lower_bound_by_key<K, F>(&self, key: &K, f: F) -> usize where K: Ord, F: FnMut(&Self::Item) -> K; fn upper_bound(&self, key: &Self::Item) -> usize where Self::Item: Ord; fn upper_bound_by<F>(&self, f: F) -> usize where F: FnMut(&Self::Item) -> std::cmp::Ordering; fn upper_bound_by_key<K, F>(&self, key: &K, f: F) -> usize where K: Ord, F: FnMut(&Self::Item) -> K; fn next_permutation(&mut self) -> bool where Self::Item: Ord; fn next_permutation_by<F>(&mut self, f: F) -> bool where F: FnMut(&Self::Item, &Self::Item) -> std::cmp::Ordering; fn prev_permutation(&mut self) -> bool where Self::Item: Ord; } impl<T> SuperSlice for [T] { type Item = T; fn lower_bound(&self, key: &Self::Item) -> usize where T: Ord, { self.lower_bound_by(|p| p.cmp(key)) } fn lower_bound_by<F>(&self, mut f: F) -> usize where F: FnMut(&Self::Item) -> std::cmp::Ordering, { self.binary_search_by(|p| f(p).then(std::cmp::Ordering::Greater)) .unwrap_err() } fn lower_bound_by_key<K, F>(&self, key: &K, mut f: F) -> usize where K: Ord, F: FnMut(&Self::Item) -> K, { self.lower_bound_by(|p| f(p).cmp(key)) } fn upper_bound(&self, key: &Self::Item) -> usize where T: Ord, { self.upper_bound_by(|p| p.cmp(key)) } fn upper_bound_by<F>(&self, mut f: F) -> usize where F: FnMut(&Self::Item) -> std::cmp::Ordering, { self.binary_search_by(|p| f(p).then(std::cmp::Ordering::Less)) .unwrap_err() } fn upper_bound_by_key<K, F>(&self, key: &K, mut f: F) -> usize where K: Ord, F: FnMut(&Self::Item) -> K, { self.upper_bound_by(|p| f(p).cmp(key)) } fn next_permutation(&mut self) -> bool where T: Ord, { self.next_permutation_by(|a, b| a.cmp(b)) } fn next_permutation_by<F>(&mut self, mut f: F) -> bool where F: FnMut(&Self::Item, &Self::Item) -> std::cmp::Ordering, { use std::cmp::Ordering::*; if let Some(x) = self.windows(2).rposition(|a| f(&a[0], &a[1]) == Less) { let y = self.iter().rposition(|b| f(&self[x], b) == Less).unwrap(); self.swap(x, y); self[(x + 1)..].reverse(); true } else { self.reverse(); false } } fn prev_permutation(&mut self) -> bool where T: Ord, { self.next_permutation_by(|a, b| a.cmp(b).reverse()) } } // ---------- end super slice ----------