結果

問題 No.1059 素敵な集合
ユーザー kakikaki
提出日時 2020-05-23 10:00:50
言語 Rust
(1.77.0)
結果
AC  
実行時間 164 ms / 2,000 ms
コード長 3,799 bytes
コンパイル時間 970 ms
コンパイル使用メモリ 153,856 KB
実行使用メモリ 81,008 KB
最終ジャッジ日時 2023-09-30 15:36:59
合計ジャッジ時間 2,768 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 154 ms
81,008 KB
testcase_02 AC 18 ms
9,832 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 15 ms
7,048 KB
testcase_07 AC 15 ms
6,780 KB
testcase_08 AC 19 ms
9,100 KB
testcase_09 AC 5 ms
4,376 KB
testcase_10 AC 33 ms
16,056 KB
testcase_11 AC 17 ms
7,588 KB
testcase_12 AC 8 ms
4,736 KB
testcase_13 AC 34 ms
15,000 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 66 ms
28,820 KB
testcase_16 AC 18 ms
9,352 KB
testcase_17 AC 17 ms
7,748 KB
testcase_18 AC 6 ms
5,696 KB
testcase_19 AC 164 ms
77,956 KB
testcase_20 AC 160 ms
76,444 KB
testcase_21 AC 61 ms
25,952 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(non_snake_case)]
#![allow(dead_code)]
#![allow(unused_macros)]
#![allow(unused_imports)]

use std::str::FromStr;
use std::io::*;
use std::collections::*;
use std::cmp::*;

struct Scanner<I: Iterator<Item = char>> {
    iter: std::iter::Peekable<I>,
}

macro_rules! exit {
    () => {{
        exit!(0)
    }};
    ($code:expr) => {{
        if cfg!(local) {
            writeln!(std::io::stderr(), "===== Terminated =====")
                .expect("failed printing to stderr");
        }
        std::process::exit($code);
    }}
}

impl<I: Iterator<Item = char>> Scanner<I> {
    pub fn new(iter: I) -> Scanner<I> {
        Scanner {
            iter: iter.peekable(),
        }
    }

    pub fn safe_get_token(&mut self) -> Option<String> {
        let token = self.iter
            .by_ref()
            .skip_while(|c| c.is_whitespace())
            .take_while(|c| !c.is_whitespace())
            .collect::<String>();
        if token.is_empty() {
            None
        } else {
            Some(token)
        }
    }

    pub fn token(&mut self) -> String {
        self.safe_get_token().unwrap_or_else(|| exit!())
    }

    pub fn get<T: FromStr>(&mut self) -> T {
        self.token().parse::<T>().unwrap_or_else(|_| exit!())
    }

    pub fn vec<T: FromStr>(&mut self, len: usize) -> Vec<T> {
        (0..len).map(|_| self.get()).collect()
    }

    pub fn mat<T: FromStr>(&mut self, row: usize, col: usize) -> Vec<Vec<T>> {
        (0..row).map(|_| self.vec(col)).collect()
    }

    pub fn char(&mut self) -> char {
        self.iter.next().unwrap_or_else(|| exit!())
    }

    pub fn chars(&mut self) -> Vec<char> {
        self.get::<String>().chars().collect()
    }

    pub fn mat_chars(&mut self, row: usize) -> Vec<Vec<char>> {
        (0..row).map(|_| self.chars()).collect()
    }

    pub fn line(&mut self) -> String {
        if self.peek().is_some() {
            self.iter
                .by_ref()
                .take_while(|&c| !(c == '\n' || c == '\r'))
                .collect::<String>()
        } else {
            exit!();
        }
    }

    pub fn peek(&mut self) -> Option<&char> {
        self.iter.peek()
    }
}

use std::mem::swap;

struct UnionFind {
    parents: Vec<i64>,
}

impl UnionFind {
    fn new(n: usize) -> UnionFind {
        UnionFind { parents: vec![-1; n] }
    }

    fn unite(&mut self, x: usize, y: usize) {
        let mut px = self.root(x);
        let mut py = self.root(y);
        if px == py {
            return;
        }
        if -self.parents[x] < -self.parents[y] {
            swap(&mut px, &mut py);
        }
        self.parents[px] += self.parents[py];
        self.parents[py] = px as i64;
    }

    fn root(&mut self, x: usize) -> usize {
        let px = self.parents[x];
        if px < 0 {
            return x;
        }
        self.parents[x] = self.root(px as usize) as i64;
        self.parents[x] as usize
    }

    fn same(&mut self, x: usize, y: usize) -> bool {
        self.root(x) == self.root(y)
    }
    
    fn size(&mut self, x: usize) -> usize {
        let px = self.root(x);
        -self.parents[px as usize] as usize
    }
}

fn main() {
    let cin = stdin();
    let cin = cin.lock();
    let mut sc = Scanner::new(cin.bytes().map(|c| c.unwrap() as char));
    let L: usize = sc.get();
    let R: usize = sc.get();
    let mut edges = Vec::new();
    for i in L..=R {
        for j in (i*2..=R).step_by(i) {
            edges.push((j%i, i, j));
        }
        if i < R {
            edges.push(((i+1)%i, i, i+1));
        }
    }
    edges.sort();
    let mut uf = UnionFind::new(R+1);
    let mut ans = 0;
    for &(cost, i, j) in &edges {
        if !uf.same(i, j) {
            ans += cost;
            uf.unite(i, j);
        }
    }
    println!("{}", ans);
}
0