版本比较
模板
正文
增加 270 行删除 245 行
1123456789101112131415161718192021345672789291011121314153536373839404142431617181920214546474849222324515253542526272873 行未改动1021031041051361061071081391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721091101111129 行未改动1221231241251901261271281931941951961971981992002012022032041291301311321331341351361371382072082092102112122132142151391401411421431441451461471481491501512221521531542252262271551561572291581591601611622341631641651661671682401691701711721731742461751761772491781792512521801811821832552562572582592602612622631841851861871881891901911921931941951962661971981992002702012022032732742752762042052062782792802072082092102822112122842132142152162882892902912922932942172182192202212222232242962252262272282292302312322333052342352363083093102372382392402412429 行未改动25225325425532825625725825933233326026126226326433726526626726826934234327027127234534634734827327427527627735027827928028128235528335735835936028428536228628728836536628929036829129229329429537329629729837637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145229930045330130230330413 行未改动31831932032147532232332432517 行未改动34334434534650150250350434734834935035135235327 行未改动38138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149221 行未改动514515516517569518519520521644 行未改动全部展开116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246## 0. 火车头## 1. 数据结构
```cpp#include <bits/stdc++.h>using namespace std;
typedef long long ll;typedef unsigned long long ull;typedef pair<int, int> pii;typedef pair<ll, ll> pll;
const int M = 1000005;const int I = 0x3f3f3f3f;const ll F = 0x3f3f3f3f3f3f3f3fLL;const int G = 20;```
---
## 1. 存储结构
### 1.1 邻接表存图
```cppint n;vector<vector<int>> g(M + 1);vector<vector<pair<int, int>>> wg(M + 1);vector<vector<pair<int, int>>> g(M + 1);// 无权:g[x].push_back(y)// 带权:wg[x].push_back({y, w})// 带权:g[x].push_back({y, w})```
### 1.2 链式前向星
```cppint hd[M];int to[2 * M];int wt[2 * M];int nx[2 * M];int ct = 0;
void init_cfs(int n) { for (int i = 1; i <= n; i++) hd[i] = -1; ct = 0;int head[N], cnt = 0; struct Edge {int to, next}edge[N];void init() { for (int i=0; i < N << 1; i++) edge[i].next = -1, head[i] = -1; cnt = 0;}void add_edge(int u, int v, int w) { to[++ct] = v; wt[ct] = w; nx[ct] = hd[u]; hd[u] = ct;void addedge(int u, int v) { edge[cnt].to = v; edge[cnt].next = head[u]; head[u] = cnt++;}void add_unedge(int u, int v, int w) { add_edge(u, v, w); add_edge(v, u, w);}```
### 1.3 LCT
73 行未改动---
## 2. 图论算法
### 2.1 DFS### 2.1 拓扑排序
```cppbool vs[M];void dfs(int u) { if (vs[u]) return; vs[u] = true; for (int v : g[u]) { if (!vs[v]) dfs(v); }}```
### 2.2 BFS
```cppbool vs[M];void bfs(int s) { queue<int> q; q.push(s); vs[s] = true; while (!q.empty()) { int u = q.front(); q.pop(); for (int v : g[u]) { if (!vs[v]) { vs[v] = true; q.push(v); } } }}```
### 2.3 拓扑排序
```cppvector<int> id(M + 1, 0);void topo() { queue<int> q; for (int i = 1; i <= n; i++) {9 行未改动 }}```
### 2.4 堆优化 Dijkstra### 2.2 堆优化 Dijkstra
```cppint ds[M];bool vs[M];
void dijkstra(int s) { fill(ds, ds + n + 1, I); fill(vs, vs + n + 1, false); priority_queue< pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>> > pq; ds[s] = 0;const int INF = 0x3f3f3f3f;int dist[MAXN];bool vis[MAXN]; void dijkstra(int s, int n) { memset(dist, 0x3f, sizeof(int) * (n + 1)); memset(vis, 0, sizeof(bool) * (n + 1)); priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; dist[s] = 0; pq.push({0, s}); while (!pq.empty()) { auto [d, u] = pq.top(); pq.pop(); if (d != ds[u]) continue; if (vs[u]) continue; vs[u] = true; for (auto [v, w] : wg[u]) { if (!vs[v] && ds[v] > ds[u] + w) { ds[v] = ds[u] + w; pq.push({ds[v], v}); auto [d, u] = pq.top(); pq.pop(); if (vis[u]) continue; vis[u] = true; for (auto [v, w] : g[u]) { if (!vis[v] && dist[v] > dist[u] + w) { dist[v] = dist[u] + w; pq.push({dist[v], v}); } } }}```
### 2.5 并查集 DSU### 2.3 并查集 DSU
```cppint pa[M], rk[M];
void init_dsu(int n) {int fa[MAXN], rk[MAXN];void init(int n) { for (int i = 1; i <= n; i++) { pa[i] = i; fa[i] = i; rk[i] = 0; }}int find(int x) { return pa[x] == x ? x : pa[x] = find(pa[x]); return fa[x] == x ? x : fa[x] = find(fa[x]);}bool unite(int x, int y) { int rx = find(x), ry = find(y); if (rx == ry) return false; if (rk[rx] < rk[ry]) swap(rx, ry); pa[ry] = rx; fa[ry] = rx; if (rk[rx] == rk[ry]) rk[rx]++; return true;}```
### 2.6 Kruskal 最小生成树### 2.4 Kruskal 最小生成树
```cppstruct E {struct Edge { int u, v, w; bool operator<(const E& o) const { return w < o.w; bool operator<(const Edge& other) const { return w < other.w; }};ll kruskal(vector<E>& es) { sort(es.begin(), es.end()); init_dsu(n); ll mw = 0; int ec = 0; for (const E& e : es) { if (unite(e.u, e.v)) { mw += e.w; if (++ec == n - 1) return mw;vector<Edge> edges;long long kruskal(int n) { sort(edges.begin(), edges.end()); init(n); long long mst = 0; int cnt = 0; for (const Edge& e : edges) { if (unite(e.u, e.v)) { mst += e.w; cnt++; if (cnt == n - 1) return mst_weight; } } return -1; return -1; }```
### 2.7 SPFA 判负环### 2.5 SPFA 判负环
```cppbool iq[M];int cs[M];
// true 无负环,false 有负环bool inq[N];int dis[N], cnt[N];bool spfa(int s) { fill(ds, ds + n + 1, I); fill(iq, iq + n + 1, false); fill(cs, cs + n + 1, 0); fill(dis, dis + n + 1, INF); fill(inq, inq + n + 1, false); fill(cnt, cnt + n + 1, 0); queue<int> q; ds[s] = 0; dis[s] = 0; q.push(s); iq[s] = true; inq[s] = true; while (!q.empty()) { int u = q.front(); q.pop(); iq[u] = false; for (auto [v, w] : wg[u]) { if (ds[v] > ds[u] + w) { ds[v] = ds[u] + w; cs[v] = cs[u] + 1; if (cs[v] >= n) return false; if (!iq[v]) { inq[u] = false; for (auto [v, w] : g[u]) { if (dis[v] > dis[u] + w) { dis[v] = dis[u] + w; cnt[v] = cnt[u] + 1; if (cnt[v] >= n) return false; if (!inq[v]) { q.push(v); iq[v] = true; inq[v] = true; } } } } return true;}```
### 2.8 Floyd 全源最短路### 2.6 Floyd 全源最短路
```cppconst int FN = 505;int d[FN][FN];
const int N = 505;int d[N][N];void init_floyd(int n) { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) d[i][j] = (i == j) ? 0 : I;9 行未改动 }}```
### 2.9 Johnson 全源最短路### 2.7 Johnson 全源最短路
```cppvector<vector<int>> johnson() { vector<int> h(n + 1, 0), ct(n + 1, 0); vector<bool> iq(n + 1, false); vector<int> dis(n + 1, 0), cnt(n + 1, 0); vector<bool> inq(n + 1, false); queue<int> q; for (int i = 1; i <= n; i++) { q.push(i); iq[i] = true; inq[i] = true; } while (!q.empty()) { int u = q.front(); q.pop(); iq[u] = false; for (auto [v, w] : wg[u]) { inq[u] = false; for (auto [v, w] : g[u]) { if (h[v] > h[u] + w) { h[v] = h[u] + w; ct[v] = ct[u] + 1; if (ct[v] >= n) return {}; if (!iq[v]) { dis[v] = dis[u] + w; cnt[v] = cnt[u] + 1; if (cnt[v] >= n) return {}; if (!inq[v]) { q.push(v); iq[v] = true; inq[v] = true; } } } }
vector<vector<pair<int, int>>> ng(n + 1); for (int u = 1; u <= n; u++) for (auto [v, w] : wg[u]) ng[u].push_back({v, w + h[u] - h[v]});
for (int u = 1; u <= n; u++) for (auto [v, w] : g[u]) ng[u].push_back({v, w + h[u] - h[v]}); vector<vector<int>> rs(n + 1, vector<int>(n + 1, I)); swap(wg, ng); swap(g, ng); for (int s = 1; s <= n; s++) { dijkstra(s); for (int i = 1; i <= n; i++) if (ds[i] < I) rs[s][i] = ds[i] - h[s] + h[i]; for (int i = 1; i <= n; i++) if (ds[i] < INF) rs[s][i] = ds[i] - h[s] + h[i]; } swap(wg, ng); swap(g, ng); return rs;}```
### 2.10 Trie### 2.8 网络最大流
```cppstruct Trie { static const int ALPHA = 26; vector<array<int, ALPHA>> nxt; vector<int> pc, ec;
Trie(int rn = 0) { init(rn); }
void init(int rn = 0) { nxt.clear(); pc.clear(); ec.clear(); nxt.reserve(rn + 1); pc.reserve(rn + 1); ec.reserve(rn + 1); nxt.push_back({}); pc.push_back(0); ec.push_back(0); } int new_node() { nxt.push_back({}); pc.push_back(0); ec.push_back(0); return (int)nxt.size() - 1; } static int id(char ch) { return ch - 'a'; } void insert(const string& s) { int u = 0; pc[u]++; for (char ch : s) { int c = id(ch); if (!nxt[u][c]) nxt[u][c] = new_node(); u = nxt[u][c]; pc[u]++; } ec[u]++; } int count(const string& s) const { int u = 0; for (char ch : s) { int c = id(ch); if (!nxt[u][c]) return 0; u = nxt[u][c]; } return ec[u]; } int count_prefix(const string& s) const { int u = 0; for (char ch : s) { int c = id(ch); if (!nxt[u][c]) return 0; u = nxt[u][c]; } return pc[u]; } void erase(const string& s) { int u = 0; pc[u]--; for (char ch : s) { int c = id(ch); u = nxt[u][c]; pc[u]--; } ec[u]--; } int size() const { return (int)nxt.size(); }};```
### 2.11 网络最大流
```cppconst int NV = 250;ll graph[NV][NV], pre[NV], flow[NV];const int N = 250;ll graph[N][N], pre[N], flow[N];
ll bfs_flow(int s, int t) { memset(pre, -1, sizeof(pre)); flow[s] = F; pre[s] = 0;13 行未改动 } if (pre[t] == -1) return -1; return flow[t];}
ll maxflow(int s, int t) { ll mf = 0; while (1) { ll f = bfs_flow(s, t);17 行未改动
### 3.1 倍增法 LCA
```cppvector<int> aj[M];int dep[M];int up[M][G];
vector<int> g[N];int dep[N];int up[N][20];int lca(int a, int b) { if (dep[a] < dep[b]) swap(a, b); int df = dep[a] - dep[b]; for (int j = 0; j < G; ++j)27 行未改动```
---
### 3.2 树链剖分
```cppstruct Edge {int to, next;}edge[N << 1];int head[N << 1], cnt, w[N], w_new[N], tree[N << 2], tag[N << 2], n, m, r, mod, son[N], id[N], fa[N], deep[N], siz[N], top[N], num;void init() { for (int i=0; i < N << 1; i++) edge[i].next = -1, head[i] = -1; cnt = 0;}void addedge(int u, int v) { edge[cnt].to = v; edge[cnt].next = head[u]; head[u] = cnt++;}int ls(int x) {return x << 1;}int rs(int x) {return x << 1 | 1;}void addtag(int p, int pl, int pr ,int d) { tag[p] += d; tree[p] += (d * (pr - pl + 1)) % mod;}void pushup(int p) { tree[p] = 1LL * tree[ls(p)] + tree[rs(p)] % mod;}void pushdown(int p, int pl, int pr) { if (tag[p]) { int mid = (pl + pr) >> 1; addtag(ls(p), pl, mid, tag[p]); addtag(rs(p), mid+1, pr, tag[p]); tag[p] = 0; }}void build(int p, int pl, int pr) { tag[p] = 0; if (pl == pr) {tree[p] = w_new[pl] % mod; return;} int mid = (pl + pr) >> 1; build(ls(p), pl, mid); build(rs(p), mid+1, pr); pushup(p);}void update(int L, int R, int p, int pl, int pr, int d) { if (L <= pl && pr <= R) {addtag(p, pl, pr, d); return;} pushdown(p, pl, pr); int mid = (pl + pr) >> 1; if (L <= mid) update(L, R, ls(p), pl, mid, d); if (R > mid) update(L, R, rs(p), mid+1, pr, d); pushup(p);}int query(int L, int R, int p, int pl, int pr) { if (pl >= L && R >= pr) return tree[p] % mod; pushdown(p, pl, pr); int res = 0, mid = (pl + pr) >> 1; if (L <= mid) res += query(L, R, ls(p), pl, mid) % mod; if (R > mid) res += query(L, R, rs(p), mid+1, pr) % mod; return res % mod;}void dfs1(int x, int fa) { deep[x] = deep[fa] + 1; ::fa[x] = fa; siz[x] = 1; for (int i = head[x]; ~i; i = edge[i].next) { int y = edge[i].to; if (y != fa) { ::fa[y] = x; dfs1(y, x); siz[x] += siz[y]; if (!son[x] || siz[son[x]] < siz[y]) son[x] = y; } }}void dfs2(int x, int topx) { id[x] = ++num; w_new[num] = w[x]; top[x] = topx; if (!son[x]) return; dfs2(son[x], topx); for (int i = head[x]; ~i; i = edge[i].next) { int y = edge[i].to; if (y != fa[x] && y != son[x]) dfs2(y, y); }}void update_range(int x, int y, int z) { while (top[x] != top[y]) { if (deep[top[x]] < deep[top[y]]) swap(x, y); update(id[top[x]], id[x], 1, 1, n, z); x = fa[top[x]]; } if (deep[x] > deep[y]) swap(x, y); update(id[x], id[y], 1, 1, n, z);}int query_range(int x, int y) { int ans = 0; while (top[x] != top[y]) { if (deep[top[x]] < deep[top[y]]) swap(x, y); ans += query(id[top[x]], id[x], 1, 1, n); ans %= mod; x = fa[top[x]]; } if (deep[x] > deep[y]) swap(x, y); ans += query(id[x], id[y], 1, 1, n); return ans % mod;}void update_tree(int x, int k) {update(id[x], id[x] + siz[x] - 1, 1, 1, n, k);}int query_tree(int x) {return query(id[x], id[x] + siz[x] - 1, 1, 1, n);}```
----
## 4. 区间查询
### 4.1 ST 表 RMQ
21 行未改动```cppint a[M];int tr[4 * M];int lz[4 * M];
void build(int nd, int l, int r) { if (l == r) { tr[nd] = a[l]; return;644 行未改动全部展开 return fabs(res) / 6.0; }};```
## 8. 字符串
### 8.1 Trie
```cppstruct Trie { static const int ALPHA = 26; vector<array<int, ALPHA>> nxt; vector<int> pc, ec;
Trie(int rn = 0) { init(rn); }
void init(int rn = 0) { nxt.clear(); pc.clear(); ec.clear(); nxt.reserve(rn + 1); pc.reserve(rn + 1); ec.reserve(rn + 1); nxt.push_back({}); pc.push_back(0); ec.push_back(0); } int new_node() { nxt.push_back({}); pc.push_back(0); ec.push_back(0); return (int)nxt.size() - 1; } static int id(char ch) { return ch - 'a'; } void insert(const string& s) { int u = 0; pc[u]++; for (char ch : s) { int c = id(ch); if (!nxt[u][c]) nxt[u][c] = new_node(); u = nxt[u][c]; pc[u]++; } ec[u]++; } int count(const string& s) const { int u = 0; for (char ch : s) { int c = id(ch); if (!nxt[u][c]) return 0; u = nxt[u][c]; } return ec[u]; } int count_prefix(const string& s) const { int u = 0; for (char ch : s) { int c = id(ch); if (!nxt[u][c]) return 0; u = nxt[u][c]; } return pc[u]; } void erase(const string& s) { int u = 0; pc[u]--; for (char ch : s) { int c = id(ch); u = nxt[u][c]; pc[u]--; } ec[u]--; } int size() const { return (int)nxt.size(); }};```