Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2445d01

Browse files
committed
Add an example for ABC157-D
1 parent 614eae5 commit 2445d01

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

examples/abc157-d.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// https://atcoder.jp/contests/abc157/tasks/abc157_d
2+
//
3+
// 以下のクレートを使用。
4+
//
5+
// - `itertools`
6+
// - `petgraph`
7+
// - `proconio`
8+
9+
use itertools::Itertools as _;
10+
use petgraph::graphmap::UnGraphMap;
11+
use petgraph::unionfind::UnionFind;
12+
use proconio::input;
13+
use proconio::marker::Usize1;
14+
15+
fn main() {
16+
// `proconio::input!`。
17+
//
18+
// https://docs.rs/proconio/0.3.6/proconio/macro.input.html
19+
input! {
20+
n: usize,
21+
m: usize,
22+
k: usize,
23+
abs: [(Usize1, Usize1); m],
24+
cds: [(Usize1, Usize1); k],
25+
}
26+
27+
// `petgraph::unionfind::UnionFind<_>`を使用。
28+
// `UnionFind`は各集合のサイズは覚えてないので`vec![_; n]`で管理する。
29+
//
30+
// https://docs.rs/petgraph/0.5/petgraph/unionfind/struct.UnionFind.html
31+
let mut ff = UnionFind::new(n);
32+
let mut ff_sizes = vec![1; n];
33+
for &(a, b) in &abs {
34+
let size_sum = ff_sizes[ff.find(a)] + ff_sizes[ff.find(b)];
35+
if ff.union(a, b) {
36+
ff_sizes[ff.find(a)] = size_sum;
37+
}
38+
}
39+
40+
// `UnionFind`の`rank`はprivateな為面倒。ここではこのような方法で数える。
41+
// この際`HashSet`のかわりに`petgraph::graphmap::GraphMap<..>`を使う。
42+
//
43+
// https://docs.rs/petgraph/0.5/petgraph/graphmap/struct.GraphMap.html
44+
let mut ans = (0..n).map(|i| ff_sizes[ff.find(i)] - 1).collect::<Vec<_>>();
45+
let mut already_decr = UnGraphMap::with_capacity(n, abs.len());
46+
for (a, b) in abs {
47+
ans[a] -= 1;
48+
ans[b] -= 1;
49+
already_decr.add_edge(a, b, ());
50+
}
51+
for (c, d) in cds {
52+
if ff.equiv(c, d) && !already_decr.contains_edge(c, d) {
53+
ans[c] -= 1;
54+
ans[d] -= 1;
55+
}
56+
}
57+
58+
println!("{}", ans.iter().format(" "));
59+
}

test-examples.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,13 @@ url = "https://atcoder.jp/contests/abc156/tasks/abc156_d"
184184
matching = "Words"
185185
meta = { using = ["num", "proconio"] }
186186

187+
[examples.abc157-d]
188+
type = "Normal"
189+
name = "ABC157: D - Friend Suggestions"
190+
url = "https://atcoder.jp/contests/abc157/tasks/abc157_d"
191+
matching = "Words"
192+
meta = { using = ["itertools", "petgraph", "proconio"] }
193+
187194
[examples.agc023-a]
188195
type = "Normal"
189196
name = "AGC023: A - Zero-Sum Ranges"

0 commit comments

Comments
 (0)