From cfd9de520e6ab6b44e372a90c4f3ecfacf3bfeb6 Mon Sep 17 00:00:00 2001 From: Bruno Freitas Tissei <bft15@inf.ufpr.br> Date: Mon, 23 Apr 2018 17:56:23 +0200 Subject: [PATCH] Update Signed-off-by: Bruno Freitas Tissei <bft15@inf.ufpr.br> --- graph/articulations_bridges.cpp | 1 + graph/kosaraju.cpp | 2 ++ graph/kruskal.cpp | 1 + graph/lca.cpp | 7 ++++-- graph/topological_sort.cpp | 1 + math/modular_multiplicative_inverse.cpp | 2 ++ paradigm/ternary_search.cpp | 1 + problems/trees_partition.cpp | 32 +++++++++++++++++-------- string/kmp.cpp | 1 + structure/bit.cpp | 1 + structure/bit2d.cpp | 1 + structure/bitmask.cpp | 6 +++++ structure/disjoint_set.cpp | 2 ++ structure/lazy_segment_tree.cpp | 2 ++ structure/segment_tree.cpp | 2 ++ 15 files changed, 50 insertions(+), 12 deletions(-) diff --git a/graph/articulations_bridges.cpp b/graph/articulations_bridges.cpp index e2dc350..5065ed8 100644 --- a/graph/articulations_bridges.cpp +++ b/graph/articulations_bridges.cpp @@ -37,6 +37,7 @@ void dfs(int x) { } } + void tarjan(int v) { memset(parent, -1, sizeof parent); dfs(v); diff --git a/graph/kosaraju.cpp b/graph/kosaraju.cpp index 6808abd..b675ca5 100644 --- a/graph/kosaraju.cpp +++ b/graph/kosaraju.cpp @@ -21,6 +21,7 @@ void dfs(int x) { dfs(i); } + // Fill stack with DFS starting points to find SCC void fill_stack(int x) { cont[x] = true; @@ -32,6 +33,7 @@ void fill_stack(int x) { S.push(x); } + // Return number of SCC of a graph with n vertices int kosaraju(int n) { int scc = 0; diff --git a/graph/kruskal.cpp b/graph/kruskal.cpp index c8f13ef..ea6be8b 100644 --- a/graph/kruskal.cpp +++ b/graph/kruskal.cpp @@ -17,6 +17,7 @@ bool cmp(iii a, iii b) { //* return a.se > b.se } + // Return value of MST and build mst vector with edges that belong to the tree int kruskal() { sort(edges.begin(), edges.end(), cmp); diff --git a/graph/lca.cpp b/graph/lca.cpp index e66190c..ab7b9fd 100644 --- a/graph/lca.cpp +++ b/graph/lca.cpp @@ -6,8 +6,9 @@ * query -> O(log n) * Complexity (Space): O(n + m + n log n) * - * OBS: * = return sum path to LCA - * ** = return max value on path to LCA + * OBS: * = return sum path to LCA + * ** = return max value on path to LCA + * *** = used in both * and ** */ vector<ii> graph[MAX]; @@ -35,6 +36,7 @@ void dfs(int v, int p = -1, int c = 0) { dfs(u.fi, v, u.se); } + // Preprocess tree rooted at v void preprocess(int v) { memset(par, -1, sizeof par); @@ -42,6 +44,7 @@ void preprocess(int v) { dfs(v); } + // Return LCA (or sum or max) between p and q int query(int p, int q) { //*** int ans = 0; diff --git a/graph/topological_sort.cpp b/graph/topological_sort.cpp index b06a4db..a876c25 100644 --- a/graph/topological_sort.cpp +++ b/graph/topological_sort.cpp @@ -27,6 +27,7 @@ bool dfs(int x) { return false; } + // Return topological sort of graph with n vertices bool topological_sort(int n, vector<int> &tsort) { memset(cont, 0, sizeof cont); diff --git a/math/modular_multiplicative_inverse.cpp b/math/modular_multiplicative_inverse.cpp index ff0308e..ad96524 100644 --- a/math/modular_multiplicative_inverse.cpp +++ b/math/modular_multiplicative_inverse.cpp @@ -22,6 +22,7 @@ ll power(ll x, ll y) { return ans % MOD; } + // Find x where: // a*x === 1 (mod MOD) ll mod_inverse(ll a) { @@ -50,6 +51,7 @@ ll gcd_extended(ll a, ll b, ll &x, ll &y) { return g; } + // Find x where: // a*x === 1 (mod MOD) ll mod_inverse(ll a) { diff --git a/paradigm/ternary_search.cpp b/paradigm/ternary_search.cpp index c512ef4..f07e716 100644 --- a/paradigm/ternary_search.cpp +++ b/paradigm/ternary_search.cpp @@ -12,6 +12,7 @@ double f(double x) { return x * x; } + // Execute ternary search to find maximum or minimum between l and r double ternary_search(double l, double r) { double rt, lt; diff --git a/problems/trees_partition.cpp b/problems/trees_partition.cpp index 149cfe8..513acce 100644 --- a/problems/trees_partition.cpp +++ b/problems/trees_partition.cpp @@ -1,16 +1,22 @@ #include <bits/stdc++.h> -#define MAX 301010 -using namespace std; +#define MAX 301010 +#define MOD 1000000007 +#define inf 0x3f3f3f3f #define fi first #define se second - -#define mp make_pair +#define sz size() #define pb push_back -typedef unsigned long long ll; -typedef pair<ll,ll> ii; +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() + +using namespace std; + +typedef long long ll; +typedef pair<int,int> ii; + ll hsh[MAX]; vector<int> t1[MAX], t2[MAX]; @@ -32,6 +38,7 @@ ll dfs(int x, int par) { return down; } + ll solve(int x, int par) { ll down = hsh[x]; @@ -50,19 +57,23 @@ ll solve(int x, int par) { return down; } + int main() { + ios::sync_with_stdio(0); + cin.tie(0); + int n, x; - scanf("%d", &n); + cin >> n; mt19937_64 mt(time(NULL)); for (int i = 0; i < n - 1; ++i) { - scanf("%d", &x); + cin >> x; t1[i+1].pb(x-1); t1[x-1].pb(i+1); } for (int i = 0; i < n - 1; ++i) { - scanf("%d", &x); + cin >> x; t2[i+1].pb(x-1); t2[x-1].pb(i+1); } @@ -74,6 +85,7 @@ int main() { dfs(0, -1); solve(0, -1); - printf("%lld\n", ans); + + cout << ans << '\n'; return 0; } diff --git a/string/kmp.cpp b/string/kmp.cpp index f542fd4..81fe7bd 100644 --- a/string/kmp.cpp +++ b/string/kmp.cpp @@ -25,6 +25,7 @@ void preprocess(string patt) { } } + // Search for occurrences of patt in txt and add indexes of matches to occurs void search(string patt, string txt) { int i = 0, j = 0; diff --git a/structure/bit.cpp b/structure/bit.cpp index b7cbe39..e42f305 100644 --- a/structure/bit.cpp +++ b/structure/bit.cpp @@ -18,6 +18,7 @@ int query(int idx) { return sum; } + // Add a value (val) to a single position (idx) in the array (tree). void update(int idx, int val) { for (; idx <= MAX; idx += (idx & -idx)) diff --git a/structure/bit2d.cpp b/structure/bit2d.cpp index 211d4b2..63f3460 100644 --- a/structure/bit2d.cpp +++ b/structure/bit2d.cpp @@ -19,6 +19,7 @@ int query(int idx, int idy) { return sum; } + // Add a value (val) to a single position (idx,idy) in the array (tree). void update(int idx, int idy, int val) { for (; idx <= MAXN; idx += (idx & -idx)) diff --git a/structure/bitmask.cpp b/structure/bitmask.cpp index 65b05fd..7c33063 100644 --- a/structure/bitmask.cpp +++ b/structure/bitmask.cpp @@ -10,31 +10,37 @@ void set(ll &bitmask, int pos) { bitmask |= (1 << pos); } + // Set all bits in a bitmask with size n void set_all(ll &bitmask, int n) { bitmask = (1 << n) - 1; } + // Unset bit in position pos (1 to 0) void unset(ll &bitmask, int pos) { bitmask &= ~(1 << pos); } + // Unset all bits void unset_all(ll &bitmask) { bitmask = 0; } + // Get value of bit in position pos int get(ll bitmask, int pos) { return bitmask & (1 << pos); } + // Toggle value in position pos void toggle(ll &bitmask, int pos) { bitmask ^= (1 << pos); } + // Get position of least significant 1 int least_significant_one(ll bitmask) { return bitmask & (-bitmask); diff --git a/structure/disjoint_set.cpp b/structure/disjoint_set.cpp index b5311c3..22081e0 100644 --- a/structure/disjoint_set.cpp +++ b/structure/disjoint_set.cpp @@ -19,6 +19,7 @@ void make_set(int x) { sz[x] = 1; } + // Return set index from element x int find_set(int x) { if (par[x] != x) @@ -27,6 +28,7 @@ int find_set(int x) { return par[x]; } + // Make x and y belong to the same set void union_set(int x, int y) { int xroot = find_set(x); diff --git a/structure/lazy_segment_tree.cpp b/structure/lazy_segment_tree.cpp index b06b18d..d4eac73 100644 --- a/structure/lazy_segment_tree.cpp +++ b/structure/lazy_segment_tree.cpp @@ -30,6 +30,7 @@ int modification(int a, int b, int val) { return ((b - a) + 1) * val; } + // Update segment [i,j] by adding value val void update_tree(int i, int j, int val, int node = 1, int a = 0, int b = N) { if (lazy[node] != 0) { @@ -63,6 +64,7 @@ void update_tree(int i, int j, int val, int node = 1, int a = 0, int b = N) { tree[node] = tree[node * 2] + tree[node * 2 + 1]; } + // Return sum of [i,j] int query_tree(int i, int j, int node = 1, int a = 0, int b = N) { if (a > b || a > j || b < i) diff --git a/structure/segment_tree.cpp b/structure/segment_tree.cpp index d0f7762..dc0d56e 100644 --- a/structure/segment_tree.cpp +++ b/structure/segment_tree.cpp @@ -26,6 +26,7 @@ void build_tree(int node = 1, int a = 0, int b = N) { tree[node] = tree[node * 2] + tree[node * 2 + 1]; } + // Update position idx with value val void update_tree(int idx, int val, int node = 1, int a = 0, int b = N) { if (a > b || a > idx || b < idx) @@ -42,6 +43,7 @@ void update_tree(int idx, int val, int node = 1, int a = 0, int b = N) { tree[node] = tree[node * 2] + tree[node * 2 + 1]; } + // Return sum of [i,j] int query_tree(int i, int j, int node = 1, int a = 0, int b = N) { if (a > b || a > j || b < i) -- GitLab