結果
問題 | No.1194 Replace |
ユーザー | fukafukatani |
提出日時 | 2020-08-23 21:52:06 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 734 ms / 2,000 ms |
コード長 | 3,423 bytes |
コンパイル時間 | 15,443 ms |
コンパイル使用メモリ | 378,048 KB |
実行使用メモリ | 67,776 KB |
最終ジャッジ日時 | 2024-10-15 19:21:15 |
合計ジャッジ時間 | 26,400 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 437 ms
45,700 KB |
testcase_01 | AC | 488 ms
48,536 KB |
testcase_02 | AC | 370 ms
39,540 KB |
testcase_03 | AC | 299 ms
33,520 KB |
testcase_04 | AC | 496 ms
48,656 KB |
testcase_05 | AC | 446 ms
45,152 KB |
testcase_06 | AC | 415 ms
42,232 KB |
testcase_07 | AC | 734 ms
67,664 KB |
testcase_08 | AC | 722 ms
67,668 KB |
testcase_09 | AC | 718 ms
67,668 KB |
testcase_10 | AC | 721 ms
67,776 KB |
testcase_11 | AC | 691 ms
67,776 KB |
testcase_12 | AC | 705 ms
67,760 KB |
testcase_13 | AC | 338 ms
31,600 KB |
testcase_14 | AC | 271 ms
27,048 KB |
testcase_15 | AC | 245 ms
25,932 KB |
testcase_16 | AC | 322 ms
30,888 KB |
testcase_17 | AC | 222 ms
23,808 KB |
testcase_18 | AC | 212 ms
22,480 KB |
testcase_19 | AC | 332 ms
31,760 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 | 86 ms
12,648 KB |
testcase_24 | AC | 29 ms
6,656 KB |
testcase_25 | AC | 11 ms
5,248 KB |
testcase_26 | AC | 99 ms
13,024 KB |
testcase_27 | AC | 15 ms
5,248 KB |
testcase_28 | AC | 43 ms
7,460 KB |
testcase_29 | AC | 5 ms
5,248 KB |
コンパイルメッセージ
warning: unused variable: `i` --> src/main.rs:22:9 | 22 | for i in 0..m { | ^ help: if this is intentional, prefix it with an underscore: `_i` | = note: `#[warn(unused_variables)]` on by default
ソースコード
#![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 main() { let v = read_vec::<i64>(); let (n, m) = (v[0], v[1] as usize); let mut edges = vec![]; for i in 0..m { let v = read_vec::<i64>(); let (b, c) = (v[0], v[1]); edges.push((b, c)); } let mut used = edges.iter().map(|x| x.0).collect::<Vec<_>>(); used.append(&mut edges.iter().map(|x| x.1).collect::<Vec<_>>()); used.sort(); used.dedup(); let mut compress_dict = BTreeMap::new(); for i in 0..used.len() { compress_dict.insert(used[i], i); } let mut g = vec![vec![]; compress_dict.len()]; for &(b, c) in &edges { let from_idx = compress_dict[&b]; let to_idx = compress_dict[&c]; g[from_idx].push(to_idx); } let cmp = scc(&g); let mut indexes = (0..compress_dict.len()).collect::<Vec<_>>(); indexes.sort_by_key(|&x| cmp[x]); indexes.reverse(); let cmp_max = *cmp.iter().max().unwrap(); let mut accum_max = vec![0; cmp_max + 1]; for &idx in &indexes { accum_max[cmp[idx]] = max(used[idx], accum_max[cmp[idx]]); } for idx in indexes { for &to in &g[idx] { accum_max[cmp[idx]] = max(accum_max[cmp[to]], accum_max[cmp[idx]]); } } let mut ans = n * (n + 1) / 2; let mut used_b = HashSet::new(); for (b, _) in edges { if used_b.contains(&b) { continue; } ans -= b; ans += accum_max[cmp[compress_dict[&b]]]; used_b.insert(b); } println!("{}", ans); } fn scc(g: &[Vec<usize>]) -> Vec<usize> { fn dfs(v: usize, g: &[Vec<usize>], vs: &mut Vec<usize>, used: &mut [bool]) { used[v] = true; for &to in &g[v] { if !used[to] { dfs(to, g, vs, used); } } vs.push(v); } fn rdfs(v: usize, k: usize, g_rev: &[Vec<usize>], cmp: &mut [usize], used: &mut [bool]) { used[v] = true; cmp[v] = k; for &to in &g_rev[v] { if !used[to] { rdfs(to, k, g_rev, cmp, used); } } } let g_rev = { let mut g_rev = vec![Vec::new(); g.len()]; for (i, es) in g.iter().enumerate() { for &to in es { g_rev[to].push(i); } } g_rev }; let mut vs = Vec::new(); let mut used = vec![false; g.len()]; for v in 0..g.len() { if !used[v] { dfs(v, g, &mut vs, &mut used); } } let mut used = vec![false; g.len()]; let mut cmp = vec![0; g.len()]; let mut k = 0; for &v in vs.iter().rev() { if !used[v] { rdfs(v, k, &g_rev, &mut cmp, &mut used); k += 1; } } cmp } 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() }