Submission #47511009


Source Code Expand

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

struct UnionFind {
    parent: Vec<usize>,
    diff: Vec<i64>,
}

impl UnionFind {
    fn new(n: usize) -> Self {
        Self {
            parent: (0..n).collect(),
            diff: vec![0; n],
        }
    }

    fn mut_find(&mut self, i: usize) -> usize {
        if self.parent[i] == i {
            i
        } else {
            let p = self.parent[i];
            let r = self.mut_find(p);
            self.diff[i] += self.diff[p];
            self.parent[i] = r;
            r
        }
    }

    fn unite(&mut self, i: usize, j: usize, w: i64) -> bool {
        let ri = self.mut_find(i);
        let rj = self.mut_find(j);
        if ri == rj {
            return self.diff[j] - self.diff[i] == w;
        }
        self.parent[ri] = rj;
        self.diff[ri] = self.diff[j] - self.diff[i] - w;
        true
    }
}

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

    let mut uf = UnionFind::new(n);
    let mut ans = Vec::new();
    for (i, (a, b, d)) in abd.into_iter().enumerate() {
        if uf.unite(a, b, d) {
            ans.push(i);
        }
    }

    println!(
        "{}",
        ans.iter()
            .map(|x| (x + 1).to_string())
            .collect::<Vec<_>>()
            .join(" ")
    );
}

Submission Info

Submission Time
Task F - Good Set Query
User ikd
Language Rust (rustc 1.70.0)
Score 525
Code Size 1392 Byte
Status AC
Exec Time 46 ms
Memory 23264 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 525 / 525
Status
AC × 3
AC × 39
Set Name Test Cases
Sample example0.txt, example1.txt, example2.txt
All 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt, 032.txt, 033.txt, example0.txt, example1.txt, example2.txt, hack_001.txt, hack_002.txt
Case Name Status Exec Time Memory
000.txt AC 17 ms 9828 KiB
001.txt AC 28 ms 18512 KiB
002.txt AC 28 ms 19916 KiB
003.txt AC 14 ms 11076 KiB
004.txt AC 21 ms 13048 KiB
005.txt AC 32 ms 23224 KiB
006.txt AC 33 ms 23264 KiB
007.txt AC 31 ms 22344 KiB
008.txt AC 32 ms 22364 KiB
009.txt AC 30 ms 18480 KiB
010.txt AC 16 ms 9336 KiB
011.txt AC 24 ms 10964 KiB
012.txt AC 29 ms 17440 KiB
013.txt AC 25 ms 16512 KiB
014.txt AC 17 ms 9192 KiB
015.txt AC 25 ms 11456 KiB
016.txt AC 32 ms 18164 KiB
017.txt AC 26 ms 16876 KiB
018.txt AC 17 ms 9376 KiB
019.txt AC 25 ms 11508 KiB
020.txt AC 32 ms 18520 KiB
021.txt AC 27 ms 17152 KiB
022.txt AC 18 ms 9600 KiB
023.txt AC 27 ms 11964 KiB
024.txt AC 33 ms 18792 KiB
025.txt AC 29 ms 17552 KiB
026.txt AC 22 ms 10500 KiB
027.txt AC 31 ms 13224 KiB
028.txt AC 36 ms 19140 KiB
029.txt AC 32 ms 18216 KiB
030.txt AC 43 ms 19860 KiB
031.txt AC 44 ms 21172 KiB
032.txt AC 46 ms 22132 KiB
033.txt AC 42 ms 21468 KiB
example0.txt AC 1 ms 1980 KiB
example1.txt AC 1 ms 3456 KiB
example2.txt AC 1 ms 1980 KiB
hack_001.txt AC 1 ms 1936 KiB
hack_002.txt AC 1 ms 1864 KiB