結果
問題 | No.1194 Replace |
ユーザー | akakimidori |
提出日時 | 2020-08-22 16:40:08 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 229 ms / 2,000 ms |
コード長 | 6,438 bytes |
コンパイル時間 | 14,794 ms |
コンパイル使用メモリ | 376,828 KB |
実行使用メモリ | 41,756 KB |
最終ジャッジ日時 | 2024-10-15 10:33:46 |
合計ジャッジ時間 | 20,567 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 147 ms
28,068 KB |
testcase_01 | AC | 156 ms
29,692 KB |
testcase_02 | AC | 126 ms
24,852 KB |
testcase_03 | AC | 101 ms
21,356 KB |
testcase_04 | AC | 155 ms
29,660 KB |
testcase_05 | AC | 138 ms
27,916 KB |
testcase_06 | AC | 128 ms
26,192 KB |
testcase_07 | AC | 227 ms
41,432 KB |
testcase_08 | AC | 223 ms
41,536 KB |
testcase_09 | AC | 226 ms
41,484 KB |
testcase_10 | AC | 223 ms
41,604 KB |
testcase_11 | AC | 228 ms
41,756 KB |
testcase_12 | AC | 229 ms
41,624 KB |
testcase_13 | AC | 125 ms
18,896 KB |
testcase_14 | AC | 108 ms
15,648 KB |
testcase_15 | AC | 94 ms
17,552 KB |
testcase_16 | AC | 121 ms
18,908 KB |
testcase_17 | AC | 85 ms
16,192 KB |
testcase_18 | AC | 84 ms
13,860 KB |
testcase_19 | AC | 125 ms
19,708 KB |
testcase_20 | AC | 1 ms
5,248 KB |
testcase_21 | AC | 1 ms
5,248 KB |
testcase_22 | AC | 1 ms
5,248 KB |
testcase_23 | AC | 37 ms
8,824 KB |
testcase_24 | AC | 11 ms
5,248 KB |
testcase_25 | AC | 5 ms
5,248 KB |
testcase_26 | AC | 43 ms
7,676 KB |
testcase_27 | AC | 9 ms
5,248 KB |
testcase_28 | AC | 20 ms
5,248 KB |
testcase_29 | AC | 3 ms
5,248 KB |
ソースコード
// ---------- 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 ---------- //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, bytes) => { read_value!($iter, String).bytes().collect::<Vec<u8>>() }; ($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: [(usize, usize); m], } let mut sum = n * (n + 1) / 2; let mut z = vec![]; for &(a, b) in e.iter() { z.push(a); z.push(b); } z.sort(); z.dedup(); sum -= z.iter().sum::<usize>(); let mut scc = SCC::new(z.len()); let mut e = e; for e in e.iter_mut() { e.0 = z.binary_search(&e.0).unwrap(); e.1 = z.binary_search(&e.1).unwrap(); scc.add_edge(e.0, e.1); } scc.build(); let id = scc.get_array(); let size = *id.iter().max().unwrap() + 1; let mut g = vec![vec![]; size]; for &(a, b) in e.iter() { let a = id[a]; let b = id[b]; if a != b { g[a].push(b); } } let mut dp = vec![0; size]; for (i, z) in z.iter().enumerate() { let dp = &mut dp[id[i]]; *dp = std::cmp::max(*dp, *z); } for (v, g) in g.iter().enumerate().rev() { for &u in g.iter() { dp[v] = std::cmp::max(dp[u], dp[v]); } } for i in 0..z.len() { sum += dp[id[i]]; } println!("{}", sum); } fn main() { run(); }