From bef2be80e88eb2192952364790a7f1c92738e898 Mon Sep 17 00:00:00 2001 From: Bruno Freitas Tissei <bft15@inf.ufpr.br> Date: Wed, 13 Feb 2019 16:21:08 -0200 Subject: [PATCH] Small fixes Signed-off-by: Bruno Freitas Tissei <bft15@inf.ufpr.br> --- graph/lca.cpp | 2 ++ graph/tarjan.cpp | 2 +- graph/topological_sort.cpp | 2 +- math/modular_multiplicative_inverse.cpp | 4 ++-- paradigm/kadane.cpp | 7 ++++++- string/kmp.cpp | 2 +- structure/bit.cpp | 2 +- structure/bit2d.cpp | 2 +- structure/disjoint_set.cpp | 15 +++++++-------- structure/policy_tree.cpp | 2 ++ 10 files changed, 24 insertions(+), 16 deletions(-) diff --git a/graph/lca.cpp b/graph/lca.cpp index 77b874c..5f43a33 100644 --- a/graph/lca.cpp +++ b/graph/lca.cpp @@ -11,6 +11,8 @@ * *** = used in both * and ** */ +#define MAXLOG 20 //log2(MAX) + vector<int> graph[MAX]; //*** vector<ii> int h[MAX]; diff --git a/graph/tarjan.cpp b/graph/tarjan.cpp index f9e1952..bfcccdc 100644 --- a/graph/tarjan.cpp +++ b/graph/tarjan.cpp @@ -13,7 +13,7 @@ int ncomp, ind; int cont[MAX], parent[MAX]; int low[MAX], L[MAX]; -// Fills scc with strongly connected components of graph +// Fills SCC with strongly connected components of graph void dfs(int x) { L[x] = ind; low[x] = ind++; diff --git a/graph/topological_sort.cpp b/graph/topological_sort.cpp index d5b6739..b4a7169 100644 --- a/graph/topological_sort.cpp +++ b/graph/topological_sort.cpp @@ -28,7 +28,7 @@ bool dfs(int x) { } -// Returns if graph contains cycle or not, and fills tsort vector with +// Returns whether graph contains cycle or not, and fills tsort vector with // topological sort of the graph bool topological_sort(int n, vector<int> &tsort) { mset(cont, 0); diff --git a/math/modular_multiplicative_inverse.cpp b/math/modular_multiplicative_inverse.cpp index ad96524..8a05798 100644 --- a/math/modular_multiplicative_inverse.cpp +++ b/math/modular_multiplicative_inverse.cpp @@ -6,7 +6,7 @@ */ // ========== Fermat's Little Theorem ========== -// Used if m is prime +// Used when m is prime ll power(ll x, ll y) { ll ans = 1; @@ -31,7 +31,7 @@ ll mod_inverse(ll a) { // ========== Extended Euclidean Algorithm ========== -// Used if m and a are coprime +// Used when m and a are coprime // return gcd(a, b) and find x, y where: // a*x + b*y = gcd(a, b) diff --git a/paradigm/kadane.cpp b/paradigm/kadane.cpp index 8ad7c71..46d5b4e 100644 --- a/paradigm/kadane.cpp +++ b/paradigm/kadane.cpp @@ -1,5 +1,5 @@ /** - * Kadane + * Kadane - Largest Sum Contiguous Subarray * * Complexity (Time): O(n + m) * Complexity (Space): O(n + m) @@ -8,6 +8,7 @@ int v[MAX]; int kadane() { + // Maximum so far (msf), Maximum ending here (meh). int msf = -0x3f3f3f3f, meh = 0; int start = 0, end = 0, s = 0; @@ -15,12 +16,16 @@ int kadane() { for (int i = 0; i < n; ++i) { meh += v[i]; + // Store maximum so far as well as starting and ending position + // of the subsequence if (msf < meh) { msf = meh; start = s; end = i; } + // If maximum ending here is negative, then it's definitely + // better to restart if (meh < 0) { meh = 0; s = i + 1; diff --git a/string/kmp.cpp b/string/kmp.cpp index 81fe7bd..741289a 100644 --- a/string/kmp.cpp +++ b/string/kmp.cpp @@ -1,5 +1,5 @@ /** - * Knuth-Morris-Pratt - KMP + * Knuth-Morris-Pratt (KMP) * * Complexity (Time): * preprocess -> O(m) diff --git a/structure/bit.cpp b/structure/bit.cpp index e42f305..69d3ab1 100644 --- a/structure/bit.cpp +++ b/structure/bit.cpp @@ -1,5 +1,5 @@ /** - * Binary Indexed Tree - BIT + * Binary Indexed Tree (BIT) * * Complexity (Time): * Update -> O(log n) diff --git a/structure/bit2d.cpp b/structure/bit2d.cpp index 63f3460..786faaa 100644 --- a/structure/bit2d.cpp +++ b/structure/bit2d.cpp @@ -1,5 +1,5 @@ /** - * Binary Indexed Tree 2D - BIT2D + * Binary Indexed Tree 2D (BIT2D) * * Complexity (Time): * Update -> O(log^2 n) diff --git a/structure/disjoint_set.cpp b/structure/disjoint_set.cpp index 22081e0..e8ebabe 100644 --- a/structure/disjoint_set.cpp +++ b/structure/disjoint_set.cpp @@ -10,13 +10,13 @@ int par[MAX]; int h[MAX]; -int sz[MAX]; +int size[MAX]; // Initialize element x void make_set(int x) { par[x] = x; h[x] = 0; - sz[x] = 1; + size[x] = 1; } @@ -39,13 +39,12 @@ void union_set(int x, int y) { if (h[xroot] < h[yroot]) { par[xroot] = yroot; - sz[yroot] += sz[xroot]; - } else if (h[xroot] > h[yroot]) { - par[yroot] = xroot; - sz[xroot] += sz[yroot]; + size[yroot] += size[xroot]; } else { par[yroot] = xroot; - sz[xroot] += sz[yroot]; - h[xroot]++; + size[xroot] += size[yroot]; + + if (h[xroot] == h[yroot]) + h[xroot]++; } } diff --git a/structure/policy_tree.cpp b/structure/policy_tree.cpp index d93aca4..ee6cff3 100644 --- a/structure/policy_tree.cpp +++ b/structure/policy_tree.cpp @@ -1,6 +1,8 @@ /** * Policy Tree * + * A set-like STL structure with order statistics + * * Complexity (Time): * insert -> O(log n) * erase -> O(log n) -- GitLab