提出 #43531298


ソースコード 拡げる

use std::collections::VecDeque;

use proconio::{input, marker::Usize1};

fn dfs(
    inf: i64,
    edges: &[Vec<(usize, i64, usize)>],
    res: &mut Vec<i64>,
    u: usize,
    p: usize,
    x: i64,
) {
    res[u] = x;
    for (v, w, _) in edges[u].iter().copied() {
        if v == p {
            continue;
        }
        if res[v] != inf {
            continue;
        }

        dfs(inf, edges, res, v, u, w - x);
    }
}

fn main() {
    input! {
        n: usize,
        m: usize,
        abc: [(Usize1, Usize1, i64); m],
    };

    let mut edges = vec![vec![]; n];
    for (i, (a_i, b_i, c_i)) in abc.iter().copied().enumerate() {
        edges[a_i].push((b_i, c_i, i));
        edges[b_i].push((a_i, c_i, i));
    }

    let mut p = vec![None; n]; // p_i + x_0
    let mut q = vec![None; n]; // q_i - x_0
    let mut used_edges = vec![false; m];
    let mut deque = VecDeque::new();
    let x_0 = 0;
    deque.push_back((0, x_0, true));
    p[0] = Some(x_0);
    q[0] = Some(x_0);
    while let Some((u, x_u, u_is_p)) = deque.pop_front() {
        for (v, w, i) in edges[u].iter().copied() {
            if used_edges[i] {
                continue;
            }
            used_edges[i] = true;

            let nv = w - x_u;
            if let Some(cv) = if u_is_p { q[v] } else { p[v] } {
                if cv != nv {
                    println!("-1");
                    return;
                }
            }
            if u_is_p {
                q[v] = Some(nv);
            } else {
                p[v] = Some(nv);
            }
            deque.push_back((v, nv, !u_is_p));
        }
    }

    let mut bounds = (
        0,
        edges[0].iter().copied().map(|(_, w, _)| w).min().unwrap(),
    );
    let mut ans0 = None;
    for (p_i, q_i) in p.iter().copied().zip(q.iter().copied()).skip(1) {
        match (p_i, q_i) {
            (None, None) => unreachable!(),
            (None, Some(q_i)) => bounds = (bounds.0, bounds.1.min(q_i)),
            (Some(p_i), None) => bounds = (bounds.0.max(-p_i), bounds.1),
            (Some(p_i), Some(q_i)) => {
                if (q_i - p_i) % 2 != 0 {
                    println!("-1");
                    return;
                }
                if let Some(ans_0) = ans0 {
                    if ans_0 != (q_i - p_i) / 2 {
                        println!("-1");
                        return;
                    }
                }
                ans0 = Some((q_i - p_i) / 2);
            }
        }
    }

    let (l, u) = bounds;
    if l > u {
        println!("-1");
        return;
    }
    let ans_0 = ans0.unwrap_or(l);
    if !(l..=u).contains(&ans_0) {
        println!("-1");
        return;
    }

    let inf = 1_000_000_000_000_000_i64;
    let mut ans = vec![inf; n];
    dfs(inf, &edges, &mut ans, 0, 0, ans_0);

    for (a, b, c) in abc.iter().copied() {
        if ans[a] + ans[b] != c {
            unreachable!();
        }
    }

    for a in ans {
        println!("{}", a);
    }
}

提出情報

提出日時
問題 M - バランス
ユーザ bouzuya
言語 Rust (1.42.0)
得点 6
コード長 2992 Byte
結果 AC
実行時間 203 ms
メモリ 21976 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 6 / 6
結果
AC × 2
AC × 26
セット名 テストケース
Sample sample_01.txt, sample_02.txt
All hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, sample_01.txt, sample_02.txt
ケース名 結果 実行時間 メモリ
hand_01.txt AC 6 ms 2028 KiB
hand_02.txt AC 2 ms 2092 KiB
hand_03.txt AC 2 ms 2072 KiB
hand_04.txt AC 2 ms 2188 KiB
hand_05.txt AC 2 ms 2132 KiB
hand_06.txt AC 2 ms 2084 KiB
random_01.txt AC 164 ms 15948 KiB
random_02.txt AC 174 ms 16780 KiB
random_03.txt AC 35 ms 12072 KiB
random_04.txt AC 71 ms 13524 KiB
random_05.txt AC 11 ms 3760 KiB
random_06.txt AC 40 ms 13940 KiB
random_07.txt AC 190 ms 21976 KiB
random_08.txt AC 33 ms 10624 KiB
random_09.txt AC 38 ms 13044 KiB
random_10.txt AC 59 ms 18208 KiB
random_11.txt AC 113 ms 14784 KiB
random_12.txt AC 161 ms 20560 KiB
random_13.txt AC 50 ms 17992 KiB
random_14.txt AC 203 ms 19672 KiB
random_15.txt AC 44 ms 16500 KiB
random_16.txt AC 54 ms 18828 KiB
random_17.txt AC 172 ms 17692 KiB
random_18.txt AC 50 ms 18952 KiB
sample_01.txt AC 7 ms 2048 KiB
sample_02.txt AC 3 ms 2064 KiB