結果

問題 No.1917 LCMST
ユーザー hiromi_ayasehiromi_ayase
提出日時 2022-04-30 06:18:41
言語 Rust
(1.77.0 + proconio)
結果
MLE  
実行時間 -
コード長 5,028 bytes
コンパイル時間 14,591 ms
コンパイル使用メモリ 377,680 KB
実行使用メモリ 816,308 KB
最終ジャッジ日時 2024-06-29 12:23:27
合計ジャッジ時間 114,359 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 4 ms
5,376 KB
testcase_04 AC 4 ms
5,376 KB
testcase_05 AC 4 ms
5,376 KB
testcase_06 AC 4 ms
5,376 KB
testcase_07 AC 4 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 2,692 ms
294,456 KB
testcase_10 AC 2,683 ms
288,940 KB
testcase_11 AC 2,618 ms
278,576 KB
testcase_12 AC 2,685 ms
316,796 KB
testcase_13 AC 2,821 ms
307,796 KB
testcase_14 AC 2,679 ms
301,676 KB
testcase_15 AC 2,794 ms
303,948 KB
testcase_16 AC 2,825 ms
310,636 KB
testcase_17 AC 2,796 ms
315,628 KB
testcase_18 AC 2,775 ms
300,668 KB
testcase_19 AC 2,467 ms
322,752 KB
testcase_20 AC 2,402 ms
293,888 KB
testcase_21 AC 2,452 ms
295,324 KB
testcase_22 AC 2,405 ms
292,108 KB
testcase_23 AC 2,400 ms
290,672 KB
testcase_24 AC 2,436 ms
320,972 KB
testcase_25 AC 2,393 ms
275,644 KB
testcase_26 AC 2,423 ms
300,524 KB
testcase_27 AC 2,458 ms
303,800 KB
testcase_28 AC 2,457 ms
316,936 KB
testcase_29 AC 2,606 ms
292,048 KB
testcase_30 AC 2,602 ms
277,128 KB
testcase_31 AC 2,590 ms
274,756 KB
testcase_32 AC 2,589 ms
271,024 KB
testcase_33 AC 2,597 ms
281,124 KB
testcase_34 AC 1,831 ms
281,808 KB
testcase_35 AC 1,095 ms
95,180 KB
testcase_36 AC 2,268 ms
367,300 KB
testcase_37 AC 2,646 ms
279,780 KB
testcase_38 AC 2,618 ms
282,304 KB
testcase_39 AC 2,609 ms
287,452 KB
testcase_40 AC 2,608 ms
273,360 KB
testcase_41 AC 2,609 ms
270,864 KB
testcase_42 AC 2,608 ms
266,192 KB
testcase_43 AC 3,443 ms
507,432 KB
testcase_44 MLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::{
    collections::{HashMap},
};

#[allow(clippy::many_single_char_names)]
fn main() {
    let n = getline().parse::<usize>().unwrap();
    let mut a = getline()
        .split(' ')
        .map(|x| x.parse::<i32>().unwrap())
        .collect::<Vec<_>>();

    a.sort();
    let mut div_map: HashMap<i32, _> = HashMap::new();
    for i in 0..n as i32 {
        let divs = divisors(a[i as usize]);
        for d in divs {
            div_map.entry(d).or_insert_with(Vec::new).push(i);
        }
    }

    let mut edges: Vec<(i32, i32, i64)> = Vec::new();
    for e in div_map {
        let d = e.0 as i64;
        let u = e.1[0];

        for v in e.1 {
            if u == v {
                continue;
            }
            edges.push((u, v, a[u as usize] as i64 / d * a[v as usize] as i64));
        }
    }

    edges.sort_by_key(|e| e.2);
    let mut ds = Dsu::new(n);
    let mut ans = 0;
    for e in edges {
        if ds.same(e.0 as usize, e.1 as usize) {
            continue;
        }
        ds.merge(e.0 as usize, e.1 as usize);
        ans += e.2;
    }
    println!("{}", ans);
}

fn divisors(x: i32) -> Vec<i32> {
    let mut ret = vec![];
    let mut i = 1;
    while i * i <= x {
        if x % i == 0 {
            ret.push(i);
            if i * i != x {
                ret.push(x / i);
            }
        }
        i += 1;
    }
    ret
}

fn getline() -> String {
    let mut buf = String::new();
    std::io::stdin().read_line(&mut buf).unwrap();
    buf.trim().to_string()
}

pub struct Dsu {
    n: usize,
    // root node: -1 * component size
    // otherwise: parent
    parent_or_size: Vec<i32>,
}

impl Dsu {
    /// Creates a new `Dsu`.
    ///
    /// # Constraints
    ///
    /// - $0 \leq n \leq 10^8$
    ///
    /// # Complexity
    ///
    /// - $O(n)$
    pub fn new(size: usize) -> Self {
        Self {
            n: size,
            parent_or_size: vec![-1; size],
        }
    }

    // `\textsc` does not work in KaTeX
    /// Performs the Uɴɪᴏɴ operation.
    ///
    /// # Constraints
    ///
    /// - $0 \leq a < n$
    /// - $0 \leq b < n$
    ///
    /// # Panics
    ///
    /// Panics if the above constraints are not satisfied.
    ///
    /// # Complexity
    ///
    /// - $O(\alpha(n))$ amortized
    pub fn merge(&mut self, a: usize, b: usize) -> usize {
        assert!(a < self.n);
        assert!(b < self.n);
        let (mut x, mut y) = (self.leader(a), self.leader(b));
        if x == y {
            return x;
        }
        if -self.parent_or_size[x] < -self.parent_or_size[y] {
            std::mem::swap(&mut x, &mut y);
        }
        self.parent_or_size[x] += self.parent_or_size[y];
        self.parent_or_size[y] = x as i32;
        x
    }

    /// Returns whether the vertices $a$ and $b$ are in the same connected component.
    ///
    /// # Constraints
    ///
    /// - $0 \leq a < n$
    /// - $0 \leq b < n$
    ///
    /// # Panics
    ///
    /// Panics if the above constraint is not satisfied.
    ///
    /// # Complexity
    ///
    /// - $O(\alpha(n))$ amortized
    pub fn same(&mut self, a: usize, b: usize) -> bool {
        assert!(a < self.n);
        assert!(b < self.n);
        self.leader(a) == self.leader(b)
    }

    /// Performs the Fɪɴᴅ operation.
    ///
    /// # Constraints
    ///
    /// - $0 \leq a < n$
    ///
    /// # Panics
    ///
    /// Panics if the above constraint is not satisfied.
    ///
    /// # Complexity
    ///
    /// - $O(\alpha(n))$ amortized
    pub fn leader(&mut self, a: usize) -> usize {
        assert!(a < self.n);
        if self.parent_or_size[a] < 0 {
            return a;
        }
        self.parent_or_size[a] = self.leader(self.parent_or_size[a] as usize) as i32;
        self.parent_or_size[a] as usize
    }

    /// Returns the size of the connected component that contains the vertex $a$.
    ///
    /// # Constraints
    ///
    /// - $0 \leq a < n$
    ///
    /// # Panics
    ///
    /// Panics if the above constraint is not satisfied.
    ///
    /// # Complexity
    ///
    /// - $O(\alpha(n))$ amortized
    pub fn size(&mut self, a: usize) -> usize {
        assert!(a < self.n);
        let x = self.leader(a);
        -self.parent_or_size[x] as usize
    }

    /// Divides the graph into connected components.
    ///
    /// The result may not be ordered.
    ///
    /// # Complexity
    ///
    /// - $O(n)$
    pub fn groups(&mut self) -> Vec<Vec<usize>> {
        let mut leader_buf = vec![0; self.n];
        let mut group_size = vec![0; self.n];
        for i in 0..self.n {
            leader_buf[i] = self.leader(i);
            group_size[leader_buf[i]] += 1;
        }
        let mut result = vec![Vec::new(); self.n];
        for i in 0..self.n {
            result[i].reserve(group_size[i]);
        }
        for i in 0..self.n {
            result[leader_buf[i]].push(i);
        }
        result
            .into_iter()
            .filter(|x| !x.is_empty())
            .collect::<Vec<Vec<usize>>>()
    }
}
0