Skip to content

Commit

Permalink
Update Snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
the-hyp0cr1t3 committed Sep 9, 2021
1 parent 224df13 commit 58fc65d
Show file tree
Hide file tree
Showing 11 changed files with 197 additions and 142 deletions.
41 changes: 41 additions & 0 deletions 末 Snippets/Euler Circuit (directed).cpp
Original file line number Diff line number Diff line change
@@ -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<int> indeg(n), outdeg(n);
vector<vector<int>> 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<int> 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());
51 changes: 51 additions & 0 deletions 末 Snippets/Euler Circuit (undirected).cpp
Original file line number Diff line number Diff line change
@@ -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<int> deg(n);
vector<edge_t> edges;
edges.reserve(m);
vector<vector<edge_t*>> 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<int> 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());
48 changes: 0 additions & 48 deletions 末 Snippets/Euler Circuit.cpp

This file was deleted.

15 changes: 9 additions & 6 deletions 末 Snippets/Extended Euclidean algorithm.cpp
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -17,17 +20,17 @@ 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
// y = y0 - λ a/g
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;
Expand Down Expand Up @@ -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;
};
};
57 changes: 30 additions & 27 deletions 末 Snippets/FFT.cpp
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
*/
2 changes: 2 additions & 0 deletions 末 Snippets/Factorize.cpp → 末 Snippets/Factorization.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* Prime factorization */
/* [neal](https://codeforces.com/profile/neal) orz */

// Prime factorizes n in worst case O(sqrt n).
vector<pair<int64_t, int>> prime_factorize(int64_t n) {
assert(n >= 1);
Expand Down
55 changes: 18 additions & 37 deletions 末 Snippets/Floyd Warshall.cpp
Original file line number Diff line number Diff line change
@@ -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<int> 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<int>(n, INF)), path(n, vector<int>(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<int> ans{ u };
while(u ^ v) {
u = path[u][v];
ans.push_back(u);
}
25 changes: 14 additions & 11 deletions 末 Snippets/Gaussian elimination.cpp
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -65,13 +77,4 @@ int gauss(int n, int m, vector<bitset<N>>& 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
*/
}
16 changes: 14 additions & 2 deletions 末 Snippets/Hashing.cpp
Original file line number Diff line number Diff line change
@@ -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 <ext/pb_ds/assoc_container.hpp>
namespace Hashing {
using hash_t = pair<int, uint64_t>;
Expand Down Expand Up @@ -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)
*/
5 changes: 3 additions & 2 deletions 末 Snippets/KMP.cpp
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -11,7 +12,7 @@
// 0000 123012
auto generate_pi = [&] (const string& s) {
// s = (pattern + '#' + text)
int n = sz(s);
int n = s.size();
vector<int> pi(n);
for(i = 1; i < n; i++) {
int p = pi[i-1];
Expand Down
Loading

0 comments on commit 58fc65d

Please sign in to comment.