結果
問題 | No.2914 正閉路検出 |
ユーザー | atcoder8 |
提出日時 | 2024-10-05 05:35:21 |
言語 | Rust (1.77.0 + proconio) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,719 bytes |
コンパイル時間 | 12,869 ms |
コンパイル使用メモリ | 377,652 KB |
実行使用メモリ | 44,828 KB |
最終ジャッジ日時 | 2024-10-05 05:35:42 |
合計ジャッジ時間 | 20,572 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 1 ms
5,248 KB |
testcase_04 | AC | 1 ms
5,248 KB |
testcase_05 | AC | 2 ms
5,248 KB |
testcase_06 | AC | 1 ms
5,248 KB |
testcase_07 | AC | 2 ms
5,248 KB |
testcase_08 | AC | 2 ms
5,248 KB |
testcase_09 | AC | 1 ms
5,248 KB |
testcase_10 | AC | 1 ms
5,248 KB |
testcase_11 | AC | 1 ms
5,248 KB |
testcase_12 | AC | 1 ms
5,248 KB |
testcase_13 | WA | - |
testcase_14 | AC | 1 ms
6,816 KB |
testcase_15 | AC | 1 ms
6,816 KB |
testcase_16 | AC | 1 ms
6,816 KB |
testcase_17 | AC | 1 ms
6,816 KB |
testcase_18 | WA | - |
testcase_19 | AC | 13 ms
8,192 KB |
testcase_20 | AC | 43 ms
18,452 KB |
testcase_21 | AC | 7 ms
6,820 KB |
testcase_22 | AC | 48 ms
21,432 KB |
testcase_23 | AC | 16 ms
15,616 KB |
testcase_24 | AC | 19 ms
12,928 KB |
testcase_25 | WA | - |
testcase_26 | AC | 3 ms
6,820 KB |
testcase_27 | AC | 33 ms
37,328 KB |
testcase_28 | AC | 46 ms
35,200 KB |
testcase_29 | AC | 84 ms
32,748 KB |
testcase_30 | AC | 10 ms
11,804 KB |
testcase_31 | AC | 22 ms
23,560 KB |
testcase_32 | AC | 29 ms
22,400 KB |
testcase_33 | WA | - |
testcase_34 | WA | - |
testcase_35 | AC | 68 ms
25,868 KB |
testcase_36 | AC | 48 ms
28,844 KB |
testcase_37 | AC | 48 ms
35,296 KB |
testcase_38 | AC | 41 ms
33,596 KB |
testcase_39 | AC | 62 ms
38,476 KB |
testcase_40 | AC | 65 ms
43,956 KB |
testcase_41 | AC | 64 ms
39,412 KB |
testcase_42 | AC | 65 ms
44,828 KB |
testcase_43 | AC | 72 ms
44,144 KB |
testcase_44 | AC | 65 ms
36,700 KB |
testcase_45 | AC | 67 ms
43,316 KB |
testcase_46 | AC | 67 ms
42,596 KB |
testcase_47 | AC | 68 ms
41,628 KB |
testcase_48 | AC | 62 ms
37,792 KB |
testcase_49 | AC | 66 ms
43,500 KB |
testcase_50 | AC | 72 ms
41,664 KB |
testcase_51 | AC | 30 ms
26,972 KB |
testcase_52 | WA | - |
testcase_53 | WA | - |
testcase_54 | WA | - |
ソースコード
use proconio::{input, marker::Usize1}; fn main() { match solve() { Some(closed_path) => { println!( "{}\n{}\n{}", closed_path.edges.len(), closed_path.start + 1, closed_path .edges .iter() .map(|edge_id| (edge_id + 1).to_string()) .collect::<Vec<_>>() .join(" ") ); } None => println!("-1"), } } fn solve() -> Option<ClonedPath> { input! { (n, m): (usize, usize), uvw: [(Usize1, Usize1, i64); m], } let mut graph = vec![vec![]; n]; for (id, &(u, v, w)) in uvw.iter().enumerate() { let edge = Edge::new(id, u, v, w); graph[u].push(edge); graph[v].push(edge.inverse()); } let create_increasing_closed_path = |path: &[usize], nodes: &[Option<Node>], current: usize, potential: i64| { let mut prev_path = vec![]; let mut current = current; while current != 0 { let passed_edge = nodes[current].unwrap().passed_edge.unwrap(); prev_path.push(passed_edge.id); current = passed_edge.from; } prev_path.reverse(); let common_length = prev_path .iter() .zip(path) .take_while(|(node1, node2)| node1 == node2) .count(); let mut closed_path = prev_path[common_length..] .iter() .rev() .chain(&path[common_length..]) .cloned() .collect::<Vec<_>>(); if potential < nodes[current].unwrap().potential { closed_path.reverse(); } closed_path }; let mut nodes: Vec<Option<Node>> = vec![None; n]; let mut stack: Vec<(Option<Edge>, i64, bool)> = vec![(None, 0, true)]; let mut path: Vec<usize> = vec![]; while let Some((passed_edge, potential, advance)) = stack.pop() { if !advance { path.pop().unwrap(); continue; } if let Some(edge) = passed_edge { path.push(edge.id); } let current = passed_edge.map(|edge| edge.to).unwrap_or(0); if let Some(node) = nodes[current] { if potential == node.potential { continue; } let closed_path = create_increasing_closed_path(&path, &nodes, current, potential); return Some(ClonedPath { start: current, edges: closed_path, }); } nodes[current] = Some(Node { potential, passed_edge, }); stack.extend(graph[current].iter().flat_map(|&next_edge| { [ (passed_edge, potential, false), (Some(next_edge), potential + next_edge.weight, true), ] })); } None } #[allow(dead_code)] #[derive(Debug, Clone, Copy)] struct Edge { id: usize, from: usize, to: usize, weight: i64, } impl Edge { fn new(id: usize, from: usize, to: usize, weight: i64) -> Self { Self { id, from, to, weight, } } fn inverse(&self) -> Self { Self { from: self.to, to: self.from, weight: -self.weight, ..*self } } } #[derive(Debug, Clone, Copy)] struct Node { potential: i64, passed_edge: Option<Edge>, } #[derive(Debug, Clone)] struct ClonedPath { start: usize, edges: Vec<usize>, }