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 / wlcs.cc
Last active November 5, 2018 19:16
Weighted LCS
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdio>
#include <stack>
#include <vector>
#include <cmath>
#include <unordered_map>
#include <utility>
@yinyanghu
yinyanghu / BIT.cc
Created February 11, 2017 22:24
Binary Indexed Tree (a.k.a. Fenwick tree)
#define MAXBIT 1000000
#define lowbit(x) (x & -x)
struct BIT {
int bit[MAXBIT];
int M;
void clear(int M) {
memset(bit, 0, sizeof(bit));
this->M = M;
@yinyanghu
yinyanghu / baiduyun.sh
Created January 9, 2017 16:52
Download from BaiduYun
#!/bin/bash
while :
do
bypy -v download todownload .
sleep 10
done
@yinyanghu
yinyanghu / PrimAlgorithm.py
Created January 26, 2016 02:29
Prim Algorithm with Minimum Heap
import heapq
# min-heap
class PriorityQueue:
def __init__(self):
self._queue = []
self._index = 0
def push(self, item, priority):
heapq.heappush(self._queue, (priority, self._index, item)) # -priority if max-heap
@yinyanghu
yinyanghu / MaxWeightPerfectBipartiteMatching.cc
Created December 16, 2015 01:40
Minimum / Maximum weight perfect matching for bipartite graph - Hungarian Algorithm - O(n^3)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int MAXN = 100;
const int INF = 100000000;
@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
@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 / 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 / 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 / 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;
}