Submission #1492037


Source Code Expand

fn get_line() -> String {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().to_string()
}

fn reads<T>() -> Vec<T>
    where
        T: std::str::FromStr,
        <T as std::str::FromStr>::Err: std::fmt::Debug {
    get_line().split(' ').map(|x| x.parse().unwrap()).collect()
}

fn readln<T>() -> T
    where
        T: std::str::FromStr,
        <T as std::str::FromStr>::Err: std::fmt::Debug {
    get_line().parse().unwrap()
}

struct UFTree {
    roots: Vec<usize>,
    ranks: Vec<usize>,
}

impl UFTree {
    fn new(num: usize) -> UFTree {
        let mut rs = vec![0; num];
        for i in 0..num {
            rs[i] = i;
        }

        UFTree {
            roots: rs,
            ranks: vec![1; num],
        }
    }

    fn find(&mut self, v: usize) -> Option<usize> {
        if self.roots.len() <= v {
            None
        }
        else {
            let vr = self.roots[v];

            if vr == v {
                Some(v)
            }
            else if let Some(r) = self.find(vr) {
                self.roots[v] = r;

                Some(r)
            }
            else {
                None
            }
        }
    }

    fn unite(&mut self, u: usize, v: usize) {
        if let (Some(ur), Some(vr)) = (self.find(u), self.find(v)) {
            if ur == vr {
                return;
            }

            if self.ranks[ur] < self.ranks[vr] {
                self.roots[ur] = vr;
            }
            else {
                self.roots[vr] = ur;

                if self.ranks[ur] == self.ranks[vr] {
                    self.ranks[ur] += 1;
                }
            }
        }
    }

    fn same(&mut self, u: usize, v: usize) -> bool {
        if let (Some(ur), Some(vr)) = (self.find(u), self.find(v)) {
            ur == vr
        }
        else {
            false
        }
    }
}

use std::collections::BinaryHeap;
use std::cmp::Ordering;

#[derive(Clone, Copy, Eq, PartialEq)]
struct E {
    u: usize,
    v: usize,
    cost: u64,
}

impl Ord for E {
    fn cmp(&self, other: &Self) -> Ordering {
        other.cost.cmp(&self.cost)
    }
}

impl PartialOrd for E {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

fn main() {
    let wh = reads();
    let mut q = BinaryHeap::new();

    for i in 0..wh[0] {
        let x = readln();

        for j in 0..(wh[1] + 1) {
            q.push(E {u: i * (wh[1] + 1) + j, v: (i + 1) * (wh[1] + 1) + j, cost: x});
        }
    }
    for j in 0..wh[1] {
        let x = readln();

        for i in 0..(wh[0] + 1) {
            q.push(E {u: i * (wh[1] + 1) + j, v: i * (wh[1] + 1) + j + 1, cost: x});
        }
    }

    let mut uft = UFTree::new((wh[0] + 1) * (wh[1] + 1));

    let mut res = 0;
    while let Some(e) = q.pop() {
        if !uft.same(e.u, e.v) {
            uft.unite(e.u, e.v);

            res += e.cost;
        }
    }

    println!("{}", res);
}

Submission Info

Submission Time
Task C - Gr-idian MST
User lodnix
Language Rust (1.15.1)
Score 0
Code Size 3104 Byte
Status RE
Exec Time 1995 ms
Memory 1576948 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 500
Status
AC × 2
AC × 10
RE × 20
Set Name Test Cases
Sample s1.txt, s2.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, s1.txt, s2.txt
Case Name Status Exec Time Memory
01.txt RE 1836 ms 1576948 KB
02.txt RE 1830 ms 1576948 KB
03.txt RE 1834 ms 1576948 KB
04.txt RE 1993 ms 1576948 KB
05.txt RE 1995 ms 1576948 KB
06.txt RE 1836 ms 1576948 KB
07.txt RE 1989 ms 1576948 KB
08.txt RE 1835 ms 1576948 KB
09.txt RE 1838 ms 1576948 KB
10.txt RE 1980 ms 1576948 KB
11.txt RE 1776 ms 1576948 KB
12.txt RE 1983 ms 1576948 KB
13.txt RE 1924 ms 1576948 KB
14.txt RE 1836 ms 1576948 KB
15.txt RE 1837 ms 1576948 KB
16.txt AC 81 ms 15988 KB
17.txt AC 80 ms 15988 KB
18.txt AC 125 ms 24180 KB
19.txt AC 129 ms 24180 KB
20.txt RE 1773 ms 1576948 KB
21.txt RE 1772 ms 1576948 KB
22.txt RE 1837 ms 1576948 KB
23.txt RE 1925 ms 1576948 KB
24.txt RE 1772 ms 1576948 KB
25.txt AC 2 ms 4352 KB
26.txt AC 2 ms 4352 KB
27.txt AC 2 ms 4352 KB
28.txt AC 2 ms 4352 KB
s1.txt AC 2 ms 4352 KB
s2.txt AC 2 ms 4352 KB