結果
| 問題 |
No.1059 素敵な集合
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-01-05 23:20:23 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 45 ms / 2,000 ms |
| コード長 | 1,353 bytes |
| コンパイル時間 | 13,215 ms |
| コンパイル使用メモリ | 380,056 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-29 15:32:31 |
| 合計ジャッジ時間 | 14,485 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 19 |
コンパイルメッセージ
warning: unused variable: `c`
--> src/main.rs:51:16
|
51 | for (l, r, c) in (l..r).map(|i| (i, i+1, 1)) {
| ^ help: if this is intentional, prefix it with an underscore: `_c`
|
= note: `#[warn(unused_variables)]` on by default
warning: field `n` is never read
--> src/main.rs:3:5
|
2 | struct UnionFind {
| --------- field in this struct
3 | n: usize,
| ^
|
= note: `#[warn(dead_code)]` on by default
ソースコード
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+1);
for i in l..r {
for j in 2.. {
let val = i*j;
if val > r { break; }
uf.unite(i, val);
}
}
let mut result = 0usize;
for (l, r, c) in (l..r).map(|i| (i, i+1, 1)) {
if uf.find(l) == uf.find(r) { continue; }
uf.unite(l, r);
result += 1;
}
println!("{}", result);
}