結果

問題 No.1023 Cyclic Tour
ユーザー fukafukatanifukafukatani
提出日時 2020-04-11 11:01:59
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 90 ms / 2,000 ms
コード長 3,812 bytes
コンパイル時間 20,840 ms
コンパイル使用メモリ 377,600 KB
実行使用メモリ 20,864 KB
最終ジャッジ日時 2024-09-18 19:18:27
合計ジャッジ時間 21,784 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 0 ms
5,248 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 31 ms
6,400 KB
testcase_05 AC 30 ms
6,400 KB
testcase_06 AC 32 ms
6,656 KB
testcase_07 AC 28 ms
6,528 KB
testcase_08 AC 35 ms
10,112 KB
testcase_09 AC 46 ms
15,488 KB
testcase_10 AC 44 ms
15,616 KB
testcase_11 AC 55 ms
16,384 KB
testcase_12 AC 50 ms
18,176 KB
testcase_13 AC 49 ms
17,024 KB
testcase_14 AC 48 ms
16,768 KB
testcase_15 AC 55 ms
17,408 KB
testcase_16 AC 77 ms
20,608 KB
testcase_17 AC 75 ms
20,864 KB
testcase_18 AC 72 ms
19,712 KB
testcase_19 AC 68 ms
19,584 KB
testcase_20 AC 74 ms
15,928 KB
testcase_21 AC 76 ms
16,256 KB
testcase_22 AC 74 ms
17,096 KB
testcase_23 AC 74 ms
17,408 KB
testcase_24 AC 79 ms
19,712 KB
testcase_25 AC 66 ms
16,000 KB
testcase_26 AC 64 ms
14,848 KB
testcase_27 AC 61 ms
14,208 KB
testcase_28 AC 83 ms
18,944 KB
testcase_29 AC 78 ms
19,968 KB
testcase_30 AC 86 ms
19,328 KB
testcase_31 AC 76 ms
18,560 KB
testcase_32 AC 76 ms
19,068 KB
testcase_33 AC 80 ms
19,072 KB
testcase_34 AC 23 ms
7,552 KB
testcase_35 AC 48 ms
8,832 KB
testcase_36 AC 70 ms
16,384 KB
testcase_37 AC 90 ms
18,048 KB
testcase_38 AC 84 ms
18,944 KB
testcase_39 AC 77 ms
17,664 KB
testcase_40 AC 72 ms
17,792 KB
testcase_41 AC 73 ms
17,756 KB
testcase_42 AC 76 ms
9,856 KB
testcase_43 AC 72 ms
12,160 KB
testcase_44 AC 35 ms
10,496 KB
testcase_45 AC 76 ms
20,224 KB
testcase_46 AC 46 ms
19,456 KB
testcase_47 AC 30 ms
9,856 KB
testcase_48 AC 69 ms
17,024 KB
testcase_49 AC 61 ms
15,360 KB
testcase_50 AC 66 ms
16,896 KB
testcase_51 AC 56 ms
11,668 KB
testcase_52 AC 58 ms
11,392 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `i`
  --> src/main.rs:29:9
   |
29 |     for i in 0..m {
   |         ^ help: if this is intentional, prefix it with an underscore: `_i`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: method `get_size` is never used
   --> src/main.rs:102:8
    |
79  | impl UnionFindTree {
    | ------------------ method in this implementation
...
102 |     fn get_size(&mut self, x: usize) -> usize {
    |        ^^^^^^^^
    |
    = note: `#[warn(dead_code)]` on by default

ソースコード

diff #

#![allow(unused_imports)]
#![allow(non_snake_case)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn yes() {
    println!("Yes");
    std::process::exit(0);
}

fn main() {
    let v = read_vec::<usize>();
    let (n, m) = (v[0], v[1]);

    let mut graph = vec![vec![]; n];
    let mut uft = UnionFindTree::new(n);
    for i in 0..m {
        let v = read_vec::<usize>();
        let (a, b, c) = (v[0] - 1, v[1] - 1, v[2]);
        if c == 1 {
            if uft.same(a, b) {
                yes();
            }
            uft.unite(a, b);
        } else {
            graph[a].push(b);
        }
    }

    let mut edges = vec![];
    for i in 0..n {
        for &to in graph[i].iter() {
            if uft.same(i, to) {
                yes();
            }
            edges.push((uft.find(i), uft.find(to)));
        }
    }

    if let Some(_) = topological_sort(&edges, n) {
        println!("No");
        return;
    }
    yes();
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}

#[derive(Debug, Clone)]
struct UnionFindTree {
    parent: Vec<isize>,
    size: Vec<usize>,
    height: Vec<u64>,
}

impl UnionFindTree {
    fn new(n: usize) -> UnionFindTree {
        UnionFindTree {
            parent: vec![-1; n],
            size: vec![1usize; n],
            height: vec![0u64; n],
        }
    }

    fn find(&mut self, index: usize) -> usize {
        if self.parent[index] == -1 {
            return index;
        }
        let idx = self.parent[index] as usize;
        let ret = self.find(idx);
        self.parent[index] = ret as isize;
        ret
    }

    fn same(&mut self, x: usize, y: usize) -> bool {
        self.find(x) == self.find(y)
    }

    fn get_size(&mut self, x: usize) -> usize {
        let idx = self.find(x);
        self.size[idx]
    }

    fn unite(&mut self, index0: usize, index1: usize) -> bool {
        let a = self.find(index0);
        let b = self.find(index1);
        if a == b {
            false
        } else {
            if self.height[a] > self.height[b] {
                self.parent[b] = a as isize;
                self.size[a] += self.size[b];
            } else if self.height[a] < self.height[b] {
                self.parent[a] = b as isize;
                self.size[b] += self.size[a];
            } else {
                self.parent[b] = a as isize;
                self.size[a] += self.size[b];
                self.height[a] += 1;
            }
            true
        }
    }
}

fn topological_sort(edges: &Vec<(usize, usize)>, v: usize) -> Option<Vec<usize>> {
    let mut h = vec![0; v];
    let mut g = vec![Vec::new(); v];
    for &(s, t) in edges {
        g[s].push(t);
        h[t] += 1;
    }

    let mut st: std::collections::VecDeque<usize> = std::collections::VecDeque::new();
    for i in 0..v {
        if h[i] == 0 {
            st.push_back(i);
        }
    }

    let mut sorted_indexes = Vec::new();
    while !st.is_empty() {
        let i = st.pop_back().unwrap();
        sorted_indexes.push(i);
        for &j in g[i].iter() {
            h[j] -= 1;
            if h[j] == 0 {
                st.push_back(j);
            }
        }
    }
    debug!(sorted_indexes);
    if sorted_indexes.len() == v {
        Some(sorted_indexes)
    } else {
        None
    }
}
0