結果

問題 No.1023 Cyclic Tour
ユーザー koba-e964koba-e964
提出日時 2021-10-06 17:40:27
言語 Rust
(1.77.0)
結果
AC  
実行時間 198 ms / 2,000 ms
コード長 5,188 bytes
コンパイル時間 2,902 ms
コンパイル使用メモリ 156,644 KB
実行使用メモリ 29,192 KB
最終ジャッジ日時 2023-09-30 08:48:13
合計ジャッジ時間 14,985 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 122 ms
19,216 KB
testcase_05 AC 123 ms
20,608 KB
testcase_06 AC 140 ms
22,240 KB
testcase_07 AC 130 ms
21,580 KB
testcase_08 AC 95 ms
16,904 KB
testcase_09 AC 94 ms
16,416 KB
testcase_10 AC 89 ms
16,416 KB
testcase_11 AC 98 ms
17,300 KB
testcase_12 AC 85 ms
16,648 KB
testcase_13 AC 79 ms
15,508 KB
testcase_14 AC 82 ms
15,492 KB
testcase_15 AC 86 ms
16,080 KB
testcase_16 AC 160 ms
22,248 KB
testcase_17 AC 162 ms
23,220 KB
testcase_18 AC 161 ms
22,716 KB
testcase_19 AC 151 ms
22,008 KB
testcase_20 AC 196 ms
22,076 KB
testcase_21 AC 196 ms
22,500 KB
testcase_22 AC 173 ms
21,684 KB
testcase_23 AC 187 ms
21,736 KB
testcase_24 AC 176 ms
21,584 KB
testcase_25 AC 190 ms
22,136 KB
testcase_26 AC 197 ms
23,008 KB
testcase_27 AC 169 ms
22,588 KB
testcase_28 AC 153 ms
21,088 KB
testcase_29 AC 145 ms
20,404 KB
testcase_30 AC 170 ms
21,940 KB
testcase_31 AC 179 ms
22,180 KB
testcase_32 AC 158 ms
24,664 KB
testcase_33 AC 165 ms
23,164 KB
testcase_34 AC 169 ms
20,920 KB
testcase_35 AC 184 ms
22,332 KB
testcase_36 AC 169 ms
21,856 KB
testcase_37 AC 166 ms
24,852 KB
testcase_38 AC 144 ms
20,852 KB
testcase_39 AC 179 ms
21,196 KB
testcase_40 AC 175 ms
21,136 KB
testcase_41 AC 174 ms
21,152 KB
testcase_42 AC 198 ms
22,420 KB
testcase_43 AC 149 ms
20,092 KB
testcase_44 AC 128 ms
19,308 KB
testcase_45 AC 126 ms
29,192 KB
testcase_46 AC 127 ms
27,220 KB
testcase_47 AC 135 ms
27,228 KB
testcase_48 AC 143 ms
23,860 KB
testcase_49 AC 144 ms
23,928 KB
testcase_50 AC 143 ms
24,000 KB
testcase_51 AC 142 ms
23,832 KB
testcase_52 AC 143 ms
23,904 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes.by_ref().map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

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

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

// Strong connected components.
// Verified by: yukicoder No.470 (http://yukicoder.me/submissions/145785)
//              ABC214-H (https://atcoder.jp/contests/abc214/submissions/25082618)
struct SCC {
    n: usize,
    ncc: usize,
    g: Vec<Vec<usize>>, // graph in adjacent list
    rg: Vec<Vec<usize>>, // reverse graph
    cmp: Vec<usize>, // topological order
}

impl SCC {
    fn new(n: usize) -> Self {
        SCC {
            n: n,
            ncc: n + 1,
            g: vec![Vec::new(); n],
            rg: vec![Vec::new(); n],
            cmp: vec![0; n],
        }
    }
    fn add_edge(&mut self, from: usize, to: usize) {
        self.g[from].push(to);
        self.rg[to].push(from);
    }
    fn dfs(&self, v: usize, used: &mut [bool], vs: &mut Vec<usize>) {
        used[v] = true;
        for &w in self.g[v].iter() {
            if !used[w] {
               self.dfs(w, used, vs);
            }
        }
        vs.push(v);
    }
    fn rdfs(&self, v: usize, k: usize,
            used: &mut [bool], cmp: &mut [usize]) {
        used[v] = true;
        cmp[v] = k;
        for &w in self.rg[v].iter() {
            if !used[w] {
                self.rdfs(w, k, used, cmp);
            }
        }
    }
    fn scc(&mut self) -> usize {
        let n = self.n;
        let mut used = vec![false; n];
        let mut vs = Vec::new();
        let mut cmp = vec![0; n];
        for v in 0 .. n {
            if !used[v] { self.dfs(v, &mut used, &mut vs); }
        }
        for u in used.iter_mut() {
            *u = false;
        }
        let mut k = 0;
        for &t in vs.iter().rev() {
            if !used[t] { self.rdfs(t, k, &mut used, &mut cmp); k += 1; }
        }
        self.ncc = k;
        self.cmp = cmp;
        k
    }
    #[allow(dead_code)]
    fn top_order(&self) -> Vec<usize> {
        assert!(self.ncc <= self.n);
        self.cmp.clone()
    }
    /*
     * Returns a dag whose vertices are scc's, and whose edges are those of the original graph.
     */
    #[allow(dead_code)]
    fn dag(&self) -> Vec<Vec<usize>> {
        assert!(self.ncc <= self.n);
        let ncc = self.ncc;
        let mut ret = vec![vec![]; ncc];
        let n = self.n;
        for i in 0 .. n {
            for &to in self.g[i].iter() {
                if self.cmp[i] != self.cmp[to] {
                    assert!(self.cmp[i] < self.cmp[to]);
                    ret[self.cmp[i]].push(self.cmp[to]);
                }
            }
        }
        ret.into_iter().map(|mut v| {
            v.sort_unstable(); v.dedup();
            v
        }).collect()
    }
    #[allow(dead_code)]
    fn rdag(&self) -> Vec<Vec<usize>> {
        assert!(self.ncc <= self.n);
        let ncc = self.ncc;
        let mut ret = vec![vec![]; ncc];
        let n = self.n;
        for i in 0 .. n {
            for &to in self.g[i].iter() {
                if self.cmp[i] != self.cmp[to] {
                    assert!(self.cmp[i] < self.cmp[to]);
                    ret[self.cmp[to]].push(self.cmp[i]);
                }
            }
        }
        ret.into_iter().map(|mut v| {
            v.sort_unstable(); v.dedup();
            v
        }).collect()
    }
}

fn main() {
    // In order to avoid potential stack overflow, spawn a new thread.
    let stack_size = 104_857_600; // 100 MB
    let thd = std::thread::Builder::new().stack_size(stack_size);
    thd.spawn(|| solve()).unwrap().join().unwrap();
}

fn solve() {
    input! {
        n: usize, m: usize,
        abc: [(usize1, usize1, i32); m],
    }
    let mut scc = SCC::new(n);
    for &(a, b, c) in &abc {
        scc.add_edge(a, b);
        if c == 1 {
            scc.add_edge(b, a);
        }
    }
    let ncc = scc.scc();
    let top_ord = scc.top_order();
    let mut ne = vec![0; ncc];
    let mut nv = vec![0; ncc];
    for i in 0..n {
        nv[top_ord[i]] += 1;
    }
    for &(a, b, _) in &abc {
        if top_ord[a] == top_ord[b] {
            ne[top_ord[a]] += 1;
        }
    }
    for i in 0..ncc {
        if ne[i] >= nv[i] {
            println!("Yes");
            return;
        }
    }
    println!("No");
}
0