正文
增加 270 行删除 245 行
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575873 行未改动1321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751769 行未改动1861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133149 行未改动32432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645713 行未改动47147247347447547647747847917 行未改动49749849950050150250350450550650750827 行未改动53653753853954054154254321 行未改动565566567568569570571572573644 行未改动全部展开1218121912201221## 0. 火车头```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);// 无权:g[x].push_back(y)// 带权:wg[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;}void add_edge(int u, int v, int w) {to[++ct] = v;wt[ct] = w;nx[ct] = hd[u];hd[u] = ct;}void add_unedge(int u, int v, int w) {add_edge(u, v, w);add_edge(v, u, w);}```### 1.3 LCT73 行未改动---## 2. 图论算法### 2.1 DFS```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```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;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});}}}}```### 2.5 并查集 DSU```cppint pa[M], rk[M];void init_dsu(int n) {for (int i = 1; i <= n; i++) {pa[i] = i;rk[i] = 0;}}int find(int x) {return pa[x] == x ? x : pa[x] = find(pa[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;if (rk[rx] == rk[ry]) rk[rx]++;return true;}```### 2.6 Kruskal 最小生成树```cppstruct E {int u, v, w;bool operator<(const E& o) const {return w < o.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;}}return -1;}```### 2.7 SPFA 判负环```cppbool iq[M];int cs[M];// true 无负环,false 有负环bool spfa(int s) {fill(ds, ds + n + 1, I);fill(iq, iq + n + 1, false);fill(cs, cs + n + 1, 0);queue<int> q;ds[s] = 0;q.push(s);iq[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]) {q.push(v);iq[v] = true;}}}}return true;}```### 2.8 Floyd 全源最短路```cppconst int FN = 505;int d[FN][FN];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 全源最短路```cppvector<vector<int>> johnson() {vector<int> h(n + 1, 0), ct(n + 1, 0);vector<bool> iq(n + 1, false);queue<int> q;for (int i = 1; i <= n; i++) {q.push(i);iq[i] = true;}while (!q.empty()) {int u = q.front();q.pop();iq[u] = false;for (auto [v, w] : wg[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]) {q.push(v);iq[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]});vector<vector<int>> rs(n + 1, vector<int>(n + 1, I));swap(wg, 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];}swap(wg, ng);return rs;}```### 2.10 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();}};```### 2.11 网络最大流```cppconst int NV = 250;ll graph[NV][NV], pre[NV], flow[NV];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];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 行未改动```---## 4. 区间查询### 4.1 ST 表 RMQ21 行未改动```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;}};```1234567891011121314151617181920212223242526272873 行未改动1021031041051061071081091101111129 行未改动1221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412429 行未改动25225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330413 行未改动31831932032132232332432517 行未改动34334434534634734834935035135235327 行未改动38138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149221 行未改动514515516517518519520521644 行未改动全部展开116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246## 1. 数据结构### 1.1 邻接表存图```cppint n;vector<vector<int>> g(M + 1);vector<vector<pair<int, int>>> g(M + 1);// 无权:g[x].push_back(y)// 带权:g[x].push_back({y, w})```### 1.2 链式前向星```cppint 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 addedge(int u, int v) {edge[cnt].to = v; edge[cnt].next = head[u]; head[u] = cnt++;}```### 1.3 LCT73 行未改动---## 2. 图论算法### 2.1 拓扑排序```cppvector<int> id(M + 1, 0);void topo() {queue<int> q;for (int i = 1; i <= n; i++) {9 行未改动}}```### 2.2 堆优化 Dijkstra```cppconst 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 (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.3 并查集 DSU```cppint fa[MAXN], rk[MAXN];void init(int n) {for (int i = 1; i <= n; i++) {fa[i] = i;rk[i] = 0;}}int find(int 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);fa[ry] = rx;if (rk[rx] == rk[ry]) rk[rx]++;return true;}```### 2.4 Kruskal 最小生成树```cppstruct Edge {int u, v, w;bool operator<(const Edge& other) const {return w < other.w;}};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;}```### 2.5 SPFA 判负环```cppbool inq[N];int dis[N], cnt[N];bool spfa(int s) {fill(dis, dis + n + 1, INF);fill(inq, inq + n + 1, false);fill(cnt, cnt + n + 1, 0);queue<int> q;dis[s] = 0;q.push(s);inq[s] = true;while (!q.empty()) {int u = q.front();q.pop();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);inq[v] = true;}}}}return true;}```### 2.6 Floyd 全源最短路```cppconst 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.7 Johnson 全源最短路```cppvector<vector<int>> johnson() {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);inq[i] = true;}while (!q.empty()) {int u = q.front();q.pop();inq[u] = false;for (auto [v, w] : g[u]) {if (h[v] > h[u] + w) {dis[v] = dis[u] + w;cnt[v] = cnt[u] + 1;if (cnt[v] >= n) return {};if (!inq[v]) {q.push(v);inq[v] = true;}}}}vector<vector<pair<int, int>>> ng(n + 1);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(g, ng);for (int s = 1; s <= n; s++) {dijkstra(s);for (int i = 1; i <= n; i++) if (ds[i] < INF) rs[s][i] = ds[i] - h[s] + h[i];}swap(g, ng);return rs;}```### 2.8 网络最大流```cppconst 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> 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 表 RMQ21 行未改动```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();}};```