
模版
芋智波佐助
菜鸟一只
展开
-
字符串匹配模版
void getFail(){ int m = strlen(p); f[0] = f[1] = 0; for(int i = 1; i < m; i++) { int j = f[i]; while(j && p[i] != p[j]) j = f[j]; f[i+1] = p[i] == p[j] ? j+1 : 0; }}void find(){ int原创 2014-04-07 20:41:48 · 988 阅读 · 0 评论 -
后缀数组模版
char s[maxn];int sa[maxn];int t[maxn], t2[maxn], c[maxn];int rank[maxn], height[maxn];int n;void build_sa(int m){ int i, *x = t, *y = t2; for(i = 0; i < m; i++) c[i] = 0; for(i = 0; i < n;原创 2014-04-29 18:07:47 · 919 阅读 · 0 评论 -
字典树模版
数组版 来自大白书int ch[maxnode][sigma_size];int val[maxnode];int sz;void init(){ sz = 1; memset(ch[0], 0, sizeof(ch[0]));}int idx(char c){ return c - 'a';}void insert(char *s){ int u = 0, n原创 2014-04-09 13:11:16 · 994 阅读 · 0 评论 -
AC自动机模版
struct node{ int val; node *next[26]; node *fail; node() { val = 0; for(int i = 0; i < 26; i++) next[i] = NULL; fail = NULL; }};node *root;void insert(char *s){ int n = strlen(s)原创 2014-04-14 11:00:22 · 935 阅读 · 0 评论 -
RMQ模版
STint dp[maxn][20];int a[maxn];void RMQ_init(int n){ int i,j,k; for(i = 1; i <= n; i++) dp[i][0] = a[i]; k = (int) (log((double)n + 0.2) / log(2.0)); for(j = 1; j <= k; j++) for(i =原创 2014-05-24 19:25:06 · 879 阅读 · 0 评论 -
最近公共祖先模版
LCA tarjan 的离线算法#include #include #include using namespace std;const int maxn = 40010;int first[maxn], head[maxn], cnt, sum;struct edge{ int u, v, w, next;}e[maxn*2], qe[maxn], Q[maxn];int原创 2014-07-08 15:21:31 · 980 阅读 · 0 评论 -
二分匹配模版
const int maxn = 10010;int vis[maxn];int y[maxn];vector G[maxn];int n;bool dfs(int u){ for(int i = 0; i < G[u].size(); i++) { int v = G[u][i]; if(vis[v]) continue; vis[v] = true;原创 2014-06-12 19:47:26 · 945 阅读 · 0 评论 -
拓扑排序模版
bool Topo(){ int sum = 0; while(1) { queue Q; for(int i = 1; i <= n; i++) if(!in[i]) Q.push(i); sum += Q.size(); if(sum == n) return true; if(!Q.size()) return false; whi原创 2014-06-09 12:19:15 · 995 阅读 · 0 评论 -
2-SAT模版
const int maxn = 100010;int n, m;vector G[maxn*2];bool mark[maxn*2];int S[maxn*2], c;int a[maxn], b[maxn], sum;bool dfs(int x){ if(mark[x^1]) return false; if(mark[x]) return true; mark原创 2014-06-06 18:01:52 · 974 阅读 · 0 评论 -
割点 桥 双连通分量模版
求割点const int maxn = 1010;vector a[maxn], bcc[maxn];int pre[maxn];int low[maxn];bool iscut[maxn];int bccno[maxn];int cnt[maxn];int dfs_clock;int bcc_cnt;int n;struct Edge{ int u, v;};原创 2014-06-07 11:43:36 · 952 阅读 · 0 评论 -
最短路模版
SPFAbool SPFA(){ memset(inq, false, sizeof(inq)); for(int i = 0; i <= n; i++) d[i] = INF; d[0] = 0; queue Q; Q.push(0); inq[0] = true; while(!Q.empty()) { int u = Q.front(); Q.pop();原创 2014-04-20 13:21:05 · 1028 阅读 · 0 评论 -
强连通分量模版
Tarjanvector G[maxn];int pre[maxn];int low[maxn];int sccno[maxn];int dfs_clock;int scc_cnt;stack S;int n, m;int degree[maxn];int cnt[maxn];void dfs(int u){ pre[u] = low[u] = ++dfs_cloc原创 2014-04-19 11:04:29 · 920 阅读 · 0 评论 -
最小生成树模版
克鲁斯卡尔struct edge{ int u, v, w;}e[maxn];int f[110];bool cmp(edge a, edge b){ return a.w < b.w;}int find(int x){ if(x != f[x]) return f[x] = find(f[x]); return f[x];}int MST(){ int原创 2014-05-21 18:23:49 · 938 阅读 · 0 评论 -
网络流模版
递归版struct Edge{ int from, to, cap, flow; Edge(){} Edge(int from, int to, int cap, int flow) : from(from), to(to), cap(cap), flow(flow){}};int n, m, s, t;vector edges;vector G[maxn];bool v原创 2014-05-06 14:48:09 · 944 阅读 · 0 评论 -
字典树模版
静态版本 #include #include using namespace std;int ch[maxnode][sigma_size];int val[maxnode];int sz;void init(){ sz = 1; memset(ch[0], 0, sizeof(ch[0]));}int idx(char c){ retu原创 2016-07-07 18:35:35 · 254 阅读 · 0 评论 -
最近公共祖先模版
离线算法#include #include using namespace std;typedef long long LL;const int maxn = 12;int first[maxn], cnt;int anc[maxn][maxn], n;LL a[maxn];struct edge{ int u, v, next;}e[maxn原创 2016-07-07 18:35:31 · 331 阅读 · 0 评论 -
数论模版
//求gcd(a, b) LL gcd(LL a, LL b){ return b ? gcd(b, a%b) : a;} //求整数x和y,使得ax+by=d, 且|x|+|y|最小。其中d=gcd(a,b) void gcd(LL a, LL b, LL& d, LL& x, LL& y){ if(!b) { d = a; x = 1; y = 0原创 2014-05-10 16:57:00 · 1090 阅读 · 0 评论 -
矩阵模版
矩阵快速幂const int maxn = 3;struct Mat{ int a[maxn][maxn];};Mat A, B, C, D;int n, m;Mat get(Mat x, Mat y){ Mat z; memset(z.a, 0, sizeof(z.a)); for(int i = 1; i <= 2; i++)原创 2014-05-28 11:36:33 · 880 阅读 · 0 评论 -
高斯消元模版
矩阵的秩typedef int Matrix[maxn][maxn];int rank(Matrix A, int m, int n){ int i = 0, j = 0, k, r, u; while(i < m && j < n) { r = i; for(k = i; k < m; k++) if(A[k][j]) { r = k; brea原创 2014-06-17 13:30:54 · 921 阅读 · 0 评论 -
求原根模版
#include #include using namespace std;typedef long long LL;int p[100000], c;LL pow_mod(LL a, LL x, LL m){ LL ans = 1; while(x) { if(x&1) ans = ans * a % m; a = a * a % m; x >>= 1;原创 2014-09-20 21:37:18 · 2081 阅读 · 0 评论 -
欧拉回路模版
#include #include #include using namespace std;const int maxm = 40010;const int maxn = 1010;int first[maxn], cnt;struct edge{ int u, v, next;}e[maxn*maxn];int ans[maxm];bool vis[maxm];int原创 2014-09-10 22:11:07 · 1046 阅读 · 0 评论 -
二维线段树模版
HDU 4819 二维线段树模版题#include #include #include using namespace std;const int INF = 999999999;const int maxn = 810;int a[maxn][maxn];int st_min[maxn<<2][maxn<<2];int st_max[maxn<<2][maxn<<2];in原创 2014-09-22 19:55:47 · 1625 阅读 · 0 评论 -
splay树模版
#include #include using namespace std;typedef long long LL;const int maxn = 100010;int pre[maxn], ch[maxn][2], sz[maxn];int root, top1;int s[maxn], top2;//内存池 LL sum[maxn];int val[maxn], add原创 2014-09-03 14:38:04 · 956 阅读 · 0 评论 -
Miller_Rabin大素数测试与Pollard_rho整数分解模版
#include #include #include #include using namespace std;typedef __int64 LL;const int Times = 20;LL factor[100], l;LL gcd(LL a, LL b){ return b ? gcd(b, a%b):a;}LL add_mod(LL a, LL b, LL n)原创 2014-08-30 20:21:15 · 1132 阅读 · 0 评论 -
treap模版
#include #include #include using namespace std;struct Node{ Node *ch[2]; int r; int v; int s; Node(int v): v(v) { ch[0] = ch[1] = NULL; r = rand(); s = 1; } bool operator < (const Node&原创 2014-08-28 22:07:12 · 896 阅读 · 0 评论 -
树链剖分模版
#include #include #include using namespace std;const int maxn = 10010;struct edge{ int v, next;}e[maxn*2];int first[maxn], cnt;int top[maxn], dep[maxn], sz[maxn], f[maxn], son[maxn], rank[m原创 2015-02-02 13:34:48 · 801 阅读 · 0 评论 -
扩展baby step giant step模版
#include #include #include #include using namespace std;typedef __int64 LL;LL gcd(LL a, LL b){ return b ? gcd(b, a%b) : a;}LL pow_mod(LL a, LL p, LL n){ LL ans = 1; while(p) { if(p&1)原创 2015-03-16 16:42:27 · 750 阅读 · 0 评论 -
莫比乌斯模版
#include #include #include using namespace std;typedef long long LL;const int maxn = 100010;int mu[maxn], prime[maxn], vis[maxn];int cnt;int a, b, c, d, k;void mobi(int n){ mu[1] = 1; for(原创 2015-03-11 14:44:29 · 802 阅读 · 0 评论 -
主席树模版
参考wuyiqi#include #include #include using namespace std;const int maxn = 100010;int ls[maxn*20], rs[maxn*20], sum[maxn*20];int T[maxn], tot;void build(int l, int r, int& rt){ rt = ++tot; s原创 2015-03-24 16:33:56 · 1548 阅读 · 0 评论 -
划分树模版
#include #include #include using namespace std;const int maxn = 100010;int tree[20][maxn], toleft[20][maxn], a[maxn];void build(int l, int r, int dep){ if(l == r) return;原创 2016-07-11 09:35:53 · 348 阅读 · 0 评论 -
Splay树模版
#include #include #include #include using namespace std;typedef long long LL;const int maxn = 1000010;int pre[maxn], ch[maxn][2], sz[maxn];int root, top1; int val[maxn]; voi原创 2016-07-12 09:32:43 · 290 阅读 · 0 评论 -
网络流模板 Dinic+ISAP
dinic递归版,堆栈溢出可以手动扩栈,改成非递归版本,或者改用效率更高的ISAP。struct Edge{ int from, to, cap, flow; Edge(){} Edge(int from, int to, int cap, int flow) : from(from), to(to), cap(cap), flow(flow){}};int n, m,原创 2016-07-13 09:34:16 · 505 阅读 · 0 评论 -
主席树模版
主席树模版 #include #include #include using namespace std;const int maxn = 100010;int ls[maxn*20], rs[maxn*20], sum[maxn*20];int T[maxn], tot;void build(int l, int r, int& rt){ rt =原创 2016-07-08 09:55:26 · 399 阅读 · 0 评论