Submission #43531468


Source Code Expand

use std::collections::VecDeque;

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

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

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

    let mut pos = vec![0_i64; n]; // p_i + x_0
    let mut neg = vec![0_i64; n]; // q_i - x_0
    let mut exists_pos = vec![false; n];
    let mut exists_neg = vec![false; n];

    let mut lbound = 0_i64;
    let mut ubound = 1_i64 << 60;

    let mut deque = VecDeque::new();
    deque.push_back(0);
    exists_pos[0] = true;
    while let Some(u) = deque.pop_front() {
        for (v, c) in edges[u].iter().copied() {
            let mut updated = false;

            if exists_pos[u] && !exists_neg[v] {
                exists_neg[v] = true;
                neg[v] = c - pos[u];
                ubound = ubound.min(c - pos[u]);
                updated = true;
            }

            if exists_neg[u] && !exists_pos[v] {
                exists_pos[v] = true;
                pos[v] = c - neg[u];
                lbound = lbound.max(neg[u] - c);
                updated = true;
            }

            if updated {
                deque.push_back(v);
            }
        }
    }

    if lbound > ubound {
        println!("-1");
        return;
    }

    let mut ans = vec![0_i64; n];

    let mut x = ubound;
    for i in 0..n {
        if exists_pos[i] && exists_neg[i] {
            if (neg[i] - pos[i]) % 2 != 0 {
                println!("-1");
                return;
            }
            x = (neg[i] - pos[i]) / 2;
        }
    }

    for i in 0..n {
        if exists_pos[i] {
            ans[i] = pos[i] + x;
        } else {
            ans[i] = neg[i] - x;
        }

        if ans[i] < 0 {
            println!("-1");
            return;
        }
    }

    for (a_i, b_i, c_i) in abc.iter().copied() {
        if ans[a_i] + ans[b_i] != c_i {
            println!("-1");
            return;
        }
    }

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

Submission Info

Submission Time
Task M - Balance
User bouzuya
Language Rust (1.42.0)
Score 6
Code Size 2143 Byte
Status AC
Exec Time 193 ms
Memory 16524 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 6 / 6
Status
AC × 2
AC × 26
Set Name Test Cases
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
Case Name Status Exec Time Memory
hand_01.txt AC 11 ms 2024 KiB
hand_02.txt AC 2 ms 2068 KiB
hand_03.txt AC 1 ms 1976 KiB
hand_04.txt AC 1 ms 2028 KiB
hand_05.txt AC 2 ms 2116 KiB
hand_06.txt AC 1 ms 2028 KiB
random_01.txt AC 152 ms 13140 KiB
random_02.txt AC 162 ms 14116 KiB
random_03.txt AC 30 ms 10084 KiB
random_04.txt AC 26 ms 9564 KiB
random_05.txt AC 10 ms 3080 KiB
random_06.txt AC 37 ms 11896 KiB
random_07.txt AC 179 ms 14992 KiB
random_08.txt AC 26 ms 9348 KiB
random_09.txt AC 35 ms 11240 KiB
random_10.txt AC 48 ms 15544 KiB
random_11.txt AC 104 ms 9528 KiB
random_12.txt AC 156 ms 12884 KiB
random_13.txt AC 44 ms 14744 KiB
random_14.txt AC 193 ms 16524 KiB
random_15.txt AC 45 ms 14168 KiB
random_16.txt AC 44 ms 15724 KiB
random_17.txt AC 165 ms 14408 KiB
random_18.txt AC 53 ms 15848 KiB
sample_01.txt AC 7 ms 1960 KiB
sample_02.txt AC 1 ms 2136 KiB