結果
問題 | No.1194 Replace |
ユーザー | fukafukatani |
提出日時 | 2020-08-23 21:50:11 |
言語 | Rust (1.77.0 + proconio) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,295 bytes |
コンパイル時間 | 15,134 ms |
コンパイル使用メモリ | 377,100 KB |
実行使用メモリ | 67,900 KB |
最終ジャッジ日時 | 2024-10-15 19:20:47 |
合計ジャッジ時間 | 26,283 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | WA | - |
testcase_01 | WA | - |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
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 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | WA | - |
コンパイルメッセージ
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; for (b, _) in edges { ans -= b; ans += accum_max[cmp[compress_dict[&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() }