Skip to content

Commit

Permalink
past202209-open k
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Dec 1, 2023
1 parent 9d46db2 commit 8e71c8c
Showing 1 changed file with 55 additions and 3 deletions.
58 changes: 55 additions & 3 deletions cargo-atcoder-1.70.0/contests/past202209-open/src/bin/k.rs
@@ -1,10 +1,62 @@
use std::collections::HashSet;

use ac_library::Dsu;
use proconio::{input, marker::Usize1};

fn main() {
input! {
n: usize,
a: [Usize1; n],
m: usize,
ab: [(Usize1, Usize1); m],
q: usize,
txy: [(usize, Usize1, Usize1); q],
};
let ans = n - a.len();
println!("{}", ans);
let mut edges = HashSet::new();
for (a, b) in ab.iter().copied() {
edges.insert((a.min(b), a.max(b)));
}
for (t, x, y) in txy.iter().copied() {
match t {
1 => {
edges.insert((x.min(y), x.max(y)));
}
2 => {
edges.remove(&(x.min(y), x.max(y)));
}
3 => {
// do nothing
}
_ => unreachable!(),
}
}

let mut dsu = Dsu::new(n);
for (a, b) in edges.iter().copied() {
dsu.merge(a, b);
}

let mut ans = vec![];
for (t, x, y) in txy.iter().copied().rev() {
match t {
1 => {
edges.remove(&(x.min(y), x.max(y)));
// rebuild dsu
dsu = Dsu::new(n);
for (a, b) in edges.iter().copied() {
dsu.merge(a, b);
}
}
2 => {
edges.insert((x.min(y), x.max(y)));
dsu.merge(x, y);
}
3 => {
ans.push(dsu.same(x, y));
}
_ => unreachable!(),
}
}
for a in ans.into_iter().rev() {
println!("{}", if a { "Yes" } else { "No" });
}
}

0 comments on commit 8e71c8c

Please sign in to comment.