マージテクの例: ABC 329 F - Colored Ball
fn solve2(&self) -> Answer {
let Problem { n_box, nq, colors, queries } = self;
// box_to_colors[box_i]: box_i 番目の箱に入っているの色の集合
let mut box_to_colors = colors.iter().copied().map(|c| HashSet::from([c])).collect_vec();
let mut ans = Vec::new();
for q in queries {
// 箱a のボールを箱 b に移す
if box_to_colors[q.a].len() > box_to_colors[q.b].len() {
box_to_colors.swap(q.a, q.b)
}
for x in std::mem::take(&mut box_to_colors[q.a]) {
box_to_colors[q.b].insert(x);
}
ans.push(box_to_colors[q.b].len());
}
Answer { ans }
}
fn solve3(&self) -> Answer {
let Problem { n_box, nq, colors, queries } = self;
// box_to_colors[box_i]: box_i 番目の箱に入っているの色の集合
let mut box_to_colors = colors.iter().copied().map(|c| HashSet::from([c])).collect_vec();
let mut ans = Vec::new();
for q in queries {
// 箱a のボールを箱 b に移す
if box_to_colors[q.a].len() > box_to_colors[q.b].len() {
box_to_colors.swap(q.a, q.b)
}
for &x in &box_to_colors[q.a] { // ここで take しないと
box_to_colors[q.b].insert(x); // ここでエラーになる!!!
}
ans.push(box_to_colors[q.b].len());
}
Answer { ans }
}
cannot borrow
box_to_colors
as mutable because it is also borrowed as immutable