Skip to content

Add union-find crate #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ im-rc = "=14.0.0"
fixedbitset = "=0.2.0"
bitset-fixed = "=0.1.0"

# union-find (a.k.a. disjoint-set)
union-find = "=0.3.2"

# 競技プログラミングの入出力サポート
proconio = { version = "=0.3.4", features = ["derive"] }
text_io = "=0.1.7"
Expand Down
29 changes: 29 additions & 0 deletions examples/abc120-d.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// https://atcoder.jp/contests/abc120/tasks/abc120_d

use proconio::marker::Usize1;
use proconio::{fastout, input};
use union_find::{QuickFindUf, UnionBySize, UnionFind as _};

#[fastout]
fn main() {
input! {
n: usize,
m: usize,
abs: [(Usize1, Usize1); m],
}

let mut u = QuickFindUf::<UnionBySize>::new(n);
let mut k = n * (n - 1) / 2;
let mut r = vec![k];
r.extend(abs.into_iter().rev().map(|(a, b)| {
let p = u.get(a).size() * u.get(b).size();
if u.union(a, b) {
k -= p;
}
k
}));
assert_eq!(r.pop(), Some(0));
for r in r.into_iter().rev() {
println!("{}", r);
}
}
4 changes: 4 additions & 0 deletions examples/tests.ron
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@
name: "ABC049 / ARC065: C - 白昼夢 / Daydream",
matching: ExactWords,
),
"abc120-d": (
name: "ABC120: D - Decayed Bridges ",
matching: ExactWords,
),
}
)
6 changes: 6 additions & 0 deletions examples/testsets/abc120-d/in/sample_01.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
4 5
1 2
3 4
1 3
2 3
1 4
6 changes: 6 additions & 0 deletions examples/testsets/abc120-d/in/sample_02.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
6 5
2 3
1 2
5 6
3 4
4 5
2 changes: 2 additions & 0 deletions examples/testsets/abc120-d/in/sample_03.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
2 1
1 2
5 changes: 5 additions & 0 deletions examples/testsets/abc120-d/out/sample_01.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
0
0
4
5
6
5 changes: 5 additions & 0 deletions examples/testsets/abc120-d/out/sample_02.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
8
9
12
14
15
1 change: 1 addition & 0 deletions examples/testsets/abc120-d/out/sample_03.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
5 changes: 3 additions & 2 deletions tools/test-with-generated-opts/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,7 @@ fn test(task_name: &str, matching: Matching, testsets: &Path, binary: &Path) ->
.with_context(|| format!("Failed to execute {}", binary.display()))?;

child.stdin.as_mut().unwrap().write_all(input.as_ref())?;
let status = child.wait()?;
let stop = Instant::now();
child.stdin.take();
let actual = {
let mut actual = "".to_owned();
child
Expand All @@ -191,6 +190,8 @@ fn test(task_name: &str, matching: Matching, testsets: &Path, binary: &Path) ->
.with_context(|| format!("{} outputted invalid UTF-8", binary.display()))?;
actual
};
let status = child.wait()?;
let stop = Instant::now();

let time = (stop - start).as_millis();
let verdict = if status.success() && matching.accepts(&expected, &actual) {
Expand Down