結果

問題 No.1059 素敵な集合
ユーザー fukafukatanifukafukatani
提出日時 2020-10-11 21:25:55
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 85 ms / 2,000 ms
コード長 3,129 bytes
コンパイル時間 12,506 ms
コンパイル使用メモリ 403,552 KB
実行使用メモリ 123,904 KB
最終ジャッジ日時 2024-07-23 09:42:03
合計ジャッジ時間 13,968 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 85 ms
123,904 KB
testcase_02 AC 12 ms
12,808 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 10 ms
10,368 KB
testcase_07 AC 9 ms
10,176 KB
testcase_08 AC 12 ms
14,056 KB
testcase_09 AC 3 ms
6,944 KB
testcase_10 AC 18 ms
23,752 KB
testcase_11 AC 11 ms
12,752 KB
testcase_12 AC 5 ms
6,944 KB
testcase_13 AC 20 ms
23,360 KB
testcase_14 AC 1 ms
6,940 KB
testcase_15 AC 34 ms
45,764 KB
testcase_16 AC 11 ms
12,348 KB
testcase_17 AC 10 ms
11,344 KB
testcase_18 AC 6 ms
7,296 KB
testcase_19 AC 81 ms
114,720 KB
testcase_20 AC 79 ms
108,572 KB
testcase_21 AC 32 ms
39,732 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(unused_imports)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;
use std::ops::Bound::*;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn main() {
    let v = read_vec::<usize>();
    let (l, r) = (v[0], v[1]);
    let mut edges = vec![];
    for i in 0..r - l {
        edges.push(Edge {
            from: i,
            to: i + 1,
            cost: 1,
        });
    }
    for i in l..r + 1 {
        let mut to = i * 2;
        while to <= r {
            edges.push(Edge {
                from: i - l,
                to: to - l,
                cost: 0,
            });
            to += i;
        }
    }
    let ans = kruskal(&edges, r - l + 1);
    println!("{}", ans);
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}

use std::cmp::Ordering;

#[derive(Debug, Clone)]
struct Edge {
    from: usize,
    to: usize,
    cost: i32,
}

impl PartialEq for Edge {
    fn eq(&self, other: &Edge) -> bool {
        self.cost == other.cost
    }
}

impl Eq for Edge {}

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

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

struct UnionFindTree {
    parent_or_size: Vec<isize>,
}

impl UnionFindTree {
    fn new(size: usize) -> UnionFindTree {
        UnionFindTree {
            parent_or_size: vec![-1; size],
        }
    }

    fn find(&self, index: usize) -> usize {
        let mut index = index;
        while self.parent_or_size[index] >= 0 {
            index = self.parent_or_size[index] as usize;
        }
        index
    }

    fn same(&self, x: usize, y: usize) -> bool {
        self.find(x) == self.find(y)
    }

    fn unite(&mut self, index0: usize, index1: usize) -> bool {
        let a = self.find(index0);
        let b = self.find(index1);
        if a == b {
            false
        } else {
            if self.parent_or_size[a] < self.parent_or_size[b] {
                self.parent_or_size[a] += self.parent_or_size[b];
                self.parent_or_size[b] = a as isize;
            } else {
                self.parent_or_size[b] += self.parent_or_size[a];
                self.parent_or_size[a] = b as isize;
            }
            true
        }
    }
}

fn kruskal(edges: &Vec<Edge>, num_apexes: usize) -> i32 {
    let mut edges = edges.clone();
    let mut res = 0;
    edges.sort();
    let mut unf = UnionFindTree::new(num_apexes);
    for e in edges {
        if !unf.same(e.to, e.from) {
            unf.unite(e.to, e.from);
            res += e.cost;
        }
    }
    res
}
0