Submission #39466278


Source Code Expand

// -*- coding:utf-8-unix -*-
// rustup doc --std --toolchain 1.42.0
#![allow(unused_imports)]

pub fn solve() {
    // Initialize.
    use fastproconio::*;
    #[rustfmt::skip] #[cfg(tcheck)] let tins = std::time::Instant::now();
    #[rustfmt::skip] #[cfg(tcheck)] let mut durs = Vec::with_capacity(16);
    let stdin = std::io::stdin();
    let mut source = ProconIBufIter::new(stdin.lock());
    #[rustfmt::skip] #[allow(unused_macros)] macro_rules! finput {($($r:tt)*)=>{finput_inner!{source,$($r)*}};}
    #[rustfmt::skip] #[allow(unused_macros)] macro_rules! fread {($t:tt)=>{{fread_value!(source,$t)}};}
    //let mut obuf = ProconWriteBuffer::with_capacity(1 << 26);
    let mut out = std::io::stdout();
    //let mut out = std::io::BufWriter::with_capacity(out.lock(), 1 << 26);
    //let err = std::io::stderr();
    //let mut err = std::io::BufWriter::with_capacity(err.lock(), 1 << 26);
    #[rustfmt::skip] #[cfg(tcheck)] durs.push((tins.elapsed(), String::from("initialize")));

    // Input. (Only some or no input if you want to input in parallel with the main process.)
    finput! {
        n: usize, m: usize,
        uv: [(u32_1, u32_1); m],
    }
    #[rustfmt::skip] #[cfg(tcheck)] durs.push((tins.elapsed(), String::from("input")));

    // Main Process, Output.

    let mut dsu = Dsu::new(n);

    for &(u, v) in uv.iter() {
        dsu.merge(u as usize, v as usize);
    }

    let leader_or_size_vec = dsu.leader_or_size_vec();
    let mut remains = leader_or_size_vec.clone();

    for &(u, _v) in uv.iter() {
        let l_or_s = leader_or_size_vec[u as usize];
        let leader = if l_or_s < 0 { u as usize } else { l_or_s as usize };
        remains[leader] += 1;
    }

    let result = leader_or_size_vec.iter().enumerate().all(|(i, &l_or_s)| l_or_s >= 0 || remains[i] == 0);
    out.write_all(if result { b"Yes\n" } else { b"No\n" }).ok();
    //std::io::Write::flush(&mut out).ok();
    #[rustfmt::skip] #[cfg(tcheck)] durs.push((tins.elapsed(), String::from("output")));

    // Execution Time.
    #[rustfmt::skip] #[cfg(tcheck)] for (dur, s) in durs.iter() { eprintln!("{:.6} {}", dur.as_secs_f64(), s); };
}

pub fn main() {
    const USE_THREAD: bool = false;
    if USE_THREAD {
        // In order to avoid potential stack overflow, spawn a new thread.
        let stack_size = 134_217_728; // 128 MB
        let thd = std::thread::Builder::new().stack_size(stack_size);
        thd.spawn(|| solve()).unwrap().join().unwrap();
    } else {
        solve()
    }
}

use bitset_fixed::BitSet;
use itertools::*;
use num::integer::*;
use petgraph::algo::*;
use petgraph::graph::{DiGraph, Graph, NodeIndex, UnGraph};
use petgraph::unionfind::UnionFind;
use petgraph::visit::{
    Bfs, Dfs, EdgeRef, IntoEdges, NodeCount, NodeIndexable, VisitMap, Visitable,
};
//use proconio::{input, marker::{Bytes, Chars, Isize1, Usize1}, source::{auto::AutoSource, line::LineSource, once::OnceSource}};
use rand::{
    distributions::WeightedIndex,
    prelude::{thread_rng, Distribution},
    seq::SliceRandom,
    Rng,
};
use rand_core::le;
use regex::Regex;
use rustc_hash::{FxHashMap, FxHashSet, FxHasher};
use std::{
    collections::*,
    convert::{TryFrom, TryInto},
    hash::BuildHasherDefault,
    io::{stderr, stdin, stdout, BufRead, BufReader, BufWriter, Read, Write},
    iter::FromIterator,
};
use superslice::Ext;

/// chmax, chmin sugar syntax
trait Change {
    fn chmax(&mut self, x: Self);
    fn chmin(&mut self, x: Self);
}
impl<T: PartialOrd> Change for T {
    fn chmax(&mut self, x: T) {
        if *self < x {
            *self = x;
        }
    }
    fn chmin(&mut self, x: T) {
        if *self > x {
            *self = x;
        }
    }
}
pub mod fastproconio {
    /// input macros based on tanakh's input macro / proconio-rs.
    /// tanakh's input macro: <https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8>
    /// proconio-rs: <https://docs.rs/proconio/0.3.8/proconio/>
    /// ProconIBufIter receive `std::io::BufRead` trait. (`std::io::StdinLock`, `std::io::BufReader`, `&[u8]`, etc.)
    #[macro_export]
    macro_rules! finput_inner {
        ($source:expr) => {};
        ($source:expr, ) => {};
        ($source:expr, mut $var:ident : $t:tt $($r:tt)*) => {
            let mut $var = fread_value!($source, $t);
            finput_inner!{$source $($r)*}
        };
        ($source:expr, $var:ident : $t:tt $($r:tt)*) => {
            let $var = fread_value!($source, $t);
            finput_inner!{$source $($r)*}
        };
    }
    #[macro_export]
    macro_rules! fread_value {
        ($source:expr, ( $($t:tt),* )) => { ( $(fread_value!($source, $t)),* ) };
        ($source:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| fread_value!($source, $t)).collect::<Vec<_>>() };
        ($source:expr, u128) => { $source.next_wordtoken().as_slice().parse_u128_raw() };
        ($source:expr, usize) => { $source.next_wordtoken().as_slice().parse_u64_raw() as usize };
        ($source:expr, usize1) => { $source.next_wordtoken().as_slice().parse_u64_raw() as usize - 1 };
        ($source:expr, u64) => { $source.next_wordtoken().as_slice().parse_u64_raw() };
        ($source:expr, u64_1) => { $source.next_wordtoken().as_slice().parse_u64_raw() - 1 };
        ($source:expr, u32) => { $source.next_wordtoken().as_slice().parse_u32_raw() };
        ($source:expr, u32_1) => { $source.next_wordtoken().as_slice().parse_u32_raw() - 1 };
        ($source:expr, u16) => { $source.next_wordtoken().as_slice().parse_u16_raw() };
        ($source:expr, u16_1) => { $source.next_wordtoken().as_slice().parse_u16_raw() - 1 };
        ($source:expr, u8) => { $source.next_wordtoken().as_slice().parse_u8_raw() };
        ($source:expr, i128) => { $source.next_wordtoken().as_slice().parse_i128_raw() };
        ($source:expr, isize) => { $source.next_wordtoken().as_slice().parse_i64_raw() as isize };
        ($source:expr, i64) => { $source.next_wordtoken().as_slice().parse_i64_raw() };
        ($source:expr, i32) => { $source.next_wordtoken().as_slice().parse_i32_raw() };
        ($source:expr, i16) => { $source.next_wordtoken().as_slice().parse_i16_raw() };
        ($source:expr, i8) => { $source.next_wordtoken().as_slice().parse_i8_raw() };
        ($source:expr, byte) => { $source.get_ascii_byte() };
        ($source:expr, Bytes) => {{ $source.next_wordtoken().as_vec() }};
        ($source:expr, BytesToken) => {{ $source.next_wordtoken() }};
        ($source:expr, String) => {unsafe { $source.next_wordtoken().as_string_unchecked() }};
        ($source:expr, LineBytes) => {{ $source.next_linetoken().as_vec() }};
        ($source:expr, LineBytesToken) => {{ $source.next_linetoken() }};
        ($source:expr, LineString) => {unsafe { $source.next_linetoken().as_string_unchecked() }};
        ($source:expr, $t:ty) => {{ let mut v = vec![];$source.get_utf8_bytes(&mut v);unsafe { std::string::String::from_utf8_unchecked(v.as_slice()) }.parse::<$t>().expect("Parse error") }};
    }
    unsafe fn ptr_offset_u8(dist: *const u8, origin: *const u8) -> usize {
        // Rust 1.47.0 or later, `dist.offset_from(origin) as usize`
        // <https://doc.rust-lang.org/std/primitive.pointer.html#method.offset_from>
        dist as usize - origin as usize
    }
    #[derive(Clone, Debug)]
    pub enum Token<'a> {
        Slice(&'a [u8]),
        Bytes(Vec<u8>),
    }
    impl Token<'_> {
        pub fn as_slice(&self) -> &[u8] {
            match self {
                Self::Slice(s) => s,
                Self::Bytes(v) => v.as_slice(),
            }
        }
        pub fn as_vec(self) -> Vec<u8> {
            match self {
                Self::Slice(s) => s.to_vec(),
                Self::Bytes(v) => v,
            }
        }
        pub fn as_string(self) -> Result<String, std::string::FromUtf8Error> {
            String::from_utf8(self.as_vec())
        }
        pub unsafe fn as_string_unchecked(self) -> String {
            String::from_utf8_unchecked(self.as_vec())
        }
    }

    /// Interaction with `std::io::BufRead` Trait, Implementation of `Iterator<Item = u8>`
    pub struct ProconIBufIter<R: std::io::BufRead> {
        inner: R,
        raw: *const u8,
        ptr: *const u8,
        end: *const u8,
        len: usize,
        balign: *const u8,
        wmask: Vec<u64>,
    }
    impl<R: std::io::BufRead> ProconIBufIter<R> {
        pub fn new(inner: R) -> Self {
            const EMPTY_U8_SLICE: &'static [u8] = b"";
            Self {
                inner,
                raw: EMPTY_U8_SLICE.as_ptr(),
                ptr: EMPTY_U8_SLICE.as_ptr(),
                end: EMPTY_U8_SLICE.as_ptr(),
                len: 0,
                balign: EMPTY_U8_SLICE.as_ptr(),
                wmask: vec![0u64; 200],
            }
        }
    }
    impl<R: std::io::BufRead> ProconIBufIter<R> {
        pub fn buf_empty(&self) -> bool {
            self.ptr == self.end
        }
        #[allow(clippy::missing_safety_doc)]
        #[cold]
        unsafe fn inner_read(&mut self) -> bool {
            debug_assert_eq!(self.ptr, self.end);
            self.inner.consume(ptr_offset_u8(self.ptr, self.raw));
            if let Ok(s) = self.inner.fill_buf() {
                self.raw = s.as_ptr();
                self.ptr = s.as_ptr();
                self.end = s.as_ptr().add(s.len());
                self.len = s.len();
                self.balign = (self.raw as usize & !0x3f) as *const u8;
                let alignlen = (((self.end as usize) + 0x3f) & (!0x3f)) - self.balign as usize;
                let wmasklen = (alignlen + 63) / 64;
                #[cfg(target_arch = "x86_64")]
                {
                    #[target_feature(enable = "avx2")]
                    unsafe fn genmask_avx2(asl: &[u8], bsl: &mut [u64]) {
                        use std::arch::x86_64::*;
                        let diff = _mm256_set1_epi8(-0x21);
                        for (a, b) in asl.chunks_exact(64).zip(bsl.iter_mut()) {
                            let s0 = _mm256_load_si256(std::mem::transmute(a.as_ptr().add(0)));
                            let s1 = _mm256_load_si256(std::mem::transmute(a.as_ptr().add(32)));
                            let a0 = _mm256_add_epi8(s0, diff);
                            let a1 = _mm256_add_epi8(s1, diff);
                            let m0 = _mm256_movemask_epi8(_mm256_andnot_si256(s0, a0)) as u32;
                            let m1 = _mm256_movemask_epi8(_mm256_andnot_si256(s1, a1)) as u32;
                            *b = ((m1 as u64) << 32) | (m0 as u64);
                        }
                    }
                    unsafe fn genmask_sse2(asl: &[u8], bsl: &mut [u64]) {
                        use std::arch::x86_64::*;
                        let diff = _mm_set1_epi8(-0x21);
                        for (a, b) in asl.chunks_exact(64).zip(bsl.iter_mut()) {
                            let s0 = _mm_load_si128(std::mem::transmute(a.as_ptr().add(0)));
                            let s1 = _mm_load_si128(std::mem::transmute(a.as_ptr().add(16)));
                            let s2 = _mm_load_si128(std::mem::transmute(a.as_ptr().add(32)));
                            let s3 = _mm_load_si128(std::mem::transmute(a.as_ptr().add(48)));
                            let a0 = _mm_add_epi8(s0, diff);
                            let a1 = _mm_add_epi8(s1, diff);
                            let a2 = _mm_add_epi8(s2, diff);
                            let a3 = _mm_add_epi8(s3, diff);
                            let m0 = _mm_movemask_epi8(_mm_andnot_si128(s0, a0)) as u16;
                            let m1 = _mm_movemask_epi8(_mm_andnot_si128(s1, a1)) as u16;
                            let m2 = _mm_movemask_epi8(_mm_andnot_si128(s2, a2)) as u16;
                            let m3 = _mm_movemask_epi8(_mm_andnot_si128(s3, a3)) as u16;
                            *b = ((m3 as u64) << 48)
                                | ((m2 as u64) << 32)
                                | ((m1 as u64) << 16)
                                | (m0 as u64);
                        }
                    }
                    if self.wmask.len() <= wmasklen {
                        self.wmask
                            .extend(std::iter::repeat(0).take(wmasklen + 1 - self.wmask.len()));
                    }
                    let asl = std::slice::from_raw_parts(self.balign, wmasklen * 64);
                    if is_x86_feature_detected!("avx2") {
                        genmask_avx2(asl, &mut self.wmask);
                    } else {
                        genmask_sse2(asl, &mut self.wmask);
                    }
                };
                self.len != 0
            } else {
                self.raw = self.ptr;
                self.len = self.end as usize - self.ptr as usize;
                false
            }
        }
        #[allow(clippy::missing_safety_doc)]
        unsafe fn next_unchecked(&mut self) -> u8 {
            let p = self.ptr;
            self.ptr = p.add(1);
            *p
        }
        /// skip unmatch bytes
        pub fn skipuntil_bytes_fn<F: FnMut(u8) -> bool>(&mut self, f: &mut F) -> bool {
            loop {
                let mut ptr = self.ptr;
                while ptr != self.end {
                    if f(unsafe { *ptr }) {
                        self.ptr = ptr;
                        return true;
                    }
                    unsafe {
                        ptr = ptr.add(1);
                    }
                }
                self.ptr = ptr;
                if unsafe { !self.inner_read() } {
                    return false;
                }
            }
        }
        #[inline]
        pub fn next_wordtoken(&mut self) -> Token {
            if !self.skipuntil_bytes_fn(&mut |c: u8| c > b' ') {
                return Token::Slice(b"");
            }
            #[cfg(target_arch = "x86_64")]
            unsafe {
                let ptr = self.ptr;
                let pdiff = (self.ptr as usize) - (self.balign as usize) + 1;
                let (p64q, p64r) = (pdiff / 64, pdiff % 64);
                let mut w = self.wmask.as_ptr().add(p64q);
                let wmask = (*w) & ((!0u64) << p64r);
                let mut p = self.balign.add(p64q * 64);
                if wmask != 0 {
                    p = p.add(wmask.trailing_zeros() as usize);
                    if p < self.end {
                        self.ptr = p.add(1);
                        return Token::Slice(std::slice::from_raw_parts(
                            ptr,
                            p as usize - ptr as usize,
                        ));
                    }
                }
                p = p.add(64);
                w = w.add(1);
                let end64 = self.end.sub(64);
                while p < end64 {
                    let wmask = *w;
                    if wmask != 0 {
                        let tlz = wmask.trailing_zeros();
                        let pp = p.add(tlz as usize);
                        self.ptr = pp.add(1);
                        return Token::Slice(std::slice::from_raw_parts(
                            ptr,
                            pp as usize - ptr as usize,
                        ));
                    }
                    p = p.add(64);
                    w = w.add(1);
                }
                if p < self.end {
                    let wmask = *w;
                    if wmask != 0 {
                        let tlz = wmask.trailing_zeros();
                        let pp = p.add(tlz as usize);
                        if pp < self.end {
                            self.ptr = pp.add(1);
                            return Token::Slice(std::slice::from_raw_parts(
                                ptr,
                                pp as usize - ptr as usize,
                            ));
                        }
                    }
                }
                let mut v =
                    std::slice::from_raw_parts(ptr, self.end as usize - ptr as usize).to_vec();
                loop {
                    self.ptr = self.end;
                    if !self.inner_read() {
                        return Token::Bytes(v);
                    }
                    let ptr = self.ptr;
                    let pdiff = (ptr as usize) - (self.balign as usize);
                    let (p64q, p64r) = (pdiff / 64, pdiff % 64);
                    let mut w = self.wmask.as_ptr().add(p64q);
                    let mut wmask = (*w) & ((!0u64) << p64r);
                    let mut p = self.balign.add(p64q * 64);
                    while p < self.end {
                        if wmask != 0 {
                            p = p.add(wmask.trailing_zeros() as usize);
                            if p < self.end {
                                self.ptr = p.add(1);
                                v.extend_from_slice(std::slice::from_raw_parts(
                                    ptr,
                                    p as usize - ptr as usize,
                                ));
                                return Token::Bytes(v);
                            }
                            break;
                        }
                        p = p.add(64);
                        w = w.add(1);
                        wmask = *w;
                    }
                    v.extend_from_slice(std::slice::from_raw_parts(
                        ptr,
                        self.end as usize - ptr as usize,
                    ));
                }
            }
            #[cfg(not(target_arch = "x86_64"))]
            unsafe {
                let ptr = self.ptr;
                let mut p = ptr.add(1);
                while p < self.end {
                    if *p <= b' ' {
                        self.ptr = p.add(1);
                        return Token::Slice(std::slice::from_raw_parts(
                            ptr,
                            p as usize - ptr as usize,
                        ));
                    }
                    p = p.add(1);
                }
                let mut v =
                    std::slice::from_raw_parts(ptr, self.end as usize - ptr as usize).to_vec();
                loop {
                    self.ptr = self.end;
                    if !self.inner_read() {
                        break;
                    }
                    let ptr = self.ptr;
                    let mut p = ptr;
                    while p < self.end {
                        if *p <= b' ' {
                            self.ptr = p.add(1);
                            v.extend_from_slice(std::slice::from_raw_parts(
                                ptr,
                                p as usize - ptr as usize,
                            ));
                            return Token::Bytes(v);
                        }
                        p = p.add(1);
                    }
                    v.extend_from_slice(std::slice::from_raw_parts(
                        ptr,
                        self.end as usize - ptr as usize,
                    ));
                }
                return Token::Bytes(v);
            }
        }
        #[inline]
        pub fn next_linetoken(&mut self) -> Token {
            if !self.skipuntil_bytes_fn(&mut |c: u8| c >= b' ') {
                return Token::Slice(b"");
            }
            #[cfg(target_arch = "x86_64")]
            unsafe {
                let ptr = self.ptr;
                let pdiff = (self.ptr as usize) - (self.balign as usize) + 1;
                let (p64q, p64r) = (pdiff / 64, pdiff % 64);
                let mut w = self.wmask.as_ptr().add(p64q);
                let mut wmask = (*w) & ((!0u64) << p64r);
                let mut p = self.balign.add(p64q * 64);
                's: /* ' */ while p < self.end {
                    while wmask != 0 {
                        let tlz = wmask.trailing_zeros();
                        let pp = p.add(tlz as usize);
                        if pp >= self.end {
                            break 's;
                        }
                        if *pp < b' ' {
                            self.ptr = pp.add(1);
                            return Token::Slice(std::slice::from_raw_parts(
                                ptr,
                                pp as usize - ptr as usize,
                            ));
                        }
                        wmask &= wmask.wrapping_sub(1); // elase least one bit
                    }
                    p = p.add(64);
                    w = w.add(1);
                    wmask = *w;
                }
                let mut v =
                    std::slice::from_raw_parts(ptr, self.end as usize - ptr as usize).to_vec();
                loop {
                    self.ptr = self.end;
                    if !self.inner_read() {
                        break;
                    }
                    let ptr = self.ptr;
                    let pdiff = (ptr as usize) - (self.balign as usize);
                    let (p64q, p64r) = (pdiff / 64, pdiff % 64);
                    let mut w = self.wmask.as_ptr().add(p64q);
                    let mut wmask = (*self.wmask.get_unchecked(p64q)) & ((!0u64) << p64r);
                    let mut p = self.balign.add(p64q * 64);
                    'v: /* ' */ while p < self.end {
                        while wmask != 0 {
                            let tlz = wmask.trailing_zeros();
                            let pp = p.add(tlz as usize);
                            if pp >= self.end {
                                break 'v;
                            }
                            assert!(*pp < b' ');
                            if (*pp) < b' ' {
                                self.ptr = pp.add(1);
                                v.extend_from_slice(std::slice::from_raw_parts(
                                    ptr,
                                    pp as usize - ptr as usize,
                                ));
                                return Token::Bytes(v);
                            }
                            wmask &= wmask.wrapping_sub(1); // elase least one bit
                        }
                        p = p.add(64);
                        w = w.add(1);
                        wmask = *w;
                    }
                    v.extend_from_slice(std::slice::from_raw_parts(
                        ptr,
                        self.end as usize - ptr as usize,
                    ));
                }
                return Token::Bytes(v);
            }
            #[cfg(not(target_arch = "x86_64"))]
            unsafe {
                let ptr = self.ptr;
                let mut p = ptr.add(1);
                while p < self.end {
                    if *p < b' ' {
                        self.ptr = p.add(1);
                        return Token::Slice(std::slice::from_raw_parts(
                            ptr,
                            p as usize - ptr as usize,
                        ));
                    }
                    p = p.add(1);
                }
                let mut v =
                    std::slice::from_raw_parts(ptr, self.end as usize - ptr as usize).to_vec();
                loop {
                    self.ptr = self.end;
                    if !self.inner_read() {
                        break;
                    }
                    let ptr = self.ptr;
                    let mut p = ptr;
                    while p < self.end {
                        if *p < b' ' {
                            self.ptr = p.add(1);
                            v.extend_from_slice(std::slice::from_raw_parts(
                                ptr,
                                p as usize - ptr as usize,
                            ));
                            return Token::Bytes(v);
                        }
                        p = p.add(1);
                    }
                    v.extend_from_slice(std::slice::from_raw_parts(
                        ptr,
                        self.end as usize - ptr as usize,
                    ));
                }
                return Token::Bytes(v);
            }
        }
    }
    impl<R: std::io::BufRead> Iterator for ProconIBufIter<R> {
        type Item = u8;
        fn next(&mut self) -> Option<Self::Item> {
            if !self.buf_empty() || unsafe { self.inner_read() } {
                Some(unsafe { self.next_unchecked() })
            } else {
                None
            }
        }
        fn size_hint(&self) -> (usize, Option<usize>) {
            (usize::max_value(), None)
        }
    }
    pub trait UPrimInt:
        Copy
        + Default
        + std::cmp::Ord
        + 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::ops::RemAssign
        + std::ops::Shl<u32, Output = Self>
        + std::ops::Shr<u32, Output = Self>
        + std::ops::ShlAssign<u32>
        + std::ops::ShrAssign<u32>
        + std::ops::BitAnd<Output = Self>
        + std::ops::BitOr<Output = Self>
        + std::ops::BitXor<Output = Self>
        + std::ops::BitAndAssign
        + std::ops::BitOrAssign
        + std::ops::BitXorAssign
        + std::convert::From<u8>
    {
        const BITS: u32;
        fn count_zeros(self) -> u32;
        fn trailing_zeros(self) -> u32;
        fn leading_zeros(self) -> u32;
    }
    macro_rules! impl_uprimint {
        ($t:ty) => {
            impl UPrimInt for $t {
                const BITS: u32 = (0 as $t).count_zeros();
                fn count_zeros(self) -> u32 {
                    self.count_zeros()
                }
                fn trailing_zeros(self) -> u32 {
                    self.trailing_zeros()
                }
                fn leading_zeros(self) -> u32 {
                    self.leading_zeros()
                }
            }
        };
    }
    impl_uprimint!(u8);
    impl_uprimint!(u16);
    impl_uprimint!(u32);
    impl_uprimint!(u64);
    impl_uprimint!(u128);
    impl_uprimint!(usize);
    pub trait IPrimInt:
        Copy
        + Default
        + std::cmp::Ord
        + std::ops::Add<Output = Self>
        + std::ops::Sub<Output = Self>
        + std::ops::Neg<Output = Self>
        + std::ops::Mul<Output = Self>
        + std::ops::Div<Output = Self>
        + std::ops::Rem<Output = Self>
        + std::convert::From<i8>
    {
        const BITS: u32;
    }
    macro_rules! impl_iprimint {
        ($t:ty) => {
            impl IPrimInt for $t {
                const BITS: u32 = (0 as $t).count_zeros();
            }
        };
    }
    impl_iprimint!(i8);
    impl_iprimint!(i16);
    impl_iprimint!(i32);
    impl_iprimint!(i64);
    impl_iprimint!(i128);
    impl_iprimint!(isize);

    #[inline]
    fn parseuint_arith8le(mut a: u64) -> u64 {
        a = (a & 0x0f0f0f0f0f0f0f0f).wrapping_mul((10 << 8) + 1) >> 8;
        a = (a & 0x00ff00ff00ff00ff).wrapping_mul((100 << 16) + 1) >> 16;
        (a & 0x0000ffff0000ffff).wrapping_mul((10000 << 32) + 1) >> 32
    }
    #[inline]
    #[allow(unused)]
    fn parseuint_arith8be(mut a: u64) -> u64 {
        a = (a & 0x0f0f0f0f0f0f0f0f).wrapping_mul((1 << 8) + 10) >> 8;
        a = (a & 0x00ff00ff00ff00ff).wrapping_mul((1 << 16) + 100) >> 16;
        (a & 0x0000ffff0000ffff).wrapping_mul((1 << 32) + 10000) >> 32
    }
    #[inline]
    fn parseuint_raw32b(s: [u8; 32]) -> u128 {
        use std::convert::TryInto;
        let a = parseuint_arith8le(u64::from_le_bytes((&s[0..8]).try_into().unwrap()));
        let b = parseuint_arith8le(u64::from_le_bytes((&s[8..16]).try_into().unwrap()));
        let c = parseuint_arith8le(u64::from_le_bytes((&s[16..24]).try_into().unwrap()));
        let d = parseuint_arith8le(u64::from_le_bytes((&s[24..32]).try_into().unwrap()));
        ((a * 100000000 + b) as u128) * 10000000000000000 + ((c * 100000000 + d) as u128)
    }
    #[inline]
    fn parseuint_raw16b(s: [u8; 16]) -> u64 {
        use std::convert::TryInto;
        let a = parseuint_arith8le(u64::from_le_bytes((&s[0..8]).try_into().unwrap()));
        let b = parseuint_arith8le(u64::from_le_bytes((&s[8..16]).try_into().unwrap()));
        a * 100000000 + b
    }
    #[inline]
    fn parseuint_raw8b(s: [u8; 8]) -> u64 {
        parseuint_arith8le(u64::from_le_bytes(s))
    }
    #[inline]
    fn parseuint_raw8bf(s: &[u8]) -> u64 {
        let l = s.len();
        debug_assert!(l <= 8);
        let l8 = 8 - l;
        let l88 = l8 * 8;
        parseuint_arith8le(unsafe {
            let s_ptr = s.as_ptr();
            if l == 0 {
                0
            } else if (s_ptr as usize) & 0xff8 < 0xff8 {
                u64::from_le_bytes(*(s_ptr as *const [u8; 8])) << l88
            } else {
                u64::from_le_bytes(*(s_ptr.sub(l8) as *const [u8; 8])) >> l88 << l88
            }
        })
    }
    #[inline]
    fn parseuint_raw4bf(s: &[u8]) -> u32 {
        let l = s.len();
        debug_assert!(l <= 4);
        let l4 = 4 - l;
        let l48 = l4 * 8;
        let mut a = unsafe {
            let s_ptr = s.as_ptr();
            if l == 0 {
                0
            } else if (s_ptr as usize) & 0xffc < 0xffc {
                u32::from_le_bytes(*(s_ptr as *const [u8; 4])) << l48
            } else {
                u32::from_le_bytes(*(s_ptr.sub(l4) as *const [u8; 4])) >> l48 << l48
            }
        };
        a = (a & 0x0f0f0f0f).wrapping_mul((10 << 8) + 1) >> 8;
        a = (a & 0x00ff00ff).wrapping_mul((100 << 16) + 1) >> 16;
        a
    }
    #[inline]
    fn parseuint_raw2bf(s: &[u8]) -> u16 {
        let l = s.len();
        debug_assert!(l <= 2);
        let l2 = 2 - l;
        let l28 = l2 * 8;
        let mut a = unsafe {
            let s_ptr = s.as_ptr();
            if l == 0 {
                0
            } else if (s_ptr as usize) & 0xffe < 0xffe {
                u16::from_le_bytes(*(s_ptr as *const [u8; 2])) << l28
            } else {
                u16::from_le_bytes(*(s_ptr.sub(l2) as *const [u8; 2])) >> l28 << l28
            }
        };
        a = (a & 0x0f0f).wrapping_mul((10 << 8) + 1) >> 8;
        a
    }
    pub trait ByteParseIntRaw {
        fn parse_u128_raw(&self) -> u128;
        fn parse_u64_raw(&self) -> u64;
        fn parse_u32_raw(&self) -> u32;
        fn parse_u16_raw(&self) -> u16;
        fn parse_u8_raw(&self) -> u8;
        fn parse_i128_raw(&self) -> i128;
        fn parse_i64_raw(&self) -> i64;
        fn parse_i32_raw(&self) -> i32;
        fn parse_i16_raw(&self) -> i16;
        fn parse_i8_raw(&self) -> i8;
        fn parse_u128_rawopt(&self) -> Option<u128>;
        fn parse_u64_rawopt(&self) -> Option<u64>;
        fn parse_u32_rawopt(&self) -> Option<u32>;
        fn parse_u16_rawopt(&self) -> Option<u16>;
        fn parse_u8_rawopt(&self) -> Option<u8>;
    }
    impl ByteParseIntRaw for &[u8] {
        // parse_u128_raw: empty or int(self) > u128::MAX or self.len() > 39 or !self.iter().all(u8::is_ascii_digit) will cause Undefined Behavior.
        fn parse_u128_raw(&self) -> u128 {
            use std::convert::TryInto;
            debug_assert!(!self.is_empty());
            debug_assert!(self.len() <= 39);
            debug_assert!(self.iter().all(u8::is_ascii_digit));
            let l = self.len();
            if l > 32 {
                let (upper, lower) = self.split_at(l - 32);
                parseuint_raw8bf(upper) as u128 * 100000000000000000000000000000000
                    + parseuint_raw32b(lower.try_into().unwrap()) as u128
            } else if l > 24 {
                let (upper, t24) = self.split_at(l - 24);
                let (middle, lower) = t24.split_at(8);
                (parseuint_raw8bf(upper) * 100000000 + parseuint_raw8b(middle.try_into().unwrap()))
                    as u128
                    * 10000000000000000
                    + parseuint_raw16b(lower.try_into().unwrap()) as u128
            } else if l > 16 {
                let (upper, lower) = self.split_at(l - std::mem::size_of::<u128>());
                (parseuint_raw8bf(upper) as u128) * 10000000000000000
                    + parseuint_raw16b(lower.try_into().unwrap()) as u128
            } else if l > 8 {
                let (upper, lower) = self.split_at(l - std::mem::size_of::<u64>());
                (parseuint_raw8bf(upper) * 100000000 + parseuint_raw8b(lower.try_into().unwrap()))
                    as u128
            } else {
                parseuint_raw8bf(self) as u128
            }
        }
        // parse_u64_raw: empty or int(self) > u64::MAX or self.len() > 20 or !self.iter().all(u8::is_ascii_digit) will cause Undefined Behavior.
        fn parse_u64_raw(&self) -> u64 {
            use std::convert::TryInto;
            debug_assert!(!self.is_empty());
            debug_assert!(self.len() <= 20);
            debug_assert!(self.iter().all(u8::is_ascii_digit));
            let l = self.len();
            if l > 16 {
                let (upper, lower) = self.split_at(l - std::mem::size_of::<u128>());
                (parseuint_raw4bf(upper) as u64) * 10000000000000000
                    + parseuint_raw16b(lower.try_into().unwrap())
            } else if l > 8 {
                let (upper, lower) = self.split_at(l - std::mem::size_of::<u64>());
                (parseuint_raw8bf(upper) * 100000000 + parseuint_raw8b(lower.try_into().unwrap()))
                    as u64
            } else {
                parseuint_raw8bf(self) as u64
            }
        }
        // parse_u32_raw: empty or int(self) > u32::MAX or self.len() > 10 or !self.iter().all(u8::is_ascii_digit) will cause Undefined Behavior.
        fn parse_u32_raw(&self) -> u32 {
            use std::convert::TryInto;
            debug_assert!(!self.is_empty());
            debug_assert!(self.len() <= 10);
            debug_assert!(self.iter().all(u8::is_ascii_digit));
            let l = self.len();
            if l > 8 {
                let (upper, lower) = self.split_at(l - std::mem::size_of::<u64>());
                (parseuint_raw2bf(upper) as u32) * 100000000
                    + parseuint_raw8b(lower.try_into().unwrap()) as u32
            } else {
                parseuint_raw8bf(self) as u32
            }
        }
        // parse_u16_raw: empty or int(self) > u16::MAX or self.len() > 5 or !self.iter().all(u8::is_ascii_digit) will cause Undefined Behavior.
        fn parse_u16_raw(&self) -> u16 {
            debug_assert!(!self.is_empty());
            debug_assert!(self.len() <= 5);
            debug_assert!(self.iter().all(u8::is_ascii_digit));
            parseuint_raw8bf(self) as u16
        }
        // parse_u8_raw: empty or int(self) > u8::MAX or self.len() > 3 or !self.iter().all(u8::is_ascii_digit) will cause Undefined Behavior.
        fn parse_u8_raw(&self) -> u8 {
            debug_assert!(!self.is_empty());
            debug_assert!(self.len() <= 3);
            debug_assert!(self.iter().all(u8::is_ascii_digit));
            parseuint_raw4bf(self) as u8
        }
        fn parse_i128_raw(&self) -> i128 {
            debug_assert!(!self.is_empty());
            if self.is_empty() {
                0
            } else if self[0] == b'-' {
                (&self[1..]).parse_u128_raw().wrapping_neg() as i128
            } else {
                self.parse_u128_raw() as i128
            }
        }
        fn parse_i64_raw(&self) -> i64 {
            debug_assert!(!self.is_empty());
            if self.is_empty() {
                0
            } else if self[0] == b'-' {
                (&self[1..]).parse_u64_raw().wrapping_neg() as i64
            } else {
                self.parse_u64_raw() as i64
            }
        }
        fn parse_i32_raw(&self) -> i32 {
            debug_assert!(!self.is_empty());
            if self.is_empty() {
                0
            } else if self[0] == b'-' {
                (&self[1..]).parse_u32_raw().wrapping_neg() as i32
            } else {
                self.parse_u32_raw() as i32
            }
        }
        fn parse_i16_raw(&self) -> i16 {
            debug_assert!(!self.is_empty());
            if self.is_empty() {
                0
            } else if self[0] == b'-' {
                (&self[1..]).parse_u16_raw().wrapping_neg() as i16
            } else {
                self.parse_u16_raw() as i16
            }
        }
        fn parse_i8_raw(&self) -> i8 {
            debug_assert!(!self.is_empty());
            if self.is_empty() {
                0
            } else if self[0] == b'-' {
                (&self[1..]).parse_u128_raw().wrapping_neg() as i8
            } else {
                self.parse_u128_raw() as i8
            }
        }
        // parse_u128_rawopt: empty or int(self) > u128::MAX or self.len() > 39 or !self.iter().all(u8::is_ascii_digit) will cause Undefined Behavior.
        fn parse_u128_rawopt(&self) -> Option<u128> {
            use std::convert::TryInto;
            debug_assert!(!self.is_empty());
            debug_assert!(self.len() <= 39);
            debug_assert!(self.iter().all(u8::is_ascii_digit));
            let l = self.len();
            if l > 32 {
                let (upper, lower) = self.split_at(l - 32);
                (parseuint_raw8bf(upper) as u128)
                    .checked_mul(100000000000000000000000000000000)?
                    .checked_add(parseuint_raw32b(lower.try_into().unwrap()) as u128)
            } else if l > 24 {
                let (upper, t24) = self.split_at(l - 24);
                let (middle, lower) = t24.split_at(8);
                Some(
                    (parseuint_raw8bf(upper) * 100000000
                        + parseuint_raw8b(middle.try_into().unwrap())) as u128
                        * 10000000000000000
                        + parseuint_raw16b(lower.try_into().unwrap()) as u128,
                )
            } else if l > 16 {
                let (upper, lower) = self.split_at(l - std::mem::size_of::<u128>());
                Some(
                    (parseuint_raw8bf(upper) as u128) * 10000000000000000
                        + parseuint_raw16b(lower.try_into().unwrap()) as u128,
                )
            } else if l > 8 {
                let (upper, lower) = self.split_at(l - std::mem::size_of::<u64>());
                Some(
                    (parseuint_raw8bf(upper) * 100000000
                        + parseuint_raw8b(lower.try_into().unwrap())) as u128,
                )
            } else {
                Some(parseuint_raw8bf(self) as u128)
            }
        }
        // parse_u64_rawopt: empty or int(self) > u64::MAX or self.len() > 20 or !self.iter().all(u8::is_ascii_digit) will cause Undefined Behavior.
        fn parse_u64_rawopt(&self) -> Option<u64> {
            use std::convert::TryInto;
            debug_assert!(!self.is_empty());
            debug_assert!(self.len() <= 20);
            debug_assert!(self.iter().all(u8::is_ascii_digit));
            let l = self.len();
            if l > 16 {
                let (upper, lower) = self.split_at(l - std::mem::size_of::<u128>());
                (parseuint_raw4bf(upper) as u64)
                    .checked_mul(10000000000000000)?
                    .checked_add(parseuint_raw16b(lower.try_into().unwrap()))
            } else if l > 8 {
                let (upper, lower) = self.split_at(l - std::mem::size_of::<u64>());
                Some(
                    (parseuint_raw8bf(upper) * 100000000
                        + parseuint_raw8b(lower.try_into().unwrap())) as u64,
                )
            } else {
                Some(parseuint_raw8bf(self) as u64)
            }
        }
        // parse_u32_rawopt: empty or int(self) > u32::MAX or self.len() > 10 or !self.iter().all(u8::is_ascii_digit) will cause Undefined Behavior.
        fn parse_u32_rawopt(&self) -> Option<u32> {
            use std::convert::TryInto;
            debug_assert!(!self.is_empty());
            debug_assert!(self.len() <= 10);
            debug_assert!(self.iter().all(u8::is_ascii_digit));
            let l = self.len();
            if l > 8 {
                let (upper, lower) = self.split_at(l - std::mem::size_of::<u64>());
                (parseuint_raw2bf(upper) as u32)
                    .checked_mul(100000000)?
                    .checked_add(parseuint_raw8b(lower.try_into().unwrap()) as u32)
            } else {
                Some(parseuint_raw8bf(self) as u32)
            }
        }
        // parse_u16_rawopt: empty or int(self) > u16::MAX or self.len() > 5 or !self.iter().all(u8::is_ascii_digit) will cause Undefined Behavior.
        fn parse_u16_rawopt(&self) -> Option<u16> {
            use std::convert::TryInto;
            debug_assert!(!self.is_empty());
            debug_assert!(self.len() <= 5);
            debug_assert!(self.iter().all(u8::is_ascii_digit));
            parseuint_raw8bf(self).try_into().ok()
        }
        // parse_u8_rawopt: empty or int(self) > u8::MAX or self.len() > 3 or !self.iter().all(u8::is_ascii_digit) will cause Undefined Behavior.
        fn parse_u8_rawopt(&self) -> Option<u8> {
            use std::convert::TryInto;
            debug_assert!(!self.is_empty());
            debug_assert!(self.len() <= 3);
            debug_assert!(self.iter().all(u8::is_ascii_digit));
            parseuint_raw4bf(self).try_into().ok()
        }
    }
    /// speed frenzy input parser for program compete
    pub trait ProconParse {
        fn get_ascii_byte(&mut self) -> u8 {
            self.get_ascii_byte_opt().unwrap()
        }
        fn get_ascii_byte_or_default(&mut self) -> u8 {
            self.get_ascii_byte_opt().unwrap_or_default()
        }
        fn get_ascii_byte_opt(&mut self) -> Option<u8>;
        fn parse_uint<U: UPrimInt>(&mut self) -> U {
            self.parse_uint_opt().unwrap()
        }
        fn parse_uint_or_default<U: UPrimInt>(&mut self) -> U {
            self.parse_uint_opt().unwrap_or_default()
        }
        fn parse_uint_opt<U: UPrimInt>(&mut self) -> Option<U>;
        fn parse_iint<I: IPrimInt>(&mut self) -> I {
            self.parse_iint_opt().unwrap()
        }
        fn parse_iint_or_default<I: IPrimInt>(&mut self) -> I {
            self.parse_iint_opt().unwrap_or_default()
        }
        fn parse_iint_opt<I: IPrimInt>(&mut self) -> Option<I>;
    }
    impl<T: Iterator<Item = u8>> ProconParse for T {
        fn get_ascii_byte_opt(&mut self) -> Option<u8> {
            loop {
                match self.next() {
                    Some(c @ 0x21..=0x7e) => {
                        return Some(c);
                    }
                    Some(_) => continue,
                    _ => return None,
                }
            }
        }
        fn parse_uint_opt<U: UPrimInt>(&mut self) -> Option<U> {
            loop {
                match self.next() {
                    Some(c @ b'0'..=b'9') => {
                        let mut v = U::from(c - b'0');
                        while let Some(c @ b'0'..=b'9') = self.next() {
                            v = v * U::from(10) + U::from(c - b'0');
                        }
                        return Some(v);
                    }
                    Some(_) => continue,
                    _ => return None,
                }
            }
        }
        fn parse_iint_opt<I: IPrimInt>(&mut self) -> Option<I> {
            loop {
                match self.next() {
                    Some(c @ b'0'..=b'9') => {
                        let mut v = I::from((c - b'0') as i8);
                        while let Some(c @ b'0'..=b'9') = self.next() {
                            v = v * I::from(10) + I::from((c - b'0') as i8);
                        }
                        return Some(v);
                    }
                    Some(b'-') => match self.next() {
                        Some(c @ b'0'..=b'9') => {
                            let mut v = I::from(-((c - b'0') as i8));
                            while let Some(c @ b'0'..=b'9') = self.next() {
                                v = v * I::from(10) - I::from((c - b'0') as i8);
                            }
                            return Some(v);
                        }
                        _ => return None,
                    },
                    Some(_) => continue,
                    _ => return None,
                }
            }
        }
    }
    impl<R: std::io::BufRead> Drop for ProconIBufIter<R> {
        /// Saving the pointer on interruption
        fn drop(&mut self) {
            self.inner
                .consume(unsafe { ptr_offset_u8(self.ptr, self.raw) });
        }
    }
    /// Insufficient write buffer size causes undefined operation.
    pub struct ProconWriteBuffer(*mut u8, Vec<u8>);
    impl ProconWriteBuffer {
        pub fn with_capacity(capacity: usize) -> Self {
            let mut b = Vec::<u8>::with_capacity(capacity);
            let ptr = b.as_mut_ptr();
            Self(ptr, b)
        }
        pub fn get_mut_ptr(&self) -> *mut u8 {
            self.0
        }
        pub fn set_mut_ptr(&mut self, p: *mut u8) {
            self.0 = p;
        }
        fn decision(&mut self) {
            let bptr = self.1.as_mut_ptr();
            unsafe { self.1.set_len((self.0 as usize) - (bptr as usize)) };
        }
        pub fn clear(&mut self) {
            self.1.clear();
            self.0 = self.1.as_mut_ptr();
        }
        pub fn get_slice(&mut self) -> &[u8] {
            self.decision();
            self.1.as_slice()
        }
        pub fn reserve(&mut self, additional: usize) {
            self.decision();
            self.1.reserve(additional);
            self.0 = self.1.as_mut_ptr();
        }
        pub fn reserve_exact(&mut self, additional: usize) {
            self.decision();
            self.1.reserve_exact(additional);
            self.0 = self.1.as_mut_ptr();
        }
        pub fn uint<U>(&mut self, d: U)
        where
            U: UPrimInt + std::convert::Into<u128>,
        {
            proconwritebuf_uint(&mut self.0, d);
        }
        pub fn uint_sp<U>(&mut self, s: &[U])
        where
            U: UPrimInt + std::convert::Into<u128>,
        {
            let mut p = self.0;
            let mut it = s.iter();
            if let Some(&d) = it.next() {
                proconwritebuf_uint(&mut p, d);
                for &d in it {
                    proconwritebuf_sp(&mut p);
                    proconwritebuf_uint(&mut p, d);
                }
            }
            self.0 = p;
        }
        pub fn uint_splf<U>(&mut self, s: &[U])
        where
            U: UPrimInt + std::convert::Into<u128>,
        {
            let mut p = self.0;
            let mut it = s.iter();
            if let Some(&d) = it.next() {
                proconwritebuf_uint(&mut p, d);
                for &d in it {
                    proconwritebuf_sp(&mut p);
                    proconwritebuf_uint(&mut p, d);
                }
            }
            proconwritebuf_lf(&mut p);
            self.0 = p;
        }
        pub fn usize(&mut self, d: usize) {
            proconwritebuf_uint(&mut self.0, d as u64);
        }
        pub fn usize_sp(&mut self, s: &[usize]) {
            let mut p = self.0;
            let mut it = s.iter();
            if let Some(&d) = it.next() {
                proconwritebuf_uint(&mut p, d as u64);
                for &d in it {
                    proconwritebuf_sp(&mut p);
                    proconwritebuf_uint(&mut p, d as u64);
                }
            }
            self.0 = p;
        }
        pub fn usize_splf(&mut self, s: &[usize]) {
            let mut p = self.0;
            let mut it = s.iter();
            if let Some(&d) = it.next() {
                proconwritebuf_uint(&mut p, d as u64);
                for &d in it {
                    proconwritebuf_sp(&mut p);
                    proconwritebuf_uint(&mut p, d as u64);
                }
            }
            proconwritebuf_lf(&mut p);
            self.0 = p;
        }
        pub fn iint<I>(&mut self, d: I)
        where
            I: IPrimInt + std::convert::Into<i128>,
        {
            proconwritebuf_iint(&mut self.0, d);
        }
        pub fn iint_sp<I>(&mut self, s: &[I])
        where
            I: IPrimInt + std::convert::Into<i128>,
        {
            let mut p = self.0;
            let mut it = s.iter();
            if let Some(&d) = it.next() {
                proconwritebuf_iint(&mut p, d);
                for &d in it {
                    proconwritebuf_sp(&mut p);
                    proconwritebuf_iint(&mut p, d);
                }
            }
            self.0 = p;
        }
        pub fn iint_splf<I>(&mut self, s: &[I])
        where
            I: IPrimInt + std::convert::Into<i128> + std::convert::TryInto<u8>,
        {
            let mut p = self.0;
            let mut it = s.iter();
            if let Some(&d) = it.next() {
                proconwritebuf_iint(&mut p, d);
                for &d in it {
                    proconwritebuf_sp(&mut p);
                    proconwritebuf_iint(&mut p, d);
                }
            }
            proconwritebuf_lf(&mut p);
            self.0 = p;
        }
        pub fn sp(&mut self) {
            proconwritebuf_sp(&mut self.0);
        }
        pub fn lf(&mut self) {
            proconwritebuf_lf(&mut self.0);
        }
        pub fn bytes(&mut self, s: &[u8]) {
            proconwritebuf_bytes(&mut self.0, s);
        }
        pub fn str(&mut self, s: &str) {
            proconwritebuf_str(&mut self.0, s);
        }
        pub fn string(&mut self, s: &String) {
            proconwritebuf_string(&mut self.0, s);
        }
        pub fn write_all<W>(&mut self, out: &mut W)
        where
            W: std::io::Write,
        {
            self.decision();
            let _ = out.write_all(self.1.as_slice());
            self.1.clear();
            self.0 = self.1.as_mut_ptr();
        }
    }
    pub fn proconwritebuf_uint<U>(p: &mut *mut u8, mut d: U)
    where
        U: UPrimInt + std::convert::Into<u128>,
    {
        unsafe {
            let bptr = *p;
            let mut cptr = bptr;
            if d != U::from(0) {
                while d != U::from(0) {
                    let (q, r) = (d / U::from(10), d % U::from(10));
                    d = q;
                    *cptr = b'0' + U::into(r) as u8;
                    cptr = cptr.add(1);
                }
                *p = cptr;
                let mut lptr = bptr;
                let mut rptr = cptr.sub(1);
                while (lptr as usize) < (rptr as usize) {
                    let (dr, dl) = (*lptr, *rptr);
                    *lptr = dl;
                    *rptr = dr;
                    lptr = lptr.add(1);
                    rptr = rptr.sub(1);
                }
            } else {
                *cptr = b'0';
                *p = cptr.add(1);
            }
        };
    }
    pub fn proconwritebuf_iint<I>(p: &mut *mut u8, mut d: I)
    where
        I: IPrimInt + std::convert::Into<i128>,
    {
        unsafe {
            let bptr = *p;
            let mut cptr = bptr;
            if d > I::from(0) {
                while d != I::from(0) {
                    let (q, r) = (d / I::from(10), d % I::from(10));
                    d = q;
                    *cptr = b'0' + I::into(r) as u8;
                    cptr = cptr.add(1);
                }
                *p = cptr;
                let mut lptr = bptr;
                let mut rptr = cptr.sub(1);
                while (lptr as usize) < (rptr as usize) {
                    let (dr, dl) = (*lptr, *rptr);
                    *lptr = dl;
                    *rptr = dr;
                    lptr = lptr.add(1);
                    rptr = rptr.sub(1);
                }
            } else if d < I::from(0) {
                *cptr = b'-';
                cptr = cptr.add(1);
                let mptr = cptr;
                {
                    let (q, r) = (-(d / I::from(10)), -(d % I::from(10)));
                    d = q;
                    *cptr = b'0' + I::into(r) as u8;
                    cptr = cptr.add(1);
                }
                while d != I::from(0) {
                    let (q, r) = (d / I::from(10), d % I::from(10));
                    d = q;
                    *cptr = b'0' + I::into(r) as u8;
                    cptr = cptr.add(1);
                }
                *p = cptr;
                let mut lptr = mptr;
                let mut rptr = cptr.sub(1);
                while (lptr as usize) < (rptr as usize) {
                    let (dr, dl) = (*lptr, *rptr);
                    *lptr = dl;
                    *rptr = dr;
                    lptr = lptr.add(1);
                    rptr = rptr.sub(1);
                }
            } else {
                *cptr = b'0';
                *p = cptr.add(1);
            }
        };
    }
    pub fn proconwritebuf_sp(p: &mut *mut u8) {
        *p = unsafe {
            **p = b' ';
            (*p).add(1)
        }
    }
    pub fn proconwritebuf_lf(p: &mut *mut u8) {
        *p = unsafe {
            **p = b'\n';
            (*p).add(1)
        }
    }
    pub fn proconwritebuf_bytes(p: &mut *mut u8, bytes: &[u8]) {
        *p = unsafe {
            let len = bytes.len();
            std::ptr::copy_nonoverlapping(bytes.as_ptr(), *p, len);
            (*p).add(len)
        };
    }
    pub fn proconwritebuf_str(p: &mut *mut u8, s: &str) {
        *p = unsafe {
            let len = s.len();
            std::ptr::copy_nonoverlapping(s.as_ptr(), *p, len);
            (*p).add(len)
        };
    }
    pub fn proconwritebuf_string(p: &mut *mut u8, s: &String) {
        *p = unsafe {
            let len = s.len();
            std::ptr::copy_nonoverlapping(s.as_ptr(), *p, len);
            (*p).add(len)
        };
    }
}
//https://github.com/rust-lang-ja/ac-library-rs

pub mod dsu {
    //! A Disjoint set union (DSU) with union by size and path compression.

    /// A Disjoint set union (DSU) with union by size and path compression.
    ///
    /// See: [Zvi Galil and Giuseppe F. Italiano, Data structures and algorithms for disjoint set union problems](https://core.ac.uk/download/pdf/161439519.pdf)
    ///
    /// In the following documentation, let $n$ be the size of the DSU.
    ///
    /// # Example
    ///
    /// ```
    /// use ac_library_rs::Dsu;
    /// use proconio::{input, source::once::OnceSource};
    ///
    /// input! {
    ///     from OnceSource::from(
    ///         "5\n\
    ///          3\n\
    ///          0 1\n\
    ///          2 3\n\
    ///          3 4\n",
    ///     ),
    ///     n: usize,
    ///     abs: [(usize, usize)],
    /// }
    ///
    /// let mut dsu = Dsu::new(n);
    /// for (a, b) in abs {
    ///     dsu.merge(a, b);
    /// }
    ///
    /// assert!(dsu.same(0, 1));
    /// assert!(!dsu.same(1, 2));
    /// assert!(dsu.same(2, 4));
    ///
    /// assert_eq!(
    ///     dsu.groups()
    ///         .into_iter()
    ///         .map(|mut group| {
    ///             group.sort_unstable();
    ///             group
    ///         })
    ///         .collect::<Vec<_>>(),
    ///     [&[0, 1][..], &[2, 3, 4][..]],
    /// );
    /// ```
    pub struct Dsu {
        n: usize,
        // root node: -1 * component size
        // otherwise: parent
        parent_or_size: Vec<i32>,
    }

    impl Dsu {
        /// Creates a new `Dsu`.
        ///
        /// # Constraints
        ///
        /// - $0 \leq n \leq 10^8$
        ///
        /// # Complexity
        ///
        /// - $O(n)$
        pub fn new(size: usize) -> Self {
            Self {
                n: size,
                parent_or_size: vec![-1; size],
            }
        }

        // `\textsc` does not work in KaTeX
        /// Performs the Uɴɪᴏɴ operation.
        ///
        /// # Constraints
        ///
        /// - $0 \leq a < n$
        /// - $0 \leq b < n$
        ///
        /// # Panics
        ///
        /// Panics if the above constraints are not satisfied.
        ///
        /// # Complexity
        ///
        /// - $O(\alpha(n))$ amortized
        pub fn merge(&mut self, a: usize, b: usize) -> usize {
            debug_assert!(a < self.n);
            debug_assert!(b < self.n);
            let (mut x, mut y) = (self.leader(a), self.leader(b));
            if x == y {
                return x;
            }
            if -self.parent_or_size[x] < -self.parent_or_size[y] {
                std::mem::swap(&mut x, &mut y);
            }
            self.parent_or_size[x] += self.parent_or_size[y];
            self.parent_or_size[y] = x as i32;
            x
        }

        /// Returns whether the vertices $a$ and $b$ are in the same connected component.
        ///
        /// # Constraints
        ///
        /// - $0 \leq a < n$
        /// - $0 \leq b < n$
        ///
        /// # Panics
        ///
        /// Panics if the above constraint is not satisfied.
        ///
        /// # Complexity
        ///
        /// - $O(\alpha(n))$ amortized
        pub fn same(&mut self, a: usize, b: usize) -> bool {
            debug_assert!(a < self.n);
            debug_assert!(b < self.n);
            self.leader(a) == self.leader(b)
        }

        /// Performs the Fɪɴᴅ operation.
        ///
        /// # Constraints
        ///
        /// - $0 \leq a < n$
        ///
        /// # Panics
        ///
        /// Panics if the above constraint is not satisfied.
        ///
        /// # Complexity
        ///
        /// - $O(\alpha(n))$ amortized
        pub fn leader(&mut self, a: usize) -> usize {
            debug_assert!(a < self.n);
            if self.parent_or_size[a] < 0 {
                return a;
            }
            self.parent_or_size[a] = self.leader(self.parent_or_size[a] as usize) as i32;
            self.parent_or_size[a] as usize
        }

        pub fn leader_or_size_vec(&mut self) -> Vec<i32> {
            for a in 0..self.n {
                if self.parent_or_size[a] < 0 {
                    continue;
                }
                self.parent_or_size[a] = self.leader(self.parent_or_size[a] as usize) as i32;
            }
            self.parent_or_size.clone()
        }

        /// Returns the size of the connected component that contains the vertex $a$.
        ///
        /// # Constraints
        ///
        /// - $0 \leq a < n$
        ///
        /// # Panics
        ///
        /// Panics if the above constraint is not satisfied.
        ///
        /// # Complexity
        ///
        /// - $O(\alpha(n))$ amortized
        pub fn size(&mut self, a: usize) -> usize {
            debug_assert!(a < self.n);
            let x = self.leader(a);
            -self.parent_or_size[x] as usize
        }

        /// Divides the graph into connected components.
        ///
        /// The result may not be ordered.
        ///
        /// # Complexity
        ///
        /// - $O(n)$
        pub fn groups(&mut self) -> Vec<Vec<usize>> {
            let leader_buf = (0..self.n).map(|i| self.leader(i)).collect::<Vec<_>>();
            let mut group_size = vec![0usize; self.n];
            for &leader in leader_buf.iter() {
                group_size[leader] += 1;
            }
            let mut packed_count = 0usize;
            let mut packed = group_size
                .iter()
                .map(|&size| {
                    let val = packed_count;
                    packed_count += size;
                    (val, val)
                })
                .collect::<Vec<_>>();
            let mut result = vec![0usize; self.n];
            for (i, &leader) in leader_buf.iter().enumerate() {
                if let Some(range) = packed.get_mut(leader) {
                    result[range.1] = i;
                    range.1 += 1;
                }
            }
            packed
                .iter()
                .filter_map(|&(i, j)| {
                    if i != j {
                        Some(result[i..j].to_vec())
                    } else {
                        None
                    }
                })
                .collect::<Vec<_>>()
        }
    }

    #[cfg(test)]
    mod tests {
        use super::*;

        #[test]
        fn dsu_works() {
            let mut d = Dsu::new(4);
            d.merge(0, 1);
            assert!(d.same(0, 1));
            d.merge(1, 2);
            assert!(d.same(0, 2));
            assert_eq!(d.size(0), 3);
            assert!(!d.same(0, 3));
            assert_eq!(d.groups(), vec![vec![0, 1, 2], vec![3]]);
        }
    }
}
use dsu::*;

Submission Info

Submission Time
Task D - Unicyclic Components
User mizarjp
Language Rust (1.42.0)
Score 400
Code Size 60849 Byte
Status AC
Exec Time 28 ms
Memory 6000 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 3
AC × 61
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_rnd_00.txt, 01_rnd_01.txt, 01_rnd_02.txt, 01_rnd_03.txt, 01_rnd_04.txt, 01_rnd_05.txt, 01_rnd_06.txt, 01_rnd_07.txt, 01_rnd_08.txt, 02_one_00.txt, 02_one_01.txt, 02_one_02.txt, 02_one_03.txt, 02_one_04.txt, 02_one_05.txt, 02_one_06.txt, 02_one_07.txt, 02_one_08.txt, 02_one_09.txt, 02_one_10.txt, 02_one_11.txt, 02_one_12.txt, 02_one_13.txt, 03_two_00.txt, 03_two_01.txt, 03_two_02.txt, 03_two_03.txt, 03_two_04.txt, 03_two_05.txt, 03_two_06.txt, 03_two_07.txt, 03_two_08.txt, 03_two_09.txt, 03_two_10.txt, 03_two_11.txt, 04_many_00.txt, 04_many_01.txt, 04_many_02.txt, 04_many_03.txt, 04_many_04.txt, 04_many_05.txt, 04_many_06.txt, 04_many_07.txt, 04_many_08.txt, 04_many_09.txt, 04_many_10.txt, 04_many_11.txt, 05_hand_00.txt, 05_hand_01.txt, 99_hack_00.txt, 99_hack_01.txt, 99_hack_02.txt, 99_hack_03.txt, 99_hack_04.txt, 99_hack_05.txt, 99_hack_06.txt, 99_hack_07.txt, 99_hack_08.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 6 ms 2100 KiB
00_sample_01.txt AC 2 ms 2056 KiB
00_sample_02.txt AC 2 ms 2056 KiB
01_rnd_00.txt AC 2 ms 1944 KiB
01_rnd_01.txt AC 2 ms 2072 KiB
01_rnd_02.txt AC 15 ms 3580 KiB
01_rnd_03.txt AC 1 ms 1940 KiB
01_rnd_04.txt AC 2 ms 2060 KiB
01_rnd_05.txt AC 23 ms 3540 KiB
01_rnd_06.txt AC 4 ms 4316 KiB
01_rnd_07.txt AC 4 ms 4256 KiB
01_rnd_08.txt AC 28 ms 5764 KiB
02_one_00.txt AC 2 ms 1984 KiB
02_one_01.txt AC 2 ms 2100 KiB
02_one_02.txt AC 2 ms 1972 KiB
02_one_03.txt AC 1 ms 1936 KiB
02_one_04.txt AC 2 ms 2088 KiB
02_one_05.txt AC 2 ms 2036 KiB
02_one_06.txt AC 1 ms 1904 KiB
02_one_07.txt AC 22 ms 5888 KiB
02_one_08.txt AC 25 ms 6000 KiB
02_one_09.txt AC 21 ms 5944 KiB
02_one_10.txt AC 22 ms 5772 KiB
02_one_11.txt AC 22 ms 5792 KiB
02_one_12.txt AC 21 ms 5900 KiB
02_one_13.txt AC 25 ms 5816 KiB
03_two_00.txt AC 21 ms 5756 KiB
03_two_01.txt AC 25 ms 5828 KiB
03_two_02.txt AC 20 ms 5864 KiB
03_two_03.txt AC 27 ms 5888 KiB
03_two_04.txt AC 21 ms 5932 KiB
03_two_05.txt AC 24 ms 5832 KiB
03_two_06.txt AC 19 ms 5860 KiB
03_two_07.txt AC 23 ms 5852 KiB
03_two_08.txt AC 24 ms 5788 KiB
03_two_09.txt AC 23 ms 5772 KiB
03_two_10.txt AC 22 ms 5896 KiB
03_two_11.txt AC 24 ms 5896 KiB
04_many_00.txt AC 15 ms 4924 KiB
04_many_01.txt AC 13 ms 4772 KiB
04_many_02.txt AC 14 ms 4860 KiB
04_many_03.txt AC 15 ms 4716 KiB
04_many_04.txt AC 24 ms 5788 KiB
04_many_05.txt AC 22 ms 5876 KiB
04_many_06.txt AC 20 ms 5792 KiB
04_many_07.txt AC 22 ms 5916 KiB
04_many_08.txt AC 20 ms 5692 KiB
04_many_09.txt AC 19 ms 5624 KiB
04_many_10.txt AC 21 ms 5636 KiB
04_many_11.txt AC 20 ms 5552 KiB
05_hand_00.txt AC 1 ms 2044 KiB
05_hand_01.txt AC 21 ms 5844 KiB
99_hack_00.txt AC 3 ms 1880 KiB
99_hack_01.txt AC 1 ms 2160 KiB
99_hack_02.txt AC 2 ms 2084 KiB
99_hack_03.txt AC 2 ms 2056 KiB
99_hack_04.txt AC 2 ms 2060 KiB
99_hack_05.txt AC 5 ms 1984 KiB
99_hack_06.txt AC 2 ms 1912 KiB
99_hack_07.txt AC 1 ms 2008 KiB
99_hack_08.txt AC 1 ms 2020 KiB