結果

問題 No.1023 Cyclic Tour
ユーザー akakimidoriakakimidori
提出日時 2020-04-10 22:52:14
言語 Rust
(1.77.0 + proconio)
結果
WA  
実行時間 -
コード長 7,275 bytes
コンパイル時間 19,210 ms
コンパイル使用メモリ 378,040 KB
実行使用メモリ 15,792 KB
最終ジャッジ日時 2024-09-15 23:13:04
合計ジャッジ時間 16,992 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 WA -
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 13 ms
6,016 KB
testcase_05 AC 13 ms
6,016 KB
testcase_06 AC 14 ms
6,400 KB
testcase_07 AC 13 ms
5,888 KB
testcase_08 AC 22 ms
10,060 KB
testcase_09 AC 21 ms
9,772 KB
testcase_10 AC 20 ms
9,688 KB
testcase_11 AC 22 ms
10,200 KB
testcase_12 AC 23 ms
11,244 KB
testcase_13 AC 22 ms
10,676 KB
testcase_14 AC 21 ms
10,676 KB
testcase_15 AC 22 ms
10,936 KB
testcase_16 AC 41 ms
15,420 KB
testcase_17 AC 41 ms
15,488 KB
testcase_18 AC 41 ms
15,792 KB
testcase_19 AC 40 ms
15,168 KB
testcase_20 AC 31 ms
11,264 KB
testcase_21 AC 31 ms
11,392 KB
testcase_22 AC 32 ms
12,468 KB
testcase_23 AC 33 ms
12,920 KB
testcase_24 AC 38 ms
15,088 KB
testcase_25 AC 30 ms
11,264 KB
testcase_26 AC 31 ms
11,520 KB
testcase_27 AC 30 ms
11,136 KB
testcase_28 AC 36 ms
14,676 KB
testcase_29 AC 37 ms
15,064 KB
testcase_30 AC 37 ms
14,624 KB
testcase_31 AC 34 ms
13,312 KB
testcase_32 AC 36 ms
14,492 KB
testcase_33 AC 37 ms
14,240 KB
testcase_34 AC 19 ms
9,216 KB
testcase_35 AC 22 ms
9,344 KB
testcase_36 AC 30 ms
11,776 KB
testcase_37 AC 34 ms
13,440 KB
testcase_38 AC 35 ms
14,320 KB
testcase_39 AC 34 ms
13,060 KB
testcase_40 AC 35 ms
13,288 KB
testcase_41 AC 35 ms
13,268 KB
testcase_42 WA -
testcase_43 WA -
testcase_44 AC 15 ms
6,656 KB
testcase_45 AC 27 ms
13,424 KB
testcase_46 AC 29 ms
13,548 KB
testcase_47 AC 13 ms
6,784 KB
testcase_48 AC 26 ms
11,648 KB
testcase_49 AC 26 ms
11,776 KB
testcase_50 AC 26 ms
11,520 KB
testcase_51 WA -
testcase_52 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

// ---------- begin Strongly Connected Components ----------
struct SCC {
    size: usize,
    edge: Vec<(u32, u32)>,
    id: Vec<usize>,
}

impl SCC {
    pub fn new(size: usize) -> Self {
        SCC {
            size: size,
            edge: vec![],
            id: Vec::with_capacity(size),
        }
    }
    pub fn add_edge(&mut self, from: usize, to: usize) {
        assert!(from < self.size && to < self.size);
        self.edge.push((from as u32, to as u32));
    }
    fn fill_buffer(&self, buf: &mut [u32], cnt: &[u32], inv: bool) {
        let mut index = vec![0; self.size];
        let mut sum = 0;
        for (index, c) in index.iter_mut().zip(cnt.iter()) {
            *index = sum;
            sum += *c;
        }
        for e in self.edge.iter() {
            let (f, t) = if inv {
                (e.1 as usize, e.0)
            } else {
                (e.0 as usize, e.1)
            };
            buf[index[f] as usize] = t;
            index[f] += 1;
        }
    }
    fn build_graph<'a>(&self, buf: &'a mut [u32], cnt: &'a [u32], inv: bool) -> Vec<&'a mut [u32]> {
        let size = self.size;
        self.fill_buffer(buf, cnt, inv);
        let mut buf = Some(buf);
        let mut ans = Vec::with_capacity(size);
        for c in cnt.iter() {
            let len = *c as usize;
            let x = buf.take().unwrap();
            let (x, y) = x.split_at_mut(len);
            ans.push(x);
            buf = Some(y);
        }
        ans
    }
    fn dfs1(&self, buf: &mut [u32], cnt: &[u32], q: &mut Vec<u32>) {
        let size = self.size;
        let graph = self.build_graph(buf, cnt, false);
        let mut visited = vec![false; size];
        let mut stack = vec![];
        for v in 0..size {
            if visited[v] {
                continue;
            }
            visited[v] = true;
            stack.push((v, graph[v].iter()));
            while let Some((v, mut it)) = stack.pop() {
                let mut finish = true;
                while let Some(&u) = it.next() {
                    let u = u as usize;
                    if !visited[u] {
                        visited[u] = true;
                        finish = false;
                        stack.push((v, it));
                        stack.push((u, graph[u].iter()));
                        break;
                    }
                }
                if finish {
                    q.push(v as u32);
                }
            }
        }
    }
    fn dfs2(&mut self, buf: &mut [u32], cnt: &[u32], q: &[u32]) {
        let size = self.size;
        let inv_graph = self.build_graph(buf, cnt, true);
        self.id.clear();
        self.id.resize(size, size);
        let mut counter = 0;
        let mut stack = vec![];
        for &v in q.iter().rev() {
            let v = v as usize;
            if self.id[v] < size {
                continue;
            }
            stack.push(v);
            while let Some(v) = stack.pop() {
                self.id[v] = counter;
                for &u in inv_graph[v].iter() {
                    let u = u as usize;
                    if self.id[u] == size {
                        self.id[u] = counter;
                        stack.push(u);
                    }
                }
            }
            counter += 1;
        }
    }
    pub fn build(&mut self) {
        let size = self.size;
        let mut cnt = vec![0; size];
        let mut inv_cnt = vec![0; size];
        for e in self.edge.iter() {
            cnt[e.0 as usize] += 1;
            inv_cnt[e.1 as usize] += 1;
        }
        let mut buf = vec![0; self.edge.len()];
        let buf = buf.as_mut_slice();
        let mut q = Vec::with_capacity(size);
        self.dfs1(buf, &cnt, &mut q);
        self.dfs2(buf, &inv_cnt, &q);
    }
    pub fn get_array(&self) -> Vec<usize> {
        self.id.clone()
    }
}
// ---------- end Strongly Connected Components ----------
//---------- begin union_find ----------
#[allow(dead_code)]
mod union_find {
    pub struct UF {
        p: Vec<i32>,
    }
    impl UF {
        pub fn new(n: usize) -> UF {
            UF {p: vec![-1; n] }
        }
        pub fn init(&mut self) {
            for p in self.p.iter_mut() {
                *p = -1;
            }
        }
        pub fn root(&self, mut x: usize) -> usize {
            while self.p[x] >= 0 {
                x = self.p[x] as usize;
            }
            x
        }
        pub fn same(&self, x: usize, y: usize) -> bool {
            self.root(x) == self.root(y)
        }
        pub fn unite(&mut self, mut x: usize, mut y: usize) -> Option<(usize, usize)> {
            x = self.root(x);
            y = self.root(y);
            if x == y {
                return None;
            }
            if self.p[x] > self.p[y] {
                let s = x;
                x = y;
                y = s;
            }
            self.p[x] += self.p[y];
            self.p[y] = x as i32;
            Some((x, y))
        }
        pub fn get_size(&self, x: usize) -> usize {
            let r = self.root(x);
            (-self.p[r]) as usize
        }
    }
}
//---------- end union_find ----------
//https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 より
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};
    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };
    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };
    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };
    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };
    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

//


fn run() {
    input! {
        n: usize,
        m: usize,
        e: [(usize1, usize1, usize); m],
    }
    let mut u = union_find::UF::new(n);
    for &(a, b, c) in e.iter() {
        if c == 1 {
            if u.unite(a, b).is_none() {
                println!("Yes");
                return;
            }
        }
    }
    let mut id = vec![n; n];
    let mut k = 0;
    for i in 0..n {
        let r = u.root(i);
        if id[r] == n {
            id[r] = k;
            k += 1;
        }
    }
    let mut scc = SCC::new(k);
    for &(a, b, c) in e.iter() {
        if c == 2 {
            let a = u.root(a);
            let b = u.root(b);
            scc.add_edge(id[a], id[b]);
        }
    }
    scc.build();
    if *scc.get_array().iter().max().unwrap() == k - 1 {
        println!("No");
    } else {
        println!("Yes");
    }
}

fn main() {
    run();
}
0