diff --git a/graph/articulations_bridges.cpp b/graph/articulations_bridges.cpp index e2dc35054b207947bc70d2c1b83e032695e2521a..5065ed82442ae8b9e365dd6a02c1dadbe502ff5e 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 6808abde3d9bcb23e8be79afe6861438237a82e5..b675ca58d375401c2fb68e5a27b3f6074f6824bd 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 c8f13ef57f8febf17d50a782daeb5d4db6c3a5ff..ea6be8b17d50a8783f45b942129acdf782a93af7 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 e66190c184a9ae10dc52ed4305098c195782fb9b..ab7b9fdae3d1e487bbfd0002853abd0f9aea2a7f 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 b06a4db687158400cf685f02aee915a9b3e55ba8..a876c25ee1fcffb9a6fcac5d293a3ca3f0ac894f 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 ff0308eba013106f4fd4f72f35ac079f016f24bc..ad965244f58f4fa7c86d3f302cf00fdf13e53f54 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 c512ef4bc6458c466258d3142250f3a08f08d23f..f07e7168a0bfab259579fa63e415ceba256c2698 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 149cfe8e0ae32ce0dcd20c9da4a6943467e04b13..513acce0481a5b6ca50046a3684edcc90ed6ac13 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 f542fd41ea4807aab496af0b8147e8e67beaf132..81fe7bd0a59f8a7f81147d3e1aba041c8a985e82 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 b7cbe39611e83756854e547eaffb43d136944c14..e42f30525b1c3462135c410fe160ac75e71e73fd 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 211d4b2d8353c5a0cbe2635f775ad4baa00ab3fd..63f34601066b506963e33d5cd7699d973ce03bab 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 65b05fdd1c586ae3ea06d7cc7e29bcca1f0f6550..7c330630690f52c28f5370c23517de7a754e8507 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 b5311c3cfa2bb026d010d1b3f163175ad74060d6..22081e04a18b63e061fb073403fcb50dcfb282ed 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 b06b18d67030a4855c6020dc37996094feea4d38..d4eac731417b7c71c8f736d7def720f7d96ac30f 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 d0f7762180215a813ca718282dc58da4a6d8b2e7..dc0d56e15305ee420171d5fbab3c15442ffd0268 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)