結果
問題 | No.1813 Magical Stones |
ユーザー | koba-e964 |
提出日時 | 2023-04-01 10:27:35 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 142 ms / 2,000 ms |
コード長 | 5,253 bytes |
コンパイル時間 | 13,477 ms |
コンパイル使用メモリ | 400,584 KB |
実行使用メモリ | 23,928 KB |
最終ジャッジ日時 | 2024-09-24 09:13:40 |
合計ジャッジ時間 | 18,671 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,816 KB |
testcase_01 | AC | 1 ms
6,816 KB |
testcase_02 | AC | 1 ms
6,940 KB |
testcase_03 | AC | 13 ms
12,176 KB |
testcase_04 | AC | 1 ms
6,944 KB |
testcase_05 | AC | 1 ms
6,940 KB |
testcase_06 | AC | 3 ms
6,944 KB |
testcase_07 | AC | 1 ms
6,940 KB |
testcase_08 | AC | 1 ms
6,940 KB |
testcase_09 | AC | 1 ms
6,944 KB |
testcase_10 | AC | 1 ms
6,940 KB |
testcase_11 | AC | 23 ms
7,956 KB |
testcase_12 | AC | 1 ms
6,944 KB |
testcase_13 | AC | 1 ms
6,940 KB |
testcase_14 | AC | 12 ms
12,176 KB |
testcase_15 | AC | 132 ms
22,632 KB |
testcase_16 | AC | 133 ms
22,884 KB |
testcase_17 | AC | 132 ms
22,216 KB |
testcase_18 | AC | 142 ms
23,928 KB |
testcase_19 | AC | 139 ms
23,576 KB |
testcase_20 | AC | 138 ms
22,232 KB |
testcase_21 | AC | 129 ms
22,520 KB |
testcase_22 | AC | 140 ms
22,564 KB |
testcase_23 | AC | 141 ms
23,324 KB |
testcase_24 | AC | 2 ms
6,944 KB |
testcase_25 | AC | 15 ms
6,940 KB |
testcase_26 | AC | 5 ms
6,940 KB |
testcase_27 | AC | 81 ms
18,328 KB |
testcase_28 | AC | 86 ms
15,568 KB |
testcase_29 | AC | 92 ms
17,788 KB |
testcase_30 | AC | 86 ms
17,568 KB |
testcase_31 | AC | 120 ms
21,532 KB |
testcase_32 | AC | 1 ms
6,940 KB |
testcase_33 | AC | 1 ms
6,944 KB |
testcase_34 | AC | 3 ms
6,940 KB |
testcase_35 | AC | 9 ms
6,944 KB |
testcase_36 | AC | 3 ms
6,944 KB |
testcase_37 | AC | 21 ms
6,940 KB |
testcase_38 | AC | 17 ms
9,908 KB |
testcase_39 | AC | 83 ms
19,472 KB |
testcase_40 | AC | 3 ms
6,944 KB |
testcase_41 | AC | 4 ms
6,944 KB |
testcase_42 | AC | 10 ms
6,944 KB |
testcase_43 | AC | 7 ms
6,940 KB |
ソースコード
use std::cmp::*; // 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. // This struct uses O(n) stack space. // Verified by: yukicoder No.470 (http://yukicoder.me/submissions/145785) // ABC214-H (https://atcoder.jp/contests/abc214/submissions/25082618) pub 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 { pub 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], } } pub 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); } } } pub 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 } pub 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. pub 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() } pub 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(); } // https://yukicoder.me/problems/no/1813 (3.5) // SCC を行う。SCC が 1 個であれば 0。 // SCC が 2 個以上の場合、SCC からなる DAG において max(入次数 0 の頂点数, 出次数 0 の頂点数) が答え。 // これらの間に完全マッチングに近いものが作れるため。 fn solve() { input! { n: usize, m: usize, ab: [(usize1, usize1); m], } let mut scc = SCC::new(n); for (a, b) in ab { scc.add_edge(a, b); } let ncc = scc.scc(); let mut ans = 0; if ncc >= 2 { let dag = scc.dag(); let rdag = scc.rdag(); ans = max(dag.iter().filter(|x| x.is_empty()).count(), rdag.iter().filter(|x| x.is_empty()).count()) } println!("{}", ans); }