Skip to content

Instantly share code, notes, and snippets.

View maxkibble's full-sized avatar

Qi Shen maxkibble

  • Peking University,Beijing
View GitHub Profile
@maxkibble
maxkibble / dinic.cpp
Created September 4, 2019 07:04
template for dinic algorithm, copy from tourist's submission (http://codeforces.com/contest/1198/submission/58006936)
template <typename T>
class flow_graph {
public:
static constexpr T eps = (T) 1e-9;
struct edge {
int from;
int to;
T c;
T f;
@maxkibble
maxkibble / diameter.cpp
Created July 11, 2018 11:29
求树的的直径
// 两遍DFS,直径上的点保存在trace中
const int maxn = 1e5 + 5;
typedef pair<int, int> PII;
int n, pre[maxn], dis[maxn];
vector<PII> g[maxn];
void dfs(int x, int fa) {
@maxkibble
maxkibble / undirected_tarjan.cpp
Created July 9, 2018 13:27
求无向图的边双连通分量(即在分量中删去任何一条边这个分量依然连通)
int clk, dfu[maxn], low[maxn], sz, belong[maxn];
bool inStack[maxn];
std::stack<int> st;
std::vector<std::vector<int> > comp;
void tarjan(int u, int fa) {
dfu[u] = low[u] = ++clk;
st.push(u);
inStack[u] = true;
for (int v: g[u]) {
@maxkibble
maxkibble / tarjan.cpp
Created July 9, 2018 08:16
tarjan algorithm for getting strongly connected components for directed graph
/*
Suppose graph is stored in a vector of vector...(g: std::vector<std::vector<int> >)
The graph has n nodes
The components are stored in variable: comp
belong[i] represents component ID for each node, start from 1
*/
int clk, dfu[maxn], low[maxn], sz, belong[maxn];
bool inStack[maxn];
std::stack<int> st;
@maxkibble
maxkibble / quick_sort.cpp
Last active July 1, 2018 08:11
c++ implementation for quick-sort algorithm
void quickSort(int *a, int l, int r) {
if (l >= r) return;
// this is a good coding style to avoid overflow
int mid = l + (r - l) / 2;
// choose a[mid] as pivot to speed up for sequences in order
int pivot = a[mid];
std::swap(a[mid], a[r]);
int i = l, j = r - 1;
while (i <= j) {
while (i <= j && a[i] <= pivot) i++;
@maxkibble
maxkibble / calculator.py
Created June 26, 2018 05:26
python implementation of calculating an infix expression
from operator import add, sub, mul, truediv
class Calculator(object):
op = {'+': add, '-': sub, '*': mul, '/': truediv}
def to_suffix(self, s):
st = []
ret = ''
tokens = s.split()
@maxkibble
maxkibble / binary_indexed_tree.cpp
Last active September 25, 2018 14:14
c++ implementation for date structure: binary indexed tree
struct BinaryIndexedTree {
int up;
long long tree[maxn];
void init(int u) {
up = u;
memset(tree, 0, sizeof(tree));
}
inline int lowbit(int x) { return (x & (-x)); }
const int maxn = 1e5 + 5;
struct Dsu {
struct Node {
int val, fa;
} t[maxn];
void init(int sz) {
for (int i = 0; i < n; i++)
t[i].val = 1, t[i].fa = i;
struct TrieNode {
int minVal;
TrieNode *child[2];
TrieNode() {
minVal = inf;
child[0] = child[1] = NULL;
}
};
struct Trie {
@maxkibble
maxkibble / topsort.cpp
Last active March 6, 2018 05:43
topological sort for directed graph
using std::vector;
using std::queue;
vector<int> topsort(const vector<vector<int> > &g) {
int n = g.size();
vector<int> d(n);
for(int i = 0; i < n; i++)
for(int j = 0; j < g[i].size(); j++)
d[g[i][j]]++;