From 7fe3a971835fdacd28403a01f2c8375c4513957d Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Sun, 28 Jul 2024 19:58:38 -0500 Subject: [PATCH] swap u and v (#103) * swap u and v * fix --------- Co-authored-by: Luke Videckis --- examples/data_structures/binary_trie.rs | 2 +- src/data_structures/binary_trie.rs | 16 ++++----- src/graphs/block_vertex_tree.rs | 10 +++--- src/graphs/bridges.rs | 18 +++++----- src/graphs/cent_decomp.rs | 40 +++++++++++----------- src/graphs/cuts.rs | 22 ++++++------- src/graphs/dfs_order.rs | 24 +++++++------- src/graphs/dijk.rs | 10 +++--- src/graphs/hld.rs | 44 ++++++++++++------------- src/graphs/hopcroft_karp.rs | 26 +++++++-------- src/graphs/lca.rs | 18 +++++----- src/graphs/scc.rs | 20 +++++------ src/helpers/unsafe_recursive_closure.rs | 8 ++--- 13 files changed, 129 insertions(+), 129 deletions(-) diff --git a/examples/data_structures/binary_trie.rs b/examples/data_structures/binary_trie.rs index 4a6ac218..1c5a5579 100644 --- a/examples/data_structures/binary_trie.rs +++ b/examples/data_structures/binary_trie.rs @@ -26,7 +26,7 @@ fn main() { trie.update(x, -1); } } - _ => println!("{}", trie.min_xor(x)), + _ => println!("{}", trie.walk(x)), } } } diff --git a/src/data_structures/binary_trie.rs b/src/data_structures/binary_trie.rs index 05b601ed..f1df9999 100644 --- a/src/data_structures/binary_trie.rs +++ b/src/data_structures/binary_trie.rs @@ -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, @@ -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; diff --git a/src/graphs/block_vertex_tree.rs b/src/graphs/block_vertex_tree.rs index 7e31e4df..a44e1d87 100644 --- a/src/graphs/block_vertex_tree.rs +++ b/src/graphs/block_vertex_tree.rs @@ -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); /// } /// } /// ``` diff --git a/src/graphs/bridges.rs b/src/graphs/bridges.rs index 666bb4c4..d2d19e3a 100644 --- a/src/graphs/bridges.rs +++ b/src/graphs/bridges.rs @@ -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 /// ``` @@ -41,21 +41,21 @@ pub fn get_bridges(adj: &[Vec<(usize, usize)>], m: usize) -> (usize, Vec, two_edge_ccid: vec![0; n], st: Vec::with_capacity(n), }; - fn dfs(e: &mut Env, u: usize, p_id: Option, adj: &[Vec<(usize, usize)>]) -> usize { - e.tin[u] = e.timer; + fn dfs(e: &mut Env, v: usize, p_id: Option, 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; } diff --git a/src/graphs/cent_decomp.rs b/src/graphs/cent_decomp.rs index cfd975c4..b605ae8b 100644 --- a/src/graphs/cent_decomp.rs +++ b/src/graphs/cent_decomp.rs @@ -1,40 +1,40 @@ //! # Centroid Decomposition -fn calc_sz(adj: &[Vec], 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], 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], - 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); } } diff --git a/src/graphs/cuts.rs b/src/graphs/cuts.rs index 9b01aeb0..38028115 100644 --- a/src/graphs/cuts.rs +++ b/src/graphs/cuts.rs @@ -49,21 +49,21 @@ pub fn get_cuts(adj: &[Vec<(usize, usize)>], m: usize) -> (usize, Vec, Vec bcc_id: vec![0; m], st: Vec::with_capacity(m), }; - fn dfs(e: &mut Env, u: usize, p_id: Option, adj: &[Vec<(usize, usize)>]) -> usize { - e.tin[u] = e.timer; + fn dfs(e: &mut Env, v: usize, p_id: Option, 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; } @@ -72,13 +72,13 @@ pub fn get_cuts(adj: &[Vec<(usize, usize)>], m: usize) -> (usize, Vec, 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 } diff --git a/src/graphs/dfs_order.rs b/src/graphs/dfs_order.rs index cd4ef984..3964e11b 100644 --- a/src/graphs/dfs_order.rs +++ b/src/graphs/dfs_order.rs @@ -1,21 +1,21 @@ -fn dfs_preorder(u: usize, adj: &[Vec], seen: &mut [bool], order: &mut Vec) { - 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], seen: &mut [bool], order: &mut Vec) { + 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], seen: &mut [bool], order: &mut Vec) { - seen[u] = true; - for &v in &adj[u] { - if !seen[v] { - dfs_postorder(v, adj, seen, order); +fn dfs_postorder(v: usize, adj: &[Vec], seen: &mut [bool], order: &mut Vec) { + 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(adj: &[Vec], dfs_order: F) -> Vec diff --git a/src/graphs/dijk.rs b/src/graphs/dijk.rs index a53b66ee..133be19a 100644 --- a/src/graphs/dijk.rs +++ b/src/graphs/dijk.rs @@ -24,13 +24,13 @@ pub fn dijk(adj: &[Vec<(usize, u64)>], s: usize) -> Vec { 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 diff --git a/src/graphs/hld.rs b/src/graphs/hld.rs index eb93a067..be475619 100644 --- a/src/graphs/hld.rs +++ b/src/graphs/hld.rs @@ -55,15 +55,15 @@ 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); } } } @@ -71,11 +71,11 @@ impl HLD { 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 { @@ -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 { - self.tin[u] + self.vals_edges as usize..self.tin[u] + self.siz[u] + pub fn sub_tree(&self, v: usize) -> Range { + self.tin[v] + self.vals_edges as usize..self.tin[v] + self.siz[v] } /// Gets the lowest common ancestor of u and v @@ -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 { + pub fn kth_par(&self, mut v: usize, mut k: usize) -> Option { 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; diff --git a/src/graphs/hopcroft_karp.rs b/src/graphs/hopcroft_karp.rs index 46623cac..9cc37a6c 100644 --- a/src/graphs/hopcroft_karp.rs +++ b/src/graphs/hopcroft_karp.rs @@ -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 { @@ -91,17 +91,17 @@ impl HopcroftKarp { if !found { return e; } - fn dfs(u: usize, adj: &[Vec], 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], 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) diff --git a/src/graphs/lca.rs b/src/graphs/lca.rs index 16adf1c3..5940fd35 100644 --- a/src/graphs/lca.rs +++ b/src/graphs/lca.rs @@ -47,24 +47,24 @@ impl LCA { let mut p = vec![None; n]; let mut d = vec![0; n]; let order = get_dfs_preorder(adj); - for (i, &u) in order.iter().enumerate() { - tin[u] = i; - for &v in &adj[u] { - if Some(v) != p[u] { - (p[v], d[v]) = (Some(u), d[u] + 1); + for (i, &v) in order.iter().enumerate() { + tin[v] = i; + for &u in &adj[v] { + if Some(u) != p[v] { + (p[u], d[u]) = (Some(v), d[v] + 1); } } } let mut siz = vec![1; n]; - for &u in order.iter().rev() { - if let Some(par) = p[u] { - siz[par] += siz[u]; + for &v in order.iter().rev() { + if let Some(par) = p[v] { + siz[par] += siz[v]; } } LCA { p, rmq: RMQ::new( - &order.iter().map(|&u| (d[u], u)).collect::>(), + &order.iter().map(|&v| (d[v], v)).collect::>(), |&x, &y| if x.0 < y.0 { x } else { y }, ), d, diff --git a/src/graphs/scc.rs b/src/graphs/scc.rs index 9d744e16..af28827e 100644 --- a/src/graphs/scc.rs +++ b/src/graphs/scc.rs @@ -4,7 +4,7 @@ use crate::graphs::dfs_order::get_dfs_postorder; /// # Guarantees /// - 0..num_sccs is a topological order of the SCCs -/// - 0 <= scc_id\[u\] < num_sccs +/// - 0 <= scc_id\[v\] < num_sccs /// - for each edge u -> v: scc_id\[u\] <= scc_id\[v\] /// /// # Example @@ -29,19 +29,19 @@ pub fn get_sccs(adj: &[Vec]) -> (usize, Vec) { let n = adj.len(); let rv_adj = { let mut rv_adj = vec![vec![]; n]; - for (u, vs) in adj.iter().enumerate() { - for &v in vs { - rv_adj[v].push(u); + for (v, vs) in adj.iter().enumerate() { + for &u in vs { + rv_adj[u].push(v); } } rv_adj }; - fn dfs(u: usize, adj: &[Vec], seen: &mut [bool], scc_id: &mut [usize], num_sccs: usize) { - scc_id[u] = num_sccs; - seen[u] = true; - for &v in &adj[u] { - if !seen[v] { - dfs(v, adj, seen, scc_id, num_sccs); + fn dfs(v: usize, adj: &[Vec], seen: &mut [bool], scc_id: &mut [usize], num_sccs: usize) { + scc_id[v] = num_sccs; + seen[v] = true; + for &u in &adj[v] { + if !seen[u] { + dfs(u, adj, seen, scc_id, num_sccs); } } } diff --git a/src/helpers/unsafe_recursive_closure.rs b/src/helpers/unsafe_recursive_closure.rs index 58c8f4f6..ef2bc434 100644 --- a/src/helpers/unsafe_recursive_closure.rs +++ b/src/helpers/unsafe_recursive_closure.rs @@ -24,10 +24,10 @@ /// adj[i].push(i / 2); /// } /// assert_eq!(2 * (n - 1), adj.iter().map(|elem| elem.len()).sum()); -/// let mut dfs = unsafe_recursive_closure!(|dfs, u: usize, p: Option| { -/// adj[u].retain(|&v| Some(v) != p); -/// for &v in &adj[u] { -/// dfs(v, Some(u)); +/// let mut dfs = unsafe_recursive_closure!(|dfs, v: usize, p: Option| { +/// adj[v].retain(|&v| Some(v) != p); +/// for &u in &adj[v] { +/// dfs(u, Some(v)); /// } /// }); /// dfs(0, None);