結果
問題 | No.977 アリス仕掛けの摩天楼 |
ユーザー | へのく |
提出日時 | 2020-06-22 04:33:32 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 62 ms / 2,000 ms |
コード長 | 23,283 bytes |
コンパイル時間 | 17,346 ms |
コンパイル使用メモリ | 405,656 KB |
実行使用メモリ | 12,756 KB |
最終ジャッジ日時 | 2024-07-03 18:35:18 |
合計ジャッジ時間 | 18,875 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,812 KB |
testcase_01 | AC | 1 ms
6,940 KB |
testcase_02 | AC | 1 ms
6,944 KB |
testcase_03 | AC | 1 ms
6,944 KB |
testcase_04 | AC | 1 ms
6,940 KB |
testcase_05 | AC | 1 ms
6,940 KB |
testcase_06 | AC | 1 ms
6,944 KB |
testcase_07 | AC | 1 ms
6,940 KB |
testcase_08 | AC | 1 ms
6,944 KB |
testcase_09 | AC | 1 ms
6,940 KB |
testcase_10 | AC | 1 ms
6,940 KB |
testcase_11 | AC | 1 ms
6,940 KB |
testcase_12 | AC | 1 ms
6,944 KB |
testcase_13 | AC | 6 ms
6,940 KB |
testcase_14 | AC | 3 ms
6,944 KB |
testcase_15 | AC | 3 ms
6,940 KB |
testcase_16 | AC | 6 ms
6,940 KB |
testcase_17 | AC | 7 ms
6,940 KB |
testcase_18 | AC | 17 ms
6,944 KB |
testcase_19 | AC | 21 ms
9,216 KB |
testcase_20 | AC | 10 ms
6,940 KB |
testcase_21 | AC | 16 ms
6,944 KB |
testcase_22 | AC | 21 ms
6,940 KB |
testcase_23 | AC | 24 ms
6,940 KB |
testcase_24 | AC | 62 ms
12,756 KB |
testcase_25 | AC | 23 ms
6,940 KB |
ソースコード
#![allow(unused_imports, non_snake_case)] #![allow(dead_code)] use crate::scanner::Scanner; use crate::{data_structure::union_find::Utree, graph::lowlink::LowLink}; fn main() { let mut scan = Scanner::new(); let n = scan.read::<i32>(); let mut ut = Utree::new(n); let mut edges = list!(); for _ in 0..n - 1 { let u = scan.read::<i32>(); let v = scan.read::<i32>(); ut.unite(u, v); edges.push((u, v)); } println!( "{}", if ut.get_group_cnt() >= 3 { "Alice" } else if ut.get_group_cnt() == 2 { let ll = LowLink::new(n, edges); if ll.bridges().ilen() > 0 { "Alice" } else { "Bob" } } else { "Bob" } ); } pub mod ext { pub mod length { use std::collections::{BinaryHeap, HashMap, HashSet, VecDeque}; pub trait LengthI32 { fn ilen(&self) -> i32; } impl<T> LengthI32 for [T] { fn ilen(&self) -> i32 { self.len() as i32 } } impl<T> LengthI32 for Vec<T> { fn ilen(&self) -> i32 { self.len() as i32 } } impl<T> LengthI32 for HashSet<T> { fn ilen(&self) -> i32 { self.len() as i32 } } impl<K, V> LengthI32 for HashMap<K, V> { fn ilen(&self) -> i32 { self.len() as i32 } } impl<T> LengthI32 for VecDeque<T> { fn ilen(&self) -> i32 { self.len() as i32 } } impl<T> LengthI32 for BinaryHeap<T> { fn ilen(&self) -> i32 { self.len() as i32 } } } pub mod range { use crate::independent::integer::Int; use std::cmp::{max, min}; use std::ops::{Bound, Range, RangeBounds, RangeInclusive}; pub trait RangeEx<T> { fn width(&self) -> T; fn empty(&self) -> bool; fn contain_range(&self, inner: &Self) -> bool; fn separate_range(&self, other: &Self) -> bool; type ReturnRange; fn overlap(&self, other: &Self) -> Self::ReturnRange; } impl<T: Int> RangeEx<T> for Range<T> { fn width(&self) -> T { if self.empty() { T::zero() } else { self.end - self.start } } fn empty(&self) -> bool { !(self.start < self.end) } fn contain_range(&self, inner: &Self) -> bool { self.start <= inner.start && inner.end <= self.end } fn separate_range(&self, other: &Self) -> bool { self.end <= other.start || other.end <= self.start } type ReturnRange = Range<T>; fn overlap(&self, other: &Self) -> Self::ReturnRange { let left = max(self.start, other.start); let right = min(self.end, other.end); left..right } } impl<T: Int> RangeEx<T> for RangeInclusive<T> { fn width(&self) -> T { if self.empty() { T::zero() } else { *self.end() - *self.start() + T::one() } } fn empty(&self) -> bool { !(self.start() <= self.end()) } fn contain_range(&self, inner: &Self) -> bool { self.start() <= inner.start() && inner.end() <= self.end() } fn separate_range(&self, other: &Self) -> bool { self.end() <= other.start() || other.end() <= self.start() } type ReturnRange = RangeInclusive<T>; fn overlap(&self, other: &Self) -> Self::ReturnRange { let left = *max(self.start(), other.start()); let right = *min(self.end(), other.end()); left..=right } } pub trait IntRangeBounds<U: Int>: RangeBounds<U> { #[doc = " inclusive"] fn lower_bound(&self, lower_bound: U) -> U { match self.start_bound() { Bound::Included(x) => max(lower_bound, *x), Bound::Excluded(x) => max(lower_bound, *x + U::one()), Bound::Unbounded => lower_bound, } } #[doc = " exclusive"] fn upper_bound(&self, upper_bound: U) -> U { match self.end_bound() { Bound::Included(x) => min(upper_bound, *x + U::one()), Bound::Excluded(x) => min(upper_bound, *x), Bound::Unbounded => upper_bound, } } fn to_harfopen(&self, lb: U, ub: U) -> Range<U> { self.lower_bound(lb)..self.upper_bound(ub) } } impl<T: ?Sized, U: Int> IntRangeBounds<U> for T where T: RangeBounds<U> {} } } pub mod graph { pub mod lowlink { use crate::{arraylist::List, list}; use std::{cmp::min, collections::HashSet}; pub struct LowLink { g: List<List<i32>>, edges: List<(i32, i32)>, used_v: List<bool>, used_e: HashSet<(i32, i32)>, ord: List<i32>, lowlink: List<i32>, k: i32, } impl LowLink { pub fn new(n: i32, edges: List<(i32, i32)>) -> LowLink { let mut g = list ! ( list ! ( ) ; n ); for &(u, v) in &edges { g[u].push(v); g[v].push(u); } let mut ll = LowLink { g, edges, used_v: list ! ( false ; n ), used_e: HashSet::new(), ord: list ! ( 0 ; n ), lowlink: list ! ( 0 ; n ), k: 0, }; ll.dfs(0); ll } fn dfs(&mut self, v: i32) { self.used_v[v] = true; self.ord[v] = self.k; self.lowlink[v] = self.k; self.k += 1; for u in self.g[v].clone() { if !self.used_v[u] { self.used_e.insert((v, u)); self.dfs(u); self.lowlink[v] = min(self.lowlink[v], self.lowlink[u]); } else if !self.used_e.contains(&(u, v)) { self.lowlink[v] = min(self.lowlink[v], self.ord[u]); } } } pub fn is_bridge(&self, mut u: i32, mut v: i32) -> bool { if self.ord[u] > self.ord[v] { std::mem::swap(&mut u, &mut v); } self.ord[u] < self.lowlink[v] } pub fn bridges(&self) -> List<(i32, i32)> { let mut ret = list!(); for &(u, v) in &self.edges { if self.is_bridge(u, v) { ret.push((u, v)); } } ret } pub fn is_arti(&self, v: i32) -> bool { for &u in &self.g[v] { if self.ord[v] < self.ord[u] && self.ord[u] <= self.lowlink[v] { return true; } } false } pub fn arti_points(&self) -> List<i32> { (0..self.g.ilen()).filter(|v| self.is_arti(*v)).collect() } } } } pub mod scanner { use crate::arraylist::List; use std::io::{stdin, BufReader, Bytes, Read, Stdin}; use std::str::FromStr; pub struct Scanner { buf: Bytes<BufReader<Stdin>>, } impl Scanner { pub fn new() -> Scanner { Scanner { buf: BufReader::new(stdin()).bytes(), } } pub fn read_next<T: FromStr>(&mut self) -> Option<T> { let token = self .buf .by_ref() .map(|c| c.unwrap() as char) .skip_while(|c| c.is_whitespace()) .take_while(|c| !c.is_whitespace()) .collect::<String>(); token.parse::<T>().ok() } pub fn read<T: FromStr>(&mut self) -> T { self.read_next().unwrap() } pub fn readn<T: FromStr>(&mut self, n: i32) -> List<T> { (0..n).map(|_| self.read::<T>()).collect() } pub fn chars(&mut self) -> List<char> { self.read::<String>().chars().collect() } } } pub mod data_structure { pub mod union_find { use crate::arraylist::List; use crate::ext::length::LengthI32; pub struct Utree { n: i32, par: List<i32>, rank: List<i32>, size: List<i32>, } impl Utree { pub fn new(n: i32) -> Utree { Utree { n, par: (0..n).collect(), rank: List::init(0, n), size: List::init(1, n), } } pub fn find(&mut self, x: i32) -> i32 { if self.par[x] == x { x } else { let parent = { self.par[x] }; let ret = self.find(parent); self.par[x] = ret; ret } } pub fn unite(&mut self, mut x: i32, mut y: i32) { x = self.find(x); y = self.find(y); if x == y { return; }; if self.rank[x] < self.rank[y] { self.par[x] = y; self.size[y] += self.size[x]; } else { self.par[y] = x; self.size[x] += self.size[y]; if self.rank[x] == self.rank[y] { self.rank[x] += 1; } } } pub fn is_same(&mut self, mut x: i32, mut y: i32) -> bool { x = self.find(x); y = self.find(y); x == y } pub fn get_size(&mut self, mut x: i32) -> i32 { x = self.find(x); self.size[x] } #[doc = " グループ数(root頂点数)"] pub fn get_group_cnt(&mut self) -> i32 { let mut hset = std::collections::HashSet::new(); for i in 0..self.n { hset.insert(self.find(i)); } hset.ilen() } } } } pub mod arraylist { use crate::{ext::range::IntRangeBounds, independent::integer::Int}; use std::fmt::Formatter; use std::iter::FromIterator; use std::ops::{Index, IndexMut, RangeBounds}; use std::slice::Iter; #[derive(Clone, PartialEq, Eq)] pub struct List<T> { pub vec: Vec<T>, } impl<T> List<T> { #[inline] pub fn new() -> List<T> { List { vec: vec![] } } #[inline] pub fn init(init: T, n: i32) -> List<T> where T: Clone, { List { vec: vec![init; n as usize], } } #[inline] pub fn from_vec(vec: Vec<T>) -> List<T> { List { vec } } #[inline] pub fn ilen(&self) -> i32 { self.vec.len() as i32 } #[inline] pub fn iter(&self) -> Iter<'_, T> { self.vec.iter() } #[inline] pub fn push(&mut self, item: T) { self.vec.push(item); } #[inline] pub fn sort(&mut self) where T: Ord, { self.vec.sort(); } #[inline] pub fn reverse(&mut self) { self.vec.reverse(); } #[inline] pub fn sort_by<F>(&mut self, compare: F) where F: FnMut(&T, &T) -> std::cmp::Ordering, { self.vec.sort_by(compare) } #[inline] pub fn sort_by_key<K, F>(&mut self, compare: F) where F: FnMut(&T) -> K, K: Ord, { self.vec.sort_by_key(compare) } #[inline] pub fn first(&self) -> Option<&T> { self.vec.first() } #[inline] pub fn last(&self) -> Option<&T> { self.vec.last() } #[inline] pub fn pop(&mut self) -> Option<T> { self.vec.pop() } #[inline] pub fn swap(&mut self, i: i32, j: i32) { self.vec.swap(i as usize, j as usize); } #[inline] pub fn append(&mut self, mut other: Self) { self.vec.append(&mut other.vec); } #[inline] pub fn extend(&mut self, other: impl Iterator<Item = T>) { self.vec.extend(other); } #[inline] pub fn mirror(&self) -> std::iter::Cloned<Iter<T>> where T: Clone, { self.iter().cloned() } #[inline] pub fn map<B, F>(&self, f: F) -> List<B> where T: Clone, F: FnMut(T) -> B, { self.mirror().map(f).collect() } #[inline] pub fn filter<P>(&self, predicate: P) -> List<T> where T: Clone, P: FnMut(&T) -> bool, { self.mirror().filter(predicate).collect() } #[inline] pub fn filter_map<B, F>(&self, f: F) -> List<B> where T: Clone, F: FnMut(T) -> Option<B>, { self.mirror().filter_map(f).collect() } #[inline] pub fn any<P>(&self, predicate: P) -> bool where P: FnMut(&T) -> bool, { self.iter().any(predicate) } #[inline] pub fn all<P>(&self, predicate: P) -> bool where P: FnMut(&T) -> bool, { self.iter().all(predicate) } #[inline] pub fn sum(&self) -> T where T: Int, { self.iter().cloned().fold(T::zero(), |acc, x| acc + x) } #[inline] pub fn enumerate(&self) -> List<(i32, T)> where T: Clone, { self.mirror() .enumerate() .map(|p| (p.0 as i32, p.1)) .collect() } #[inline] pub fn find<P>(&self, mut predicate: P) -> Option<&T> where P: FnMut(&T) -> bool, { self.iter().find(|x| predicate(*x)) } #[inline] pub fn index_of<P>(&self, mut predicate: P) -> Option<i32> where P: FnMut(&T) -> bool, { self.iter() .enumerate() .find(|&(_i, x)| predicate(x)) .map(|p| p.0 as i32) } #[inline] pub fn to<B: FromIterator<T>>(&self) -> B where T: Clone, { self.mirror().collect() } #[inline] pub fn min(&self) -> Option<&T> where T: Ord, { self.iter().min() } #[inline] pub fn max(&self) -> Option<&T> where T: Ord, { self.iter().max() } #[inline] pub fn argmin(&self) -> Option<i32> where T: Ord, { let item = self.iter().min()?; self.iter() .enumerate() .find(|p| p.1 == item) .map(|p| p.0 as i32) } #[inline] pub fn argmax(&self) -> Option<i32> where T: Ord, { let item = self.iter().max()?; self.iter() .enumerate() .find(|p| p.1 == item) .map(|p| p.0 as i32) } #[inline] pub fn part<U>(&self, range: U) -> List<T> where T: Clone, U: RangeBounds<i32>, { List::from_vec( self.vec[range.lower_bound(0) as usize..range.upper_bound(self.ilen()) as usize] .to_vec(), ) } #[inline] pub fn first_exn(&self) -> &T { self.first().unwrap() } #[inline] pub fn last_exn(&self) -> &T { self.last().unwrap() } #[inline] pub fn pop_exn(&mut self) -> T { self.pop().unwrap() } #[inline] pub fn min_exn(&self) -> &T where T: Ord, { self.min().unwrap() } #[inline] pub fn max_exn(&self) -> &T where T: Ord, { self.max().unwrap() } #[inline] pub fn argmin_exn(&self) -> i32 where T: Ord, { self.argmin().unwrap() } #[inline] pub fn argmax_exn(&self) -> i32 where T: Ord, { self.argmax().unwrap() } #[inline] pub fn find_exn<P>(&self, predicate: P) -> &T where P: FnMut(&T) -> bool, { self.find(predicate).unwrap() } #[inline] pub fn index_of_exn<P>(&self, predicate: P) -> i32 where P: FnMut(&T) -> bool, { self.index_of(predicate).unwrap() } } impl<T> Index<i32> for List<T> { type Output = T; #[inline] fn index(&self, index: i32) -> &Self::Output { if cfg!(debug_assertions) { self.vec.index(index as usize) } else { unsafe { self.vec.get_unchecked(index as usize) } } } } impl<T> IndexMut<i32> for List<T> { #[inline] fn index_mut(&mut self, index: i32) -> &mut Self::Output { if cfg!(debug_assertions) { self.vec.index_mut(index as usize) } else { unsafe { self.vec.get_unchecked_mut(index as usize) } } } } impl<T> FromIterator<T> for List<T> { fn from_iter<U: IntoIterator<Item = T>>(iter: U) -> Self { let mut vec = vec![]; for i in iter { vec.push(i); } List { vec } } } impl<T> IntoIterator for List<T> { type Item = T; type IntoIter = std::vec::IntoIter<T>; fn into_iter(self) -> std::vec::IntoIter<T> { self.vec.into_iter() } } impl<'a, T> IntoIterator for &'a List<T> { type Item = &'a T; type IntoIter = Iter<'a, T>; fn into_iter(self) -> Iter<'a, T> { self.vec.iter() } } impl<T: std::fmt::Display> std::fmt::Display for List<T> { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { write!( f, "{}", self.iter() .map(|x| format!("{}", x)) .collect::<Vec<_>>() .join(" ") ) } } impl<T: std::fmt::Debug> std::fmt::Debug for List<T> { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!( f, "[{}]", self.iter() .map(|x| format!("{:?}", x)) .collect::<Vec<_>>() .join(", ") ) } } impl<T> From<Vec<T>> for List<T> { fn from(vec: Vec<T>) -> Self { Self::from_vec(vec) } } impl<T: Clone> From<&[T]> for List<T> { fn from(slice: &[T]) -> Self { slice.iter().cloned().collect() } } #[macro_export] macro_rules ! list { ( ) => { $ crate :: arraylist :: List :: new ( ) } ; ( $ v : expr ; $ a : expr ) => { $ crate :: arraylist :: List :: init ( $ v , $ a ) } ; ( $ v : expr ; $ a : expr ; $ ( $ rest : expr ) ;+ ) => { $ crate :: arraylist :: List :: init ( list ! ( $ v ; $ ( $ rest ) ;+ ) , $ a ) } ; } } pub mod independent { pub mod integer { pub trait Int: std::ops::Add<Output = Self> + std::ops::Sub<Output = Self> + std::ops::Mul<Output = Self> + std::ops::Div<Output = Self> + std::ops::Rem<Output = Self> + std::ops::AddAssign + std::ops::SubAssign + std::ops::MulAssign + std::ops::DivAssign + std::hash::Hash + PartialEq + Eq + PartialOrd + Ord + Copy { fn to_u8(&self) -> u8; fn to_u16(&self) -> u16; fn to_u32(&self) -> u32; fn to_u64(&self) -> u64; fn to_u128(&self) -> u128; fn to_i8(&self) -> i8; fn to_i16(&self) -> i16; fn to_i32(&self) -> i32; fn to_i64(&self) -> i64; fn to_i128(&self) -> i128; fn to_usize(&self) -> usize; fn to_isize(&self) -> isize; fn from_u8(x: u8) -> Self; fn from_u16(x: u16) -> Self; fn from_u32(x: u32) -> Self; fn from_u64(x: u64) -> Self; fn from_u128(x: u128) -> Self; fn from_i8(x: i8) -> Self; fn from_i16(x: i16) -> Self; fn from_i32(x: i32) -> Self; fn from_i64(x: i64) -> Self; fn from_i128(x: i128) -> Self; fn from_usize(x: usize) -> Self; fn from_isize(x: isize) -> Self; fn zero() -> Self; fn one() -> Self; fn next(&self) -> Self { *self + Self::one() } } macro_rules ! impl_integer_functions { ( $ selftpe : ident , $ ( $ tofn : ident , $ fromfn : ident , $ tpe : ident ) ,* ) => { $ ( fn $ tofn ( & self ) -> $ tpe { * self as $ tpe } fn $ fromfn ( x : $ tpe ) -> Self { x as $ selftpe } ) * } ; } macro_rules ! impl_integer { ( $ ( $ tpe : ident ) ,* ) => { $ ( impl Int for $ tpe { impl_integer_functions ! ( $ tpe , to_u8 , from_u8 , u8 , to_u16 , from_u16 , u16 , to_u32 , from_u32 , u32 , to_u64 , from_u64 , u64 , to_u128 , from_u128 , u128 , to_i8 , from_i8 , i8 , to_i16 , from_i16 , i16 , to_i32 , from_i32 , i32 , to_i64 , from_i64 , i64 , to_i128 , from_i128 , i128 , to_usize , from_usize , usize , to_isize , from_isize , isize ) ; fn zero ( ) -> Self { 0 } fn one ( ) -> Self { 1 } } ) * } ; } impl_integer!(u8, u16, u32, u64, u128, i8, i16, i32, i64, i128, usize, isize); } }