結果

問題 No.1023 Cyclic Tour
ユーザー cotton_fn_cotton_fn_
提出日時 2020-04-11 23:12:02
言語 Rust
(1.77.0)
結果
AC  
実行時間 164 ms / 2,000 ms
コード長 6,808 bytes
コンパイル時間 2,454 ms
コンパイル使用メモリ 197,176 KB
実行使用メモリ 39,776 KB
最終ジャッジ日時 2023-10-19 20:01:15
合計ジャッジ時間 10,081 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 17 ms
4,348 KB
testcase_05 AC 17 ms
4,348 KB
testcase_06 AC 22 ms
4,348 KB
testcase_07 AC 18 ms
4,348 KB
testcase_08 AC 48 ms
10,736 KB
testcase_09 AC 94 ms
22,000 KB
testcase_10 AC 93 ms
22,084 KB
testcase_11 AC 101 ms
23,432 KB
testcase_12 AC 109 ms
25,656 KB
testcase_13 AC 99 ms
24,344 KB
testcase_14 AC 105 ms
23,884 KB
testcase_15 AC 99 ms
24,564 KB
testcase_16 AC 160 ms
28,252 KB
testcase_17 AC 164 ms
28,148 KB
testcase_18 AC 155 ms
25,780 KB
testcase_19 AC 152 ms
26,100 KB
testcase_20 AC 85 ms
20,688 KB
testcase_21 AC 96 ms
21,292 KB
testcase_22 AC 119 ms
22,616 KB
testcase_23 AC 122 ms
23,092 KB
testcase_24 AC 154 ms
26,460 KB
testcase_25 AC 87 ms
20,980 KB
testcase_26 AC 79 ms
19,792 KB
testcase_27 AC 65 ms
18,612 KB
testcase_28 AC 149 ms
25,476 KB
testcase_29 AC 150 ms
26,524 KB
testcase_30 AC 144 ms
25,860 KB
testcase_31 AC 135 ms
24,912 KB
testcase_32 AC 144 ms
26,804 KB
testcase_33 AC 143 ms
25,700 KB
testcase_34 AC 53 ms
6,512 KB
testcase_35 AC 52 ms
6,512 KB
testcase_36 AC 97 ms
21,560 KB
testcase_37 AC 137 ms
26,336 KB
testcase_38 AC 147 ms
27,584 KB
testcase_39 AC 115 ms
23,104 KB
testcase_40 AC 124 ms
23,484 KB
testcase_41 AC 126 ms
23,388 KB
testcase_42 AC 62 ms
9,416 KB
testcase_43 AC 74 ms
12,056 KB
testcase_44 AC 36 ms
16,320 KB
testcase_45 AC 152 ms
39,776 KB
testcase_46 AC 141 ms
33,636 KB
testcase_47 AC 38 ms
16,752 KB
testcase_48 AC 83 ms
23,068 KB
testcase_49 AC 71 ms
21,112 KB
testcase_50 AC 79 ms
23,008 KB
testcase_51 AC 61 ms
11,816 KB
testcase_52 AC 62 ms
11,556 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(unused)]

use kyoproio::*;
use std::{
    collections::*,
    io::{self, prelude::*},
    iter,
    mem::{replace, swap},
};

fn main() -> io::Result<()> {
    std::thread::Builder::new()
        .stack_size(10 * 1024 * 1024)
        .spawn(solve)?
        .join()
        .unwrap();
    Ok(())
}

fn solve() {
    let stdin = io::stdin();
    let mut kin = KInput::new(stdin.lock());
    let stdout = io::stdout();
    let mut out = io::BufWriter::new(stdout.lock());
    macro_rules! output {
        ($($args:expr),+) => { write!(&mut out, $($args),+) };
    }
    macro_rules! outputln {
        ($($args:expr),+) => { output!($($args),+); outputln!(); };
        () => { output!("\n"); if cfg!(debug_assertions) { out.flush(); } }
    }

    let (n, m): (usize, usize) = kin.input();
    let mut uf = UnionFind::new(n + 1);
    let mut g = vec![vec![]; n + 1];
    let mut cy = false;
    for (a, b, c) in kin.iter::<(usize, usize, char)>().take(m) {
        match c {
            '1' => if !uf.unite(a, b) {
                cy = true;
            }
            '2' => g[a].push(b),
            _ => {}
        }
    }
    if cy {
        outputln!("Yes");
        return;
    }
    let mut h = vec![vec![]; n + 1];
    for u in 1..=n {
        for &v in &g[u] {
            let ru = uf.root(u);
            let rv = uf.root(v);
            if ru == rv {
                outputln!("Yes");
                return;
            }
            h[ru].push(rv);
        }
    }
    let scc = strongly_connected_components(&h);
    for c in scc {
        if c.len() >= 2 {
            outputln!("Yes");
            return;
        }
    }
    outputln!("No");
}
pub struct UnionFind {
    p: Vec<isize>,
}
impl UnionFind {
    pub fn new(n: usize) -> Self {
        Self { p: vec![-1; n] }
    }
    pub fn root(&self, mut u: usize) -> usize {
        while self.p[u] >= 0 {
            u = self.p[u] as usize;
        }
        u
    }
    pub fn size(&self, u: usize) -> usize {
        (-self.p[self.root(u)]) as usize
    }
    pub fn unite(&mut self, u: usize, v: usize) -> bool {
        let mut u = self.root(u);
        let mut v = self.root(v);
        if u == v {
            return false;
        }
        if self.p[u] > self.p[v] {
            swap(&mut u, &mut v);
        }
        self.p[u] += self.p[v];
        self.p[v] = u as isize;
        true
    }
    pub fn is_same(&self, u: usize, v: usize) -> bool {
        self.root(u) == self.root(v)
    }
}

fn strongly_connected_components(g: &[Vec<usize>]) -> Vec<Vec<usize>> {
    let mut ord = Vec::with_capacity(g.len());
    let mut vis = vec![false; g.len()];
    for u in 0..g.len() {
        if !vis[u] {
            dfs_f(g, u, &mut ord, &mut vis);
        }
    }
    let mut comps = Vec::new();
    let mut h = vec![Vec::new(); g.len()];
    for u in 0..g.len() {
        for &v in &g[u] {
            h[v].push(u);
        }
    }
    vis.iter_mut().for_each(|v| *v = false);
    for u in ord.into_iter().rev() {
        if !vis[u] {
            let mut comp = Vec::new();
            dfs_c(&h, u, &mut comp, &mut vis);
            comps.push(comp);
        }
    }
    comps
}
fn dfs_f(g: &[Vec<usize>], u: usize, ord: &mut Vec<usize>, vis: &mut [bool]) {
    vis[u] = true;
    for &v in &g[u] {
        if !vis[v] {
            dfs_f(g, v, ord, vis);
        }
    }
    ord.push(u);
}
fn dfs_c(h: &[Vec<usize>], u: usize, comp: &mut Vec<usize>, vis: &mut [bool]) {
    vis[u] = true;
    comp.push(u);
    for &v in &h[u] {
        if !vis[v] {
            dfs_c(h, v, comp, vis);
        }
    }
}

// -----------------------------------------------------------------------------
pub mod kyoproio {
    #![warn(unused)]
    use std::io::prelude::*;
    pub trait Input {
        fn str(&mut self) -> &str;
        fn bytes(&mut self) -> &[u8] {
            self.str().as_ref()
        }
        fn input<T: InputParse>(&mut self) -> T {
            T::input(self).expect("input error")
        }
        fn iter<T: InputParse>(&mut self) -> Iter<T, Self> {
            Iter(self, std::marker::PhantomData)
        }
        fn seq<T: InputParse, B: std::iter::FromIterator<T>>(&mut self, n: usize) -> B {
            self.iter().take(n).collect()
        }
    }
    pub struct KInput<R> {
        src: R,
        buf: String,
        pos: usize,
    }
    impl<R: BufRead> KInput<R> {
        pub fn new(src: R) -> Self {
            Self {
                src,
                buf: String::with_capacity(1024),
                pos: 0,
            }
        }
    }
    impl<R: BufRead> Input for KInput<R> {
        fn str(&mut self) -> &str {
            loop {
                if self.pos >= self.buf.len() {
                    self.pos = 0;
                    self.buf.clear();
                    if self.src.read_line(&mut self.buf).expect("io error") == 0 {
                        return &self.buf;
                    }
                }
                let range = self.pos
                    ..self.buf[self.pos..]
                        .find(|c: char| c.is_ascii_whitespace())
                        .map(|i| i + self.pos)
                        .unwrap_or_else(|| self.buf.len());
                self.pos = range.end + 1;
                if range.end > range.start {
                    return &self.buf[range];
                }
            }
        }
    }
    pub struct Iter<'a, T, I: ?Sized>(&'a mut I, std::marker::PhantomData<*const T>);
    impl<'a, T: InputParse, I: Input + ?Sized> Iterator for Iter<'a, T, I> {
        type Item = T;
        fn next(&mut self) -> Option<Self::Item> {
            Some(self.0.input())
        }
    }
    pub trait InputParse: Sized {
        type Err: std::fmt::Debug;
        fn input<I: Input + ?Sized>(input: &mut I) -> Result<Self, Self::Err>;
    }
    macro_rules! from_str_impls {
        { $($T:ty)* } => {
            $(impl InputParse for $T {
                type Err = <$T as std::str::FromStr>::Err;
                fn input<I: Input + ?Sized>(input: &mut I) -> Result<Self, Self::Err> {
                    input.str().parse::<$T>()
                }
            })*
        };
    }
    from_str_impls! {
        String char bool f32 f64 isize i8 i16 i32 i64 i128 usize u8 u16 u32 u64 u128
    }
    macro_rules! tuple_impls {
        ($H:ident $($T:ident)*) => {
            impl<$H: InputParse, $($T: InputParse),*> InputParse for ($H, $($T),*) {
                type Err = std::convert::Infallible;
                fn input<I: Input + ?Sized>(input: &mut I) -> Result<Self, Self::Err> {
                    // ?
                    Ok(($H::input(input).unwrap(), $($T::input(input).unwrap()),*))
                }
            }
            tuple_impls!($($T)*);
        };
        () => {};
    }
    tuple_impls!(A B C D E F G);
}
0