Submission #43233802
Source Code Expand
use dsu::*;
use proconio::{input, marker::Usize1};
fn main() {
input! {
n: usize,
m: usize,
ab: [(Usize1, Usize1); m],
};
let mut dsu = Dsu::new(n);
for (a, b) in ab {
dsu.merge(a, b);
}
let ans = dsu.groups().len() - 1;
println!("{}", ans);
}
//https://github.com/rust-lang-ja/ac-library-rs
pub mod dsu {
/// Implement (union by size) + (path compression)
/// Reference:
/// Zvi Galil and Giuseppe F. Italiano,
/// Data structures and algorithms for disjoint set union problems
pub struct Dsu {
n: usize,
// root node: -1 * component size
// otherwise: parent
parent_or_size: Vec<i32>,
}
impl Dsu {
// 0 <= size <= 10^8 is constrained.
pub fn new(size: usize) -> Self {
Self {
n: size,
parent_or_size: vec![-1; size],
}
}
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
}
pub fn same(&mut self, a: usize, b: usize) -> bool {
assert!(a < self.n);
assert!(b < self.n);
self.leader(a) == self.leader(b)
}
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
}
pub fn size(&mut self, a: usize) -> usize {
assert!(a < self.n);
let x = self.leader(a);
-self.parent_or_size[x] as usize
}
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>>>()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn dsu_works() {
let mut d = Dsu::new(4);
d.merge(0, 1);
assert_eq!(d.same(0, 1), true);
d.merge(1, 2);
assert_eq!(d.same(0, 2), true);
assert_eq!(d.size(0), 3);
assert_eq!(d.same(0, 3), false);
assert_eq!(d.groups(), vec![vec![0, 1, 2], vec![3]]);
}
}
}
Submission Info
| Submission Time |
|
| Task |
C - Connect Cities |
| User |
bouzuya |
| Language |
Rust (1.42.0) |
| Score |
300 |
| Code Size |
3263 Byte |
| Status |
AC |
| Exec Time |
30 ms |
| Memory |
10352 KiB |
Judge Result
| Set Name |
Sample |
All |
| Score / Max Score |
0 / 0 |
300 / 300 |
| Status |
|
|
| Set Name |
Test Cases |
| Sample |
example0.txt |
| All |
000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, example0.txt |
| Case Name |
Status |
Exec Time |
Memory |
| 000.txt |
AC |
14 ms |
3480 KiB |
| 001.txt |
AC |
9 ms |
3420 KiB |
| 002.txt |
AC |
20 ms |
6952 KiB |
| 003.txt |
AC |
23 ms |
4924 KiB |
| 004.txt |
AC |
18 ms |
5216 KiB |
| 005.txt |
AC |
21 ms |
6796 KiB |
| 006.txt |
AC |
26 ms |
7660 KiB |
| 007.txt |
AC |
18 ms |
10352 KiB |
| 008.txt |
AC |
30 ms |
8604 KiB |
| 009.txt |
AC |
12 ms |
4036 KiB |
| 010.txt |
AC |
29 ms |
8932 KiB |
| 011.txt |
AC |
26 ms |
8936 KiB |
| 012.txt |
AC |
27 ms |
8908 KiB |
| 013.txt |
AC |
30 ms |
8980 KiB |
| 014.txt |
AC |
29 ms |
8972 KiB |
| 015.txt |
AC |
26 ms |
8840 KiB |
| 016.txt |
AC |
26 ms |
8944 KiB |
| 017.txt |
AC |
24 ms |
9012 KiB |
| 018.txt |
AC |
24 ms |
8928 KiB |
| 019.txt |
AC |
28 ms |
8944 KiB |
| example0.txt |
AC |
3 ms |
1956 KiB |