From 58fc65df2a86c8e33f79c860a5e61a63d12b001f Mon Sep 17 00:00:00 2001 From: the-hyp0cr1t3 Date: Thu, 9 Sep 2021 23:21:07 +0530 Subject: [PATCH] Update Snippets --- .../Euler Circuit (directed).cpp" | 41 +++++++++++++ .../Euler Circuit (undirected).cpp" | 51 +++++++++++++++++ "\346\234\253 Snippets/Euler Circuit.cpp" | 48 ---------------- .../Extended Euclidean algorithm.cpp" | 15 +++-- "\346\234\253 Snippets/FFT.cpp" | 57 ++++++++++--------- .../Factorization.cpp" | 2 + "\346\234\253 Snippets/Floyd Warshall.cpp" | 55 ++++++------------ .../Gaussian elimination.cpp" | 25 ++++---- "\346\234\253 Snippets/Hashing.cpp" | 16 +++++- "\346\234\253 Snippets/KMP.cpp" | 5 +- "\346\234\253 Snippets/NTT.cpp" | 24 +++++--- 11 files changed, 197 insertions(+), 142 deletions(-) create mode 100644 "\346\234\253 Snippets/Euler Circuit (directed).cpp" create mode 100644 "\346\234\253 Snippets/Euler Circuit (undirected).cpp" delete mode 100644 "\346\234\253 Snippets/Euler Circuit.cpp" rename "\346\234\253 Snippets/Factorize.cpp" => "\346\234\253 Snippets/Factorization.cpp" (98%) diff --git "a/\346\234\253 Snippets/Euler Circuit (directed).cpp" "b/\346\234\253 Snippets/Euler Circuit (directed).cpp" new file mode 100644 index 0000000..7d7e63a --- /dev/null +++ "b/\346\234\253 Snippets/Euler Circuit (directed).cpp" @@ -0,0 +1,41 @@ +/* Eulerian Circuit (directed) */ +// Hierholzer’s Algorithm + +/* + CPH: + https://cses.fi/book/book.pdf#page=183 + Hello world: + https://cses.fi/problemset/task/1693 +*/ + +vector indeg(n), outdeg(n); +vector> g(n); +for(i = 0; i < m; i++) { + int u, v; cin >> u >> v; + outdeg[--u]++; indeg[--v]++; + g[u].push_back(v); +} + +bool bad = false; +for(i = 1; i < n-1; i++) + bad |= outdeg[i] ^ indeg[i]; + +bad |= outdeg[0]-indeg[0] ^ 1; +bad |= indeg[n-1]-outdeg[n-1] ^ 1; + +if(bad) + // IMPOSSIBLE + +vector tour; +tour.reserve(m+1); +Y([&](auto dfs, int v) -> void { + while(outdeg[v]-- > 0) + dfs(g[v][outdeg[v]]); + tour.push_back(v); +})(0); + +for(i = 0; i < n; i++) + if(outdeg[i] > 0) + // IMPOSSIBLE + +reverse(tour.begin(), tour.end()); diff --git "a/\346\234\253 Snippets/Euler Circuit (undirected).cpp" "b/\346\234\253 Snippets/Euler Circuit (undirected).cpp" new file mode 100644 index 0000000..578914f --- /dev/null +++ "b/\346\234\253 Snippets/Euler Circuit (undirected).cpp" @@ -0,0 +1,51 @@ +/* Eulerian Circuit (undirected) */ +// Hierholzer’s Algorithm + +/* + CPH: + https://cses.fi/book/book.pdf#page=183 + Hello world: + https://cses.fi/problemset/task/1691 +*/ + +struct edge_t { + int u{-1}, v{-1}; bool done{false}; + edge_t() = default; + edge_t(int u, int v) : u(u), v(v) {} +}; + +vector deg(n); +vector edges; +edges.reserve(m); +vector> g(n); + +for(i = 0; i < m; i++) { + int u, v; cin >> u >> v; + deg[--u]++; deg[--v]++; + edges.emplace_back(u, v); + g[u].push_back(&edges.back()); + g[v].push_back(&edges.back()); +} + +bool bad = false; +for(i = 0; i < n; i++) + bad |= deg[i] & 1; + +if(bad) + // IMPOSSIBLE + +vector tour; +tour.reserve(m+1); +Y([&](auto dfs, int v) -> void { + while(deg[v]-- > 0) { + auto e = g[v][deg[v]]; + if(!e->done) + e->done = true, dfs(e->u ^ e->v ^ v); + } tour.push_back(v); +})(0); + +for(i = 0; i < n; i++) + if(deg[i] > 0) + // IMPOSSIBLE + +reverse(tour.begin(), tour.end()); diff --git "a/\346\234\253 Snippets/Euler Circuit.cpp" "b/\346\234\253 Snippets/Euler Circuit.cpp" deleted file mode 100644 index 9f6aaea..0000000 --- "a/\346\234\253 Snippets/Euler Circuit.cpp" +++ /dev/null @@ -1,48 +0,0 @@ -//Hierholzer’s Algorithm for Euler circuit -#include -#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); -using namespace std; -#define sz(x) (int)(x).size() -const int N = 1e5 + 5; -vector g[N], circuit; -int n, m; - -void makeG() { - for(int i = 0; i < m; i++) { - int u, v; - cin >> u >> v; - u--, v--; - g[u].push_back(v); - } -} - -void EulerCycle(int v) { - stack curpath; - curpath.push(v); - int cur = v; - while(!curpath.empty()) { - if(sz(g[cur])) { - int nxt = g[cur].back(); - curpath.push(nxt); - g[cur].pop_back(); - cur = nxt; - } - else { - cur = curpath.top(); - circuit.push_back(cur+1); - curpath.pop(); - } - } - reverse(circuit.begin(), circuit.end()); -} - -int32_t main() { - IOS; - cin >> n >> m; - circuit.reserve(m+2); - makeG(); - EulerCycle(0); - for(auto& x: circuit) - cout << x << " "; - return 0; -} \ No newline at end of file diff --git "a/\346\234\253 Snippets/Extended Euclidean algorithm.cpp" "b/\346\234\253 Snippets/Extended Euclidean algorithm.cpp" index a18414b..fb15dd5 100644 --- "a/\346\234\253 Snippets/Extended Euclidean algorithm.cpp" +++ "b/\346\234\253 Snippets/Extended Euclidean algorithm.cpp" @@ -1,6 +1,9 @@ /* Extended Euclidean algorithm and Linear Diophantine Equation solver */ -// https://cp-algorithms.com/algebra/extended-euclid-algorithm.html -// https://cp-algorithms.com/algebra/linear-diophantine-equation.html + +/* + https://cp-algorithms.com/algebra/extended-euclid-algorithm.html + https://cp-algorithms.com/algebra/linear-diophantine-equation.html +*/ // ax + by = gcd(a, b) : returns {x, y, g} auto extended = [](int64_t a, int64_t b) { @@ -17,7 +20,7 @@ auto extended = [](int64_t a, int64_t b) { auto diophantine = [&extended](int64_t a, int64_t b, int64_t c) { if(!a and !b) return make_tuple(!c, 42LL, 17LL, 0LL); auto [x, y, g] = extended(a, b); - return make_tuple(!(c % g), c/g * x, c/g * y, abs(g)); + return make_tuple(!(c % g), c / g * x, c / g * y, abs(g)); }; // x = x0 + λ b/g @@ -25,9 +28,9 @@ auto diophantine = [&extended](int64_t a, int64_t b, int64_t c) { auto diophantine_range = [&extended] (int64_t a, int64_t b, int64_t c, int64_t lx, int64_t rx, int64_t ly, int64_t ry) { assert(a and b); - + auto [x, y, g] = extended(a, b); - x *= c/g; y *= c/g; g = abs(g); + x *= c / g; y *= c / g; g = abs(g); a /= g; b /= g; if(c % g) return 0LL; @@ -64,4 +67,4 @@ auto diophantine_range = [&extended] (int64_t a, int64_t b, int64_t c, if(_lx > _rx) return 0LL; return (_rx - _lx) / abs(b) + 1; -}; +}; \ No newline at end of file diff --git "a/\346\234\253 Snippets/FFT.cpp" "b/\346\234\253 Snippets/FFT.cpp" index 78163de..6919c87 100644 --- "a/\346\234\253 Snippets/FFT.cpp" +++ "b/\346\234\253 Snippets/FFT.cpp" @@ -1,7 +1,33 @@ -// Iterative FFT -// Inspired by Neal's amazing template -// [neal's submission](https://codeforces.com/contest/1334/submission/76217102) -// [mine](https://codeforces.com/contest/993/submission/119625892) +/* Iterative FFT */ + +/* + Inspired by Neal's amazing template + [neal's submission](https://codeforces.com/contest/1334/submission/76217102) + [mine](https://codeforces.com/contest/993/submission/119625892) + + Concise: + https://cp-algorithms.com/algebra/fft.html (Learn from somewhere else if it's your first time) + + FFT problems: + https://www.spoj.com/problems/POLYMUL/ + https://www.spoj.com/problems/MAXMATCH/ + https://www.spoj.com/problems/ADAMATCH/ + https://codeforces.com/contest/993/problem/E + https://www.codechef.com/problems/COUNTWAY/ + https://codeforces.com/contest/954/problem/I + https://codeforces.com/contest/958/problem/F3 + https://open.kattis.com/problems/kinversions + https://codeforces.com/contest/1398/problem/G + https://codeforces.com/gym/101667/attachments/download/6491/problemset-2017.pdf (Problem H) + https://codeforces.com/contest/1251/problem/F (nice :3) + https://codeforces.com/contest/754/problem/E + https://codeforces.com/gym/102920/problem/H + + NTT problems: + https://codeforces.com/contest/1096/problem/G + https://codeforces.com/contest/632/problem/E +*/ + namespace FFT { using float_t = double; @@ -220,26 +246,3 @@ namespace FFT { return res; } - -/* -Concise: https://cp-algorithms.com/algebra/fft.html (Learn from somewhere else if it's your first time) - -FFT -https://www.spoj.com/problems/POLYMUL/ -https://www.spoj.com/problems/MAXMATCH/ -https://www.spoj.com/problems/ADAMATCH/ -https://codeforces.com/contest/993/problem/E -https://www.codechef.com/problems/COUNTWAY/ -https://codeforces.com/contest/954/problem/I -https://codeforces.com/contest/958/problem/F3 -https://open.kattis.com/problems/kinversions -https://codeforces.com/contest/1398/problem/G -https://codeforces.com/gym/101667/attachments/download/6491/problemset-2017.pdf (Problem H) -https://codeforces.com/contest/1251/problem/F (nice :3) -https://codeforces.com/contest/754/problem/E -https://codeforces.com/gym/102920/problem/H - -NTT -https://codeforces.com/contest/1096/problem/G -https://codeforces.com/contest/632/problem/E -*/ \ No newline at end of file diff --git "a/\346\234\253 Snippets/Factorize.cpp" "b/\346\234\253 Snippets/Factorization.cpp" similarity index 98% rename from "\346\234\253 Snippets/Factorize.cpp" rename to "\346\234\253 Snippets/Factorization.cpp" index 8bba766..24accf5 100644 --- "a/\346\234\253 Snippets/Factorize.cpp" +++ "b/\346\234\253 Snippets/Factorization.cpp" @@ -1,4 +1,6 @@ +/* Prime factorization */ /* [neal](https://codeforces.com/profile/neal) orz */ + // Prime factorizes n in worst case O(sqrt n). vector> prime_factorize(int64_t n) { assert(n >= 1); diff --git "a/\346\234\253 Snippets/Floyd Warshall.cpp" "b/\346\234\253 Snippets/Floyd Warshall.cpp" index a1962ad..b1d56c7 100644 --- "a/\346\234\253 Snippets/Floyd Warshall.cpp" +++ "b/\346\234\253 Snippets/Floyd Warshall.cpp" @@ -1,43 +1,24 @@ /* Floyd-Warshall's all pair shortest paths */ -// (https://cp-algorithms.com/graph/all-pair-shortest-path-floyd-warshall.html) -int d[N][N]; -vector ans; +/* + https://github.com/the-hyp0cr1t3/CC/blob/master/Beginner%20Topics/%5BS5%5D%20Do%20you%20understand%20the%20graphity%20of%20this%20situation/%5BEP%202%5D%20Shortest%20paths/%5BPt%203%5D%20Floyd-Warshall.md +*/ -for(i = 0; i < n; i++) { - for(j = 0; j < n; j++) { - d[i][j] = INF; - p[i][j] = -1; - } - d[i][i] = 0; - p[i][i] = 0; -} +auto chmin = [](auto& A, auto&& B) { return B < A? A = B, true : false; }; -for(i = 0; i < m; i++) { - int u, v, cost; - cin >> u >> v >> cost; - u--, v--; - d[u][v] = d[v][u] = cost; - p[u][v] = v; - p[v][u] = u; -} +vector d(n, vector(n, INF)), path(n, vector(n, -1)); +for(auto [u, v, w]: edges) + d[u][v] = w, path[u][v] = v; +// d[i][i] = 0; -void warshall() { - for(int k = 0; k < n; k++) { - for(int i = 0; i < n; i++) { - for(int j = 0; j < n; j++) { - if(d[i][k]^INF and d[k][j]^INF and (d[i][k]+d[k][j] < d[i][j])) { - d[i][j] = d[i][k] + d[k][j]; - p[i][j] = p[i][k]; - } - } - } - } -}; - -void path(int u, int v) { - ans.pb(u+1); - if(u == v) return; - path(p[u][v], v); -} +for(int k = 0; k < n; k++) + for(int i = 0; i < n; i++) + for(int j = 0; j < n; j++) + if(chmin(d[i][j], d[i][k] + d[k][j])) + path[i][j] = path[i][k]; +vector ans{ u }; +while(u ^ v) { + u = path[u][v]; + ans.push_back(u); +} \ No newline at end of file diff --git "a/\346\234\253 Snippets/Gaussian elimination.cpp" "b/\346\234\253 Snippets/Gaussian elimination.cpp" index 30fe2e3..ac729ba 100644 --- "a/\346\234\253 Snippets/Gaussian elimination.cpp" +++ "b/\346\234\253 Snippets/Gaussian elimination.cpp" @@ -1,4 +1,16 @@ -// https://codeforces.com/blog/entry/68953 +/* Gaussian Elimination */ + +/* + Blog: + https://codeforces.com/blog/entry/68953 + General RREF: + https://cp-algorithms.com/linear_algebra/linear-system-gauss.html + Problems: + https://codeforces.com/contest/1101/problem/G + https://codeforces.com/contest/895/problem/C + https://www.codechef.com/problems/XORCMPNT + https://codeforces.com/gym/102920/problem/J +*/ // Use int or int64_t for upto 63 bits struct BinarySpan { @@ -65,13 +77,4 @@ int gauss(int n, int m, vector>& a) { } return pivots; -} - -// General RREF: [https://cp-algorithms.com/linear_algebra/linear-system-gauss.html] - -/* -https://codeforces.com/contest/1101/problem/G -https://codeforces.com/contest/895/problem/C -https://www.codechef.com/problems/XORCMPNT -https://codeforces.com/gym/102920/problem/J -*/ \ No newline at end of file +} \ No newline at end of file diff --git "a/\346\234\253 Snippets/Hashing.cpp" "b/\346\234\253 Snippets/Hashing.cpp" index 6e0b3d3..2a8d6bf 100644 --- "a/\346\234\253 Snippets/Hashing.cpp" +++ "b/\346\234\253 Snippets/Hashing.cpp" @@ -1,3 +1,16 @@ +/* Hashing templates - custom hash, gp_hash_table, string hashing */ + +/* + - pbds gp hash table (better than unordered_map) + https://codeforces.com/blog/entry/60737 + - custom hash, blowing up unordered_map + https://codeforces.com/blog/entry/62393 + - string hashing + for prefix implementations, see + https://codeforces.com/contest/1469/submission/102644985 + https://codeforces.com/contest/1469/submission/102644888 +*/ + #include namespace Hashing { using hash_t = pair; @@ -109,5 +122,4 @@ int main() { auto [x, y] = B(); cout << x << ' ' << y; } -*/ -// for prefix implementations, see (https://codeforces.com/contest/1469/submission/102644985) and (https://codeforces.com/contest/1469/submission/102644888) +*/ \ No newline at end of file diff --git "a/\346\234\253 Snippets/KMP.cpp" "b/\346\234\253 Snippets/KMP.cpp" index 009d56c..a031aa4 100644 --- "a/\346\234\253 Snippets/KMP.cpp" +++ "b/\346\234\253 Snippets/KMP.cpp" @@ -1,4 +1,5 @@ -/* Knuth–Morris–Pratt (KMP) algorithm */ +/* KMP */ + /* https://discuss.codechef.com/t/tutorial-prefix-function-pattern-matching-supposedly-kmp/67531 https://cp-algorithms.com/string/prefix-function.html @@ -11,7 +12,7 @@ // 0000 123012 auto generate_pi = [&] (const string& s) { // s = (pattern + '#' + text) - int n = sz(s); + int n = s.size(); vector pi(n); for(i = 1; i < n; i++) { int p = pi[i-1]; diff --git "a/\346\234\253 Snippets/NTT.cpp" "b/\346\234\253 Snippets/NTT.cpp" index 6aba957..c8fda94 100644 --- "a/\346\234\253 Snippets/NTT.cpp" +++ "b/\346\234\253 Snippets/NTT.cpp" @@ -1,6 +1,17 @@ -// Inspired by Neal's amazing template -// [neal's submission](https://codeforces.com/contest/1096/submission/47700940) -// [my submission](https://codeforces.com/contest/1096/submission/119566645) +/* Iterative NTT */ + +/* + Inspired by Neal's amazing template + [neal's submission](https://codeforces.com/contest/1096/submission/47700940) + [my submission](https://codeforces.com/contest/1096/submission/119566645) + + uses modint template -> mint + + NTT problems: + https://codeforces.com/contest/1096/problem/G + https://codeforces.com/contest/632/problem/E +*/ + namespace NTT { const int FFT_CUTOFF = 150; @@ -123,9 +134,4 @@ namespace NTT { return fa; } -} // namespace NTT - -/* -https://codeforces.com/contest/1096/problem/G -https://codeforces.com/contest/632/problem/E -*/ \ No newline at end of file +} // namespace NTT \ No newline at end of file