結果

問題 No.1059 素敵な集合
ユーザー phsplsphspls
提出日時 2023-01-04 22:08:53
言語 Rust
(1.77.0)
結果
AC  
実行時間 42 ms / 2,000 ms
コード長 1,344 bytes
コンパイル時間 833 ms
コンパイル使用メモリ 148,760 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-18 18:57:55
合計ジャッジ時間 2,526 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 32 ms
4,380 KB
testcase_02 AC 5 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 4 ms
4,380 KB
testcase_07 AC 4 ms
4,380 KB
testcase_08 AC 5 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 8 ms
4,380 KB
testcase_11 AC 4 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 8 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 14 ms
4,376 KB
testcase_16 AC 4 ms
4,376 KB
testcase_17 AC 4 ms
4,380 KB
testcase_18 AC 3 ms
4,376 KB
testcase_19 AC 42 ms
4,380 KB
testcase_20 AC 40 ms
4,376 KB
testcase_21 AC 14 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: field `n` is never read
 --> Main.rs:3:5
  |
2 | struct UnionFind {
  |        --------- field in this struct
3 |     n: usize,
  |     ^
  |
  = note: `#[warn(dead_code)]` on by default

warning: 1 warning emitted

ソースコード

diff #

struct UnionFind {
    n: usize,
    parents: Vec<usize>,
}

impl UnionFind {
    fn new(n: usize) -> Self {
        UnionFind {
            n: n,
            parents: (0..n).collect(),
        }
    }

    fn equiv(&mut self, a: usize, b: usize) -> bool {
        self.find(a) == self.find(b)
    }
    
    fn unite(&mut self, a: usize, b: usize) {
        if self.equiv(a, b) { return; }
        let (a, b) = (a.min(b), a.max(b));
        let x = self.parents[a];
        let y = self.parents[b];
        self.parents[y] = self.parents[x];
    }

    fn find(&mut self, a: usize) -> usize {
        if self.parents[a] == a { return a; }
        let p = self.find(self.parents[a]);
        self.parents[a] = p;
        p
    }
}

fn main() {
    let mut lr = String::new();
    std::io::stdin().read_line(&mut lr).ok();
    let lr: Vec<usize> = lr.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
    let l = lr[0];
    let r = lr[1];

    let mut uf = UnionFind::new(r + 10);
    for i in l..r {
        for j in 2..1000000 {
            let val = i * j;
            if val > r { break; }
            uf.unite(i, val);
        }
    }
    let mut result = 0usize;
    for i in l..r {
        if uf.find(i) != uf.find(i+1) {
            result += 1;
            uf.unite(i, i+1);
        }
    }
    println!("{}", result);
}
0