結果

問題 No.2914 正閉路検出
ユーザー atcoder8atcoder8
提出日時 2024-10-05 06:19:22
言語 Rust
(1.77.0 + proconio)
結果
WA  
実行時間 -
コード長 3,734 bytes
コンパイル時間 13,095 ms
コンパイル使用メモリ 398,876 KB
実行使用メモリ 45,912 KB
最終ジャッジ日時 2024-10-05 06:19:41
合計ジャッジ時間 18,813 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 ms
6,820 KB
testcase_01 AC 1 ms
6,820 KB
testcase_02 AC 1 ms
6,816 KB
testcase_03 AC 1 ms
6,816 KB
testcase_04 AC 0 ms
6,816 KB
testcase_05 AC 1 ms
6,820 KB
testcase_06 AC 1 ms
6,820 KB
testcase_07 AC 1 ms
6,816 KB
testcase_08 AC 1 ms
6,816 KB
testcase_09 AC 1 ms
6,820 KB
testcase_10 AC 1 ms
6,816 KB
testcase_11 AC 1 ms
6,816 KB
testcase_12 AC 1 ms
6,820 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,820 KB
testcase_17 AC 1 ms
6,820 KB
testcase_18 AC 1 ms
6,820 KB
testcase_19 AC 9 ms
8,192 KB
testcase_20 AC 18 ms
18,752 KB
testcase_21 AC 5 ms
6,820 KB
testcase_22 AC 21 ms
21,804 KB
testcase_23 AC 15 ms
16,572 KB
testcase_24 AC 16 ms
13,056 KB
testcase_25 AC 2 ms
6,816 KB
testcase_26 AC 4 ms
6,820 KB
testcase_27 AC 31 ms
36,704 KB
testcase_28 AC 41 ms
35,136 KB
testcase_29 AC 60 ms
32,048 KB
testcase_30 AC 9 ms
11,780 KB
testcase_31 AC 26 ms
22,716 KB
testcase_32 AC 30 ms
22,528 KB
testcase_33 WA -
testcase_34 WA -
testcase_35 AC 44 ms
25,816 KB
testcase_36 AC 45 ms
28,716 KB
testcase_37 AC 47 ms
34,756 KB
testcase_38 AC 40 ms
34,848 KB
testcase_39 AC 63 ms
37,608 KB
testcase_40 AC 69 ms
44,272 KB
testcase_41 AC 69 ms
39,476 KB
testcase_42 AC 73 ms
45,912 KB
testcase_43 AC 69 ms
44,168 KB
testcase_44 AC 63 ms
36,736 KB
testcase_45 AC 65 ms
44,108 KB
testcase_46 AC 67 ms
42,484 KB
testcase_47 AC 68 ms
42,616 KB
testcase_48 AC 67 ms
37,980 KB
testcase_49 AC 69 ms
43,464 KB
testcase_50 AC 65 ms
41,652 KB
testcase_51 AC 30 ms
27,000 KB
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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>], start: usize, potential: i64| {
            let mut prev_path = vec![];
            let mut current = start;
            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[start].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 {
                let closed_path = create_increasing_closed_path(&path, &nodes, current, potential);
                return Some(ClonedPath {
                    start: current,
                    edges: closed_path,
                });
            }

            continue;
        }

        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 {
            id: self.id,
            from: self.to,
            to: self.from,
            weight: -self.weight,
        }
    }
}

#[derive(Debug, Clone, Copy)]
struct Node {
    potential: i64,
    passed_edge: Option<Edge>,
}

#[derive(Debug, Clone)]
struct ClonedPath {
    start: usize,
    edges: Vec<usize>,
}
0