struct Count01 {
cnt0: usize,
cnt1: usize,
}Fields§
§cnt0: usize§cnt1: usizeImplementations§
Source§impl Count01
impl Count01
Sourcepub fn new(cnt0: usize, cnt1: usize) -> Self
pub fn new(cnt0: usize, cnt1: usize) -> Self
Examples found in repository?
crates/competitive/src/algorithm/solve_01_on_tree.rs (line 53)
42pub fn solve_01_on_tree(
43 n: usize,
44 c01: impl Fn(usize) -> (usize, usize),
45 root: usize,
46 parent: impl Fn(usize) -> usize,
47) -> (usize, Vec<usize>) {
48 pub type UF<T, M> =
49 UnionFindBase<(), union_find::PathCompression, union_find::FnMerger<T, M>, (), ()>;
50 let mut cost = 0usize;
51 let c01 = |u| {
52 let c = c01(u);
53 Count01::new(c.0, c.1)
54 };
55 let mut uf = UF::new_with_merger(n, &c01, |x, y| {
56 cost += x.cnt1 * y.cnt0;
57 *x += *y;
58 });
59 let mut label = vec![0; n];
60 let mut heap = BinaryHeap::from_iter((0..n).filter(|&u| u != root).map(|u| (c01(u), u, 0)));
61 let mut next: Vec<_> = (0..n).collect();
62 let mut ord = Vec::with_capacity(n);
63 while let Some((_c, u, l)) = heap.pop() {
64 if label[u] != l {
65 continue;
66 }
67 let p = uf.find_root(parent(u));
68 uf.unite(u, p);
69 if !uf.same(p, root) {
70 label[p] += 1;
71 heap.push((*uf.merge_data(p), p, label[p]));
72 }
73 next.swap(u, p);
74 }
75 let mut u = next[root];
76 ord.push(u);
77 while u != root {
78 u = next[u];
79 ord.push(u);
80 }
81 ord.reverse();
82 (cost, ord)
83}Trait Implementations§
Source§impl AddAssign for Count01
impl AddAssign for Count01
Source§fn add_assign(&mut self, other: Self)
fn add_assign(&mut self, other: Self)
Performs the
+= operation. Read moreSource§impl Ord for Count01
impl Ord for Count01
Source§impl PartialOrd for Count01
impl PartialOrd for Count01
impl Copy for Count01
impl Eq for Count01
impl StructuralPartialEq for Count01
Auto Trait Implementations§
impl Freeze for Count01
impl RefUnwindSafe for Count01
impl Send for Count01
impl Sync for Count01
impl Unpin for Count01
impl UnwindSafe for Count01
Blanket Implementations§
Source§impl<T> AsTotalOrd for Twhere
T: PartialOrd,
impl<T> AsTotalOrd for Twhere
T: PartialOrd,
fn as_total_ord(&self) -> TotalOrd<&T>
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more