結果

問題 No.1023 Cyclic Tour
ユーザー cotton_fn_cotton_fn_
提出日時 2020-04-11 23:12:02
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 156 ms / 2,000 ms
コード長 6,808 bytes
コンパイル時間 13,862 ms
コンパイル使用メモリ 399,284 KB
実行使用メモリ 39,604 KB
最終ジャッジ日時 2024-09-19 16:09:25
合計ジャッジ時間 20,834 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,812 KB
testcase_02 AC 1 ms
6,816 KB
testcase_03 AC 2 ms
6,816 KB
testcase_04 AC 17 ms
6,816 KB
testcase_05 AC 16 ms
6,940 KB
testcase_06 AC 19 ms
6,944 KB
testcase_07 AC 18 ms
6,944 KB
testcase_08 AC 44 ms
11,776 KB
testcase_09 AC 89 ms
22,912 KB
testcase_10 AC 83 ms
23,276 KB
testcase_11 AC 94 ms
24,448 KB
testcase_12 AC 97 ms
26,584 KB
testcase_13 AC 94 ms
25,216 KB
testcase_14 AC 99 ms
24,832 KB
testcase_15 AC 98 ms
25,344 KB
testcase_16 AC 148 ms
27,904 KB
testcase_17 AC 156 ms
28,032 KB
testcase_18 AC 145 ms
26,504 KB
testcase_19 AC 141 ms
26,332 KB
testcase_20 AC 77 ms
21,716 KB
testcase_21 AC 86 ms
22,400 KB
testcase_22 AC 108 ms
23,528 KB
testcase_23 AC 111 ms
24,032 KB
testcase_24 AC 138 ms
27,260 KB
testcase_25 AC 85 ms
21,844 KB
testcase_26 AC 72 ms
20,608 KB
testcase_27 AC 63 ms
19,584 KB
testcase_28 AC 135 ms
26,360 KB
testcase_29 AC 134 ms
27,500 KB
testcase_30 AC 131 ms
26,784 KB
testcase_31 AC 125 ms
25,896 KB
testcase_32 AC 131 ms
27,628 KB
testcase_33 AC 133 ms
26,688 KB
testcase_34 AC 50 ms
7,424 KB
testcase_35 AC 50 ms
7,552 KB
testcase_36 AC 94 ms
22,400 KB
testcase_37 AC 117 ms
26,636 KB
testcase_38 AC 134 ms
26,740 KB
testcase_39 AC 112 ms
24,064 KB
testcase_40 AC 120 ms
24,572 KB
testcase_41 AC 114 ms
24,440 KB
testcase_42 AC 59 ms
10,496 KB
testcase_43 AC 69 ms
13,184 KB
testcase_44 AC 36 ms
17,240 KB
testcase_45 AC 142 ms
39,604 KB
testcase_46 AC 138 ms
33,468 KB
testcase_47 AC 37 ms
17,600 KB
testcase_48 AC 78 ms
23,872 KB
testcase_49 AC 69 ms
22,060 KB
testcase_50 AC 79 ms
23,808 KB
testcase_51 AC 55 ms
12,608 KB
testcase_52 AC 54 ms
12,544 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