voidtopsort(){ queue<int> q; for (int i = 1; i <= n; ++i) if (!deg[i]) q.push(i); while (!q.empty()) { int x = q.front(); q.pop(); a[++cnt] = x; for (int i = head[x]; i; i = nxt[i]) { int y = ver[i]; if (--deg[y] == 0) q.push(y); } } }
数学
素数
\(Eratosthenes\)筛法
1 2 3 4 5 6 7 8 9
//O(nloglogn) voidsieve(){ memset(v, 0, sizeof(v)); for (int i = 2; i < N; ++i) { if (v[i]) continue; p[++cnt] = i; for (int j = i; j < N / i; ++j) v[i*j] = 1; } }
线性筛法
1 2 3 4 5 6 7 8 9 10 11
//O(n) voidsieve(){ memset(v, 0, sizeof(v)); for (int i = 2; i < N; ++i) { if (!v[i]) p[++cnt] = i; for (int j = 1; j <= cnt && p[j] * i < N; ++j) { v[i*p[j]] = 1; if (i % p[j] == 0) break; } } }
欧几里得
1 2 3 4 5 6
intgcd(int a, int b){ return !b ? a : gcd(b, a % b); } intlcm(int a, int b){ return a / gcd(a, b) * b; }
//单个Euler函数值 intphi(int n){ int res = n; for (int i = 2; i * i <= n; ++i) { if (n % i == 0) { res -= res / i; while (n % i == 0) n /= i; } } if (n > 1) res -= res / n; return res; } //O(nlogn) voideuler(int n){ for (int i = 1; i <= n; ++i) phi[i] = i; for (int i = 2; i <= n; ++i) if (phi[i] == i) for (int j = i; j <= n; j += i) phi[j] = phi[j] / i * (i - 1); }
数据结构
并查集
1 2 3 4 5 6 7 8 9 10 11 12 13
intfind(int x){ if (fa[x] == x) return x; int root = find(fa[x]); d[x] += d[fa[x]]; return fa[x] = root; }
voidunite(int x, int y){ int fx = find(x); int fy = find(y); if (fx == fy) return; fa[fx] = fy; }
intkrusal(){ sort(e + 1, e + m + 1); int cnt = 0, ans = 0; for (int i = 1; i <= n; ++i) f[i] = i; for (int i = 1; i <= m; ++i) { int x = find(e[i].u); int y = find(e[i].v); if (x != y) { ans += e[i].w; f[x] = y; cnt++; } if (cnt == n - 1) break; } if (cnt < n - 1) return-1; //不连通 return ans; }
\(Prim\)算法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
constint N = 5005; int n, m, ans; int a[N][N], d[N], v[N];
voidprim(){ memset(d, 0x3f, sizeof(d)); d[1] = 0; for (int i = 1; i <= n; ++i) { int x = 0; for (int j = 1; j <= n; ++j) if (!v[j] && (x == 0 || d[j] < d[x])) x = j; v[x] = 1; for (int y = 1; y <= n; ++y) if (!v[y]) d[y] = min(d[y], a[x][y]); } }
int n, m, s, t, maxflow, tot = 1; int ver[M], edge[M], Next[M], head[N], v[N], incf[N], pre[N];
voidadd(int x, int y, int z){ ver[++tot] = y, edge[tot] = z, Next[tot] = head[x], head[x] = tot; ver[++tot] = x, edge[tot] = 0, Next[tot] = head[y], head[y] = tot; }
boolbfs(){ memset(v, 0, sizeof(v)); queue<int> q; q.push(s); v[s] = 1; incf[s] = INF; while (!q.empty()) { int x = q.front(); q.pop(); for (int i = head[x]; i; i = Next[i]) if (edge[i]) { int y = ver[i]; if (v[y]) continue; incf[y] = min(incf[x], edge[i]); pre[y] = i; q.push(y); v[y] = 1; if (y == t) return1; } } return0; }
voidEK(){ while (bfs()) { int x = t; while (x != s) { int i = pre[x]; edge[i] -= incf[t]; edge[i^1] += incf[t]; x = ver[i^1]; } maxflow += incf[t]; } }
Dinic
1 2
附录
对拍
\(bat\)语言版本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
@echo off set cnt=0 for /l %%iin (1,1,10) do ( mine.exe < stdin%%i.txt > out%%i.txt fc out%%i.txt stdout%%i.txt > diff%%i.txt iferrorlevel1 ( set cnt=1 echo%%i:UnAccepted! ) ifnoterrorlevel1 ( del diff%%i.txt echo%%i:Accepted! ) ) if%cnt%==0color a && echo *** Totally Accepted! *** if%cnt%==1color c && echo *** Not All Accepted! *** pause
setnu set ruler set cul set tabstop=4 setshiftwidth=4 set expandtab set autoindent set nobackup set paste set incsearch set showcmd set showmatch set ignorecase set cin colo evening set mouse=a