Skip to content

Commit

Permalink
swap u and v (#103)
Browse files Browse the repository at this point in the history
* swap u and v

* fix

---------

Co-authored-by: Luke Videckis <[email protected]>
  • Loading branch information
lrvideckis and lrvideckis authored Jul 29, 2024
1 parent 817bfa9 commit 7fe3a97
Show file tree
Hide file tree
Showing 13 changed files with 129 additions and 129 deletions.
2 changes: 1 addition & 1 deletion examples/data_structures/binary_trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn main() {
trie.update(x, -1);
}
}
_ => println!("{}", trie.min_xor(x)),
_ => println!("{}", trie.walk(x)),
}
}
}
16 changes: 8 additions & 8 deletions src/data_structures/binary_trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ struct Node {
/// use programming_team_code_rust::data_structures::binary_trie::BinaryTrie;
///
/// let mut trie = BinaryTrie::default();
/// assert!(std::panic::catch_unwind(|| trie.min_xor(4)).is_err());
/// assert!(std::panic::catch_unwind(|| trie.walk(4)).is_err());
/// trie.update(1, 1);
/// assert!(std::panic::catch_unwind(|| trie.min_xor(4)).is_ok());
/// assert!(std::panic::catch_unwind(|| trie.walk(4)).is_ok());
/// trie.update(2, 1);
/// trie.update(3, 1);
/// trie.update(2, -1);
/// assert_eq!(trie.count(2), 0);
/// assert_eq!(trie.count(3), 1);
/// assert_eq!(trie.count(4), 0);
/// assert_eq!(trie.min_xor(0), 1);
/// assert_eq!(trie.min_xor(1), 0);
/// assert_eq!(trie.min_xor(2), 1);
/// assert_eq!(trie.min_xor(3), 0);
/// assert_eq!(trie.min_xor(4), 5);
/// assert_eq!(trie.walk(0), 1);
/// assert_eq!(trie.walk(1), 0);
/// assert_eq!(trie.walk(2), 1);
/// assert_eq!(trie.walk(3), 0);
/// assert_eq!(trie.walk(4), 5);
/// ```
pub struct BinaryTrie {
t: Vec<Node>,
Expand Down Expand Up @@ -84,7 +84,7 @@ impl BinaryTrie {
/// # Complexity
/// - Time: O(log(max_num))
/// - Space: O(1)
pub fn min_xor(&self, num: T) -> T {
pub fn walk(&self, num: T) -> T {
assert!(self.t[0].sub_sz > 0);
let mut v = 0;
let mut ans = 0;
Expand Down
10 changes: 5 additions & 5 deletions src/graphs/block_vertex_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@
/// // indexes 0..n are nodes
/// // indexes n..n + num_bccs are bccs
///
/// for u in 0..n {
/// // loop over each unique bcc containing a node u
/// for bccid in bvt[u].iter().map(|v| v - n) {
/// for v in 0..n {
/// // loop over each unique bcc containing a node v
/// for bccid in bvt[v].iter().map(|u| u - n) {
/// assert!(0 <= bccid && bccid < num_bccs);
/// }
/// }
/// for bccid in 0..num_bccs {
/// // loop over each unique node inside a bcc
/// for &u in bvt[bccid + n].iter() {
/// assert!(0 <= u && u < n);
/// for &v in bvt[bccid + n].iter() {
/// assert!(0 <= v && v < n);
/// }
/// }
/// ```
Expand Down
18 changes: 9 additions & 9 deletions src/graphs/bridges.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! # Bridge Edges
/// # Guarantees
/// - 0 <= two_edge_ccid\[u\] < num_2_edge_ccs
/// - 0 <= two_edge_ccid\[v\] < num_2_edge_ccs
///
/// # Example
/// ```
Expand Down Expand Up @@ -41,21 +41,21 @@ pub fn get_bridges(adj: &[Vec<(usize, usize)>], m: usize) -> (usize, Vec<bool>,
two_edge_ccid: vec![0; n],
st: Vec::with_capacity(n),
};
fn dfs(e: &mut Env, u: usize, p_id: Option<usize>, adj: &[Vec<(usize, usize)>]) -> usize {
e.tin[u] = e.timer;
fn dfs(e: &mut Env, v: usize, p_id: Option<usize>, adj: &[Vec<(usize, usize)>]) -> usize {
e.tin[v] = e.timer;
let (mut low, st_sz) = (e.timer, e.st.len());
e.timer += 1;
e.st.push(u);
for &(v, e_id) in &adj[u] {
e.st.push(v);
for &(u, e_id) in &adj[v] {
if Some(e_id) == p_id {
continue;
}
if e.tin[v] == 0 {
low = low.min(dfs(e, v, Some(e_id), adj));
if e.tin[u] == 0 {
low = low.min(dfs(e, u, Some(e_id), adj));
}
low = low.min(e.tin[v]);
low = low.min(e.tin[u]);
}
if e.tin[u] == low {
if e.tin[v] == low {
if let Some(p) = p_id {
e.is_bridge[p] = true;
}
Expand Down
40 changes: 20 additions & 20 deletions src/graphs/cent_decomp.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
//! # Centroid Decomposition
fn calc_sz(adj: &[Vec<usize>], u: usize, p: usize, sub_sz: &mut [usize]) {
sub_sz[u] = 1;
for &v in adj[u].iter() {
if v != p {
calc_sz(adj, v, u, sub_sz);
sub_sz[u] += sub_sz[v];
fn calc_sz(adj: &[Vec<usize>], v: usize, p: usize, sub_sz: &mut [usize]) {
sub_sz[v] = 1;
for &u in adj[v].iter() {
if u != p {
calc_sz(adj, u, v, sub_sz);
sub_sz[v] += sub_sz[u];
}
}
}

fn dfs(
adj: &mut [Vec<usize>],
mut u: usize,
mut v: usize,
sub_sz: &mut [usize],
call_dfs: &mut dyn CentDecompDfs,
) {
calc_sz(adj, u, u, sub_sz);
let sz_root = sub_sz[u];
let mut p = u;
calc_sz(adj, v, v, sub_sz);
let sz_root = sub_sz[v];
let mut p = v;
loop {
let big_ch = adj[u]
let big_ch = adj[v]
.iter()
.filter(|&&v| v != p)
.find(|&&v| sub_sz[v] * 2 > sz_root);
if let Some(&v) = big_ch {
p = u;
u = v;
.filter(|&&u| u != p)
.find(|&&u| sub_sz[u] * 2 > sz_root);
if let Some(&u) = big_ch {
p = v;
v = u;
} else {
break;
}
}
call_dfs.dfs(adj, u);
for v in adj[u].clone() {
adj[v].retain(|&x| x != u);
dfs(adj, v, sub_sz, call_dfs);
call_dfs.dfs(adj, v);
for u in adj[v].clone() {
adj[u].retain(|&x| x != v);
dfs(adj, u, sub_sz, call_dfs);
}
}

Expand Down
22 changes: 11 additions & 11 deletions src/graphs/cuts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,21 @@ pub fn get_cuts(adj: &[Vec<(usize, usize)>], m: usize) -> (usize, Vec<bool>, Vec
bcc_id: vec![0; m],
st: Vec::with_capacity(m),
};
fn dfs(e: &mut Env, u: usize, p_id: Option<usize>, adj: &[Vec<(usize, usize)>]) -> usize {
e.tin[u] = e.timer;
fn dfs(e: &mut Env, v: usize, p_id: Option<usize>, adj: &[Vec<(usize, usize)>]) -> usize {
e.tin[v] = e.timer;
let (mut low, mut deg) = (e.timer, 0);
e.timer += 1;
for &(v, e_id) in &adj[u] {
assert_ne!(u, v);
for &(u, e_id) in &adj[v] {
assert_ne!(v, u);
if Some(e_id) == p_id {
continue;
}
if e.tin[v] == 0 {
if e.tin[u] == 0 {
let st_sz = e.st.len();
e.st.push(e_id);
let low_ch = dfs(e, v, Some(e_id), adj);
if low_ch >= e.tin[u] {
e.is_cut[u] = true;
let low_ch = dfs(e, u, Some(e_id), adj);
if low_ch >= e.tin[v] {
e.is_cut[v] = true;
for &id in e.st.iter().skip(st_sz) {
e.bcc_id[id] = e.num_bccs;
}
Expand All @@ -72,13 +72,13 @@ pub fn get_cuts(adj: &[Vec<(usize, usize)>], m: usize) -> (usize, Vec<bool>, Vec
}
low = low.min(low_ch);
deg += 1;
} else if e.tin[v] < e.tin[u] {
} else if e.tin[u] < e.tin[v] {
e.st.push(e_id);
low = low.min(e.tin[v]);
low = low.min(e.tin[u]);
}
}
if p_id.is_none() {
e.is_cut[u] = deg > 1;
e.is_cut[v] = deg > 1;
}
low
}
Expand Down
24 changes: 12 additions & 12 deletions src/graphs/dfs_order.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
fn dfs_preorder(u: usize, adj: &[Vec<usize>], seen: &mut [bool], order: &mut Vec<usize>) {
order.push(u);
seen[u] = true;
for &v in &adj[u] {
if !seen[v] {
dfs_preorder(v, adj, seen, order);
fn dfs_preorder(v: usize, adj: &[Vec<usize>], seen: &mut [bool], order: &mut Vec<usize>) {
order.push(v);
seen[v] = true;
for &u in &adj[v] {
if !seen[u] {
dfs_preorder(u, adj, seen, order);
}
}
}

fn dfs_postorder(u: usize, adj: &[Vec<usize>], seen: &mut [bool], order: &mut Vec<usize>) {
seen[u] = true;
for &v in &adj[u] {
if !seen[v] {
dfs_postorder(v, adj, seen, order);
fn dfs_postorder(v: usize, adj: &[Vec<usize>], seen: &mut [bool], order: &mut Vec<usize>) {
seen[v] = true;
for &u in &adj[v] {
if !seen[u] {
dfs_postorder(u, adj, seen, order);
}
}
order.push(u);
order.push(v);
}

fn get_dfs_order<F>(adj: &[Vec<usize>], dfs_order: F) -> Vec<usize>
Expand Down
10 changes: 5 additions & 5 deletions src/graphs/dijk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ pub fn dijk(adj: &[Vec<(usize, u64)>], s: usize) -> Vec<u64> {
let mut dist = vec![u64::MAX; n];
let mut q = std::collections::BinaryHeap::new();
q.push(Reverse((0, s)));
while let Some(Reverse((d, u))) = q.pop() {
if dist[u] <= d {
while let Some(Reverse((d, v))) = q.pop() {
if dist[v] <= d {
continue;
}
dist[u] = d;
for &(v, w) in &adj[u] {
q.push(Reverse((dist[u] + w, v)));
dist[v] = d;
for &(u, w) in &adj[v] {
q.push(Reverse((dist[v] + w, u)));
}
}
dist
Expand Down
44 changes: 22 additions & 22 deletions src/graphs/hld.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,27 @@ impl HLD {
let n = adj.len();
let mut p = vec![None; n];
let mut siz = vec![0; n];
for &u in get_dfs_postorder(adj).iter() {
adj[u].retain(|&v| siz[v] > 0);
siz[u] = 1;
for i in 0..adj[u].len() {
let v = adj[u][i];
p[v] = Some(u);
siz[u] += siz[v];
if siz[v] > siz[adj[u][0]] {
adj[u].swap(0, i);
for &v in get_dfs_postorder(adj).iter() {
adj[v].retain(|&u| siz[u] > 0);
siz[v] = 1;
for i in 0..adj[v].len() {
let u = adj[v][i];
p[u] = Some(v);
siz[v] += siz[u];
if siz[u] > siz[adj[v][0]] {
adj[v].swap(0, i);
}
}
}
let mut tin = vec![0; n];
let mut head = vec![0; n];
let mut d = vec![0; n];
let ord = get_dfs_preorder(adj);
for (i, &u) in ord.iter().enumerate() {
tin[u] = i;
for &v in &adj[u] {
d[v] = 1 + d[u];
head[v] = if v == adj[u][0] { head[u] } else { v };
for (i, &v) in ord.iter().enumerate() {
tin[v] = i;
for &u in &adj[v] {
d[u] = 1 + d[v];
head[u] = if u == adj[v][0] { head[v] } else { u };
}
}
HLD {
Expand Down Expand Up @@ -113,13 +113,13 @@ impl HLD {
);
}

/// Gets range representing the subtree of u
/// Gets range representing the subtree of v
///
/// # Complexity
/// - Time: O(1)
/// - Space: O(1)
pub fn sub_tree(&self, u: usize) -> Range<usize> {
self.tin[u] + self.vals_edges as usize..self.tin[u] + self.siz[u]
pub fn sub_tree(&self, v: usize) -> Range<usize> {
self.tin[v] + self.vals_edges as usize..self.tin[v] + self.siz[v]
}

/// Gets the lowest common ancestor of u and v
Expand Down Expand Up @@ -171,14 +171,14 @@ impl HLD {
/// # Complexity
/// - Time: O(log n)
/// - Space: O(1)
pub fn kth_par(&self, mut u: usize, mut k: usize) -> Option<usize> {
pub fn kth_par(&self, mut v: usize, mut k: usize) -> Option<usize> {
loop {
let len_path = self.tin[u] - self.tin[self.head[u]];
let len_path = self.tin[v] - self.tin[self.head[v]];
if k <= len_path {
return Some(self.ord[self.tin[u] - k]);
return Some(self.ord[self.tin[v] - k]);
}
match self.p[self.head[u]] {
Some(v) => u = v,
match self.p[self.head[v]] {
Some(u) => v = u,
None => return None,
}
k -= len_path + 1;
Expand Down
26 changes: 13 additions & 13 deletions src/graphs/hopcroft_karp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ impl HopcroftKarp {
for v in &mut e.mvc_r {
*v = false;
}
while let Some(u) = q.pop_front() {
e.mvc_l[u] = false;
for &v in &adj[u] {
e.mvc_r[v] = true;
if let Some(w) = e.r_to_l[v] {
if dist[w] > 1 + dist[u] {
dist[w] = 1 + dist[u];
while let Some(v) = q.pop_front() {
e.mvc_l[v] = false;
for &u in &adj[v] {
e.mvc_r[u] = true;
if let Some(w) = e.r_to_l[u] {
if dist[w] > 1 + dist[v] {
dist[w] = 1 + dist[v];
q.push_back(w);
}
} else {
Expand All @@ -91,17 +91,17 @@ impl HopcroftKarp {
if !found {
return e;
}
fn dfs(u: usize, adj: &[Vec<usize>], dist: &mut [usize], e: &mut HopcroftKarp) -> bool {
for &v in &adj[u] {
let w = e.r_to_l[v];
fn dfs(v: usize, adj: &[Vec<usize>], dist: &mut [usize], e: &mut HopcroftKarp) -> bool {
for &u in &adj[v] {
let w = e.r_to_l[u];
if w.is_none()
|| dist[u] + 1 == dist[w.unwrap()] && dfs(w.unwrap(), adj, dist, e)
|| dist[v] + 1 == dist[w.unwrap()] && dfs(w.unwrap(), adj, dist, e)
{
(e.l_to_r[u], e.r_to_l[v]) = (Some(v), Some(u));
(e.l_to_r[v], e.r_to_l[u]) = (Some(u), Some(v));
return true;
}
}
dist[u] = usize::MAX;
dist[v] = usize::MAX;
false
}
e.matching_siz += (0..lsz)
Expand Down
Loading

0 comments on commit 7fe3a97

Please sign in to comment.