Skip to content

Instantly share code, notes, and snippets.

View yinyanghu's full-sized avatar
🎯
Focusing

Jian Li yinyanghu

🎯
Focusing
View GitHub Profile
@yinyanghu
yinyanghu / dinic.cc
Last active April 9, 2017 19:20
Maximum Flow - Dinic Algorithm
typedef int F;
#define MAXE 50000
#define MAXV 20000
#define F_INF 1000000
struct MaxFlow {
int V, E;
int start[MAXV], next[MAXE], v[MAXE];
int used[MAXV], level[MAXV];
F cap[MAXE], flow[MAXE];
@yinyanghu
yinyanghu / bipartite_matching.cc
Last active April 9, 2017 18:14
Maximum bipartite matching - augmenting path algorithm
bool extendpath(int x) {
for (int i = 1; i <= m; ++ i)
if (edge[x][i] && flag[i]) {
flag[i] = false;
if (matching[i] == -1 || extendpath(matching[i])) {
matching[i] = x;
return true;
}
}
return false;
@yinyanghu
yinyanghu / gen.cc
Last active August 29, 2015 14:22
POJ 2104 - K-th Number http://poj.org/problem?id=2104
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
const int N = 100000;
const int M = 5000;
const int MOD = 100000000;
int A[N];
@yinyanghu
yinyanghu / st.cc
Last active August 29, 2015 14:23
RMQ - Sparse Table Algorithm
#define MAXST 100000
#define LogMAXST 18
inline int clz(int x){return __builtin_clz(x);}
inline int lg2(int x){return !x ? -1 : 31 - clz(x);}
struct SparseTable {
int N;
int F[LogMAXST][MAXST];
@yinyanghu
yinyanghu / lca.cc
Last active August 29, 2015 14:23
Lowest Common Ancestor (LCA) - LCA -> RMQ -> ST - O(nlogn) - O(1)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
#define MAXN 1000 + 1
#define MAXE 2*(MAXN)
@yinyanghu
yinyanghu / repeated_squaring.cc
Last active August 29, 2015 14:23
repeated squaring to compute x^y mod p
// return x^y mod p
int repeated_squaring(int x, int y, int p) {
if (y <= 0) return 1;
int ret = 1;
for (int base = x; y; base = (base * base) % p, y >>= 1)
if ((y & 1) == 1) ret = (ret * base) % p;
return ret;
}
@yinyanghu
yinyanghu / extended_euclidean.cc
Created June 23, 2015 16:18
Extended Euclidean Algorithm and find modular inverse
// return <gcd(a, b), <x, y>> such that ax + by = gcd(a, b)
pair< int, pair<int, int> > extended_euclidean(int a, int b) {
if (b == 0) return make_pair(a, make_pair(1, 0));
pair< int, pair<int, int> > next = extended_euclidean(b, a % b);
int y = next.second.first, x = next.second.second;
y = y - (a / b) * x;
return make_pair(next.first, make_pair(x, y));
}
// p is prime, return b such that a * b = 1 (mod p)
@yinyanghu
yinyanghu / union_find.cc
Last active August 29, 2015 14:23
Union Find data structure for Disjoint-Set, O(\alpha(n))
#define MAXU 100000
struct UnionFind {
int f[MAXU];
int rank[MAXU];
void clear() {
memset(f, -1, sizeof(f));
memset(rank, 0, sizeof(rank));
}
@yinyanghu
yinyanghu / mdst.cc
Created July 2, 2015 03:57
Minimum Diameter Spanning Tree (MDST) - SPOJ - http://www.spoj.com/problems/MDST/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAXN 1000+1
#define INF 100000000
int N;
@yinyanghu
yinyanghu / undirected.cc
Last active August 29, 2015 14:24
Undirected Graph - Find bridges, articulation vertices, and contract cycles in graph
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#define MAXN 1000+1
#define MAXE 2*10000