标题
增加 1 行删除 1 行
1模板(更新中)1关于 BSGS
正文
增加 8 行删除 1153 行
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511564 行未改动11611162116311641165116611671168116911701171117223 行未改动1196119711981199120012011202120312041205120612071208120912101211121222 行未改动1235123612371238## 树状数组### 单点修改 + 单点查询```cpp#include <iostream>using namespace std;int n, m, now, x, y, tree[500010], a[500010];int lowbit(int x){return x & (-x);}void add(int l, int a){for (int i = l; i <= n; i += lowbit(i))tree[i] += a;for (int i = l + 1; i <= n; i += lowbit(i))tree[i] -= a;}//差分,将第l个数加上avoid sum(int x){int s = 0;for (int i = x; i > 0; i -= lowbit(i))s += tree[i];//前缀和cout << s + a[x] << endl;}//查询数组第x个数并输出int main(){cin >> n >> m;for (int i = 1; i <= n; i ++)cin >> a[i];for (int i = 1; i <= m && cin >> now >> x; i ++){if (now == 1){cin >> y;add(x, y);}else sum(x);}return 0;}```### 单点修改 + 区间查询```cpp#include <iostream>using namespace std;int n, m, now, x, y, tree[500010];int lowbit(int x){return x & (-x);}void add(int l, int a){for (int i = l; i <= n; i += lowbit(i))tree[i] += a;}//将第l个数加上avoid sum(int l, int r){int s = 0;for (int i = r; i > 0; i -= lowbit(i))s += tree[i];for (int i = l - 1; i > 0; i -= lowbit(i))s -= tree[i];cout << s << endl;}//查询数组从l到r的区间和并输出int main(){cin >> n >> m;for (int i = 1; i <= n && cin >> x; i ++)add(i, x);for (int i = 1; i <= m && cin >> now >> x >> y; i ++){if (now == 1)add(x, y);else sum(x, y);}return 0;}```### 区间修改 + 单点查询```cpp#include <iostream>using namespace std;int n, m, now, x, y, z, a[500010], c[500010];int lowbit(int x){return x & -x;}void add(int l, int r, int sum){for (int i = l; i <= n; i += lowbit(i))c[i] += sum;for (int i = r + 1; i <= n; i += lowbit(i))c[i] -= sum;//差分}//差分数组,从l到r全部加上sumvoid sum(int x){int s = 0;for (int i = x; i > 0; i -= lowbit(i))s += c[i];//前缀和统计cout << a[x] + s << endl;}//查询第x个数的值并输出int main(){cin >> n >> m;for (int i = 1; i <= n; i ++)cin >> a[i];for (int i = 1; i <= m && cin >> now >> x; i ++){if (now == 1){cin >> y >> z;add(x, y, z);}else sum(x);}return 0;}```### 区间修改 + 区间查询```cpp#include <iostream>using namespace std;long long n, m, now, x, y, z, a1, a2, b[100010], c[100010];long long lowbit(long long x){return x & (-x);}void add(long long l, long long r, long long sum){for (int i = l; i <= n; i += lowbit(i))c[i] += sum;for (int i = r + 1; i <= n; i += lowbit(i))c[i] -= sum;for (int i = l; i <= n; i += lowbit(i))b[i] += sum * (l - 1);for (int i = r + 1; i <= n; i += lowbit(i))b[i] -= sum * r;}//差分,从l到r每个数加上sum,b[i] = c[i] * (i - 1)void sum(long long l, long long r){long long sr = 0, sl = 0;for (int i = r; i > 0; i -= lowbit(i))sr += c[i];for (int i = l - 1; i > 0; i -= lowbit(i))sl += c[i];sr = sr * r - sl * (l - 1);for (int i = r; i > 0; i -= lowbit(i))sr -= b[i];for (int i = l - 1; i > 0; i -= lowbit(i))sr += b[i];cout << sr << endl;}//前缀和相加,从l到r的和//从l到r的和为c数组从1到r的和减从1到l - 1的和//化简为c数组从1到r的和乘以r减去b数组从1到r的和int main(){cin >> n >> m;for (int i = 1; i <= n && cin >> a1; i ++)add(i, n, a1 - a2), a2 = a1;//求差分for (int i = 1; i <= m && cin >> now >> x >> y; i ++){if (now == 1){cin >> z;add(x, y, z);}else sum(x, y);}return 0;}```## 线段树### 单点修改 + 单点查询```cpp#include <iostream>using namespace std;long long n, w, a, b, c, tree[400010];void add(long long l, long long r, long long now, long long h, long long sum){if (l == r){tree[now] += sum;return;}long long mid = l + r >> 1;if (h <= mid)add(l, mid, now << 1, h, sum);else add(mid + 1, r, now << 1 | 1, h, sum);tree[now] = tree[now << 1] + tree[now << 1 | 1];}//将第h点加上sumlong long sum_tree(long long l, long long r, long long now, long long x){if (l == r)return tree[now];long long mid = l + r >> 1;if (x <= mid)return sum_tree(l, mid, now << 1, x);else return sum_tree(mid + 1, r, now << 1 | 1, x);}//在数组中查询点xint main(){cin >> n >> w;for (int i = 1; i <= n && cin >> a; i ++)add(1, n, 1, i, a);//建树for (int i = 1; i <= w && cin >> a >> b; i ++){if (a == 1){cin >> c;add(1, n, 1, b, c);//单点修改}else cout << sum_tree(1, n, 1, b) << endl;//单点查询}return 0;}```### 单点修改 + 区间查询```cpp#include <iostream>using namespace std;long long n, w, a, b, c, tree[400010];void add(long long l, long long r, long long now, long long h, long long sum){if (l == r){tree[now] += sum;return;}long long mid = l + r >> 1;if (h <= mid)add(l, mid, now << 1, h, sum);else add(mid + 1, r, now << 1 | 1, h, sum);tree[now] = tree[now << 1] + tree[now << 1 | 1];}//将第h点加上sumlong long sum_tree(long long l, long long r, long long now, long long x, long long y){if (x <= l && r <= y)return tree[now];long long mid = l + r >> 1, ans = 0;if (x <= mid)ans += sum_tree(l, mid, now << 1, x, y);if (mid < y)ans += sum_tree(mid + 1, r, now << 1 | 1, x, y);return ans;}//在区间[x, y]中求和int main(){cin >> n >> w;for (int i = 1; i <= n && cin >> a; i ++) add(1, n, 1, i, a);//建树for (int i = 1; i <= w && cin >> a >> b >> c; i ++){if (a == 1)add(1, n, 1, b, c);//单点修改else cout << sum_tree(1, n, 1, b, c) << endl;//区间查询}return 0;}```### 区间修改 + 区间查询(乘 + 加)```cpp#include <bits/stdc++.h>using namespace std;int n, q, mod, a[100010], tree[400010], mul[400010], add[400010], opt, x, y, z;void down(int now, int l, int r){//懒标记下传int mid = l + r >> 1;mul[now << 1] = mul[now << 1] * 1LL * mul[now] % mod, mul[now << 1 | 1] = mul[now << 1 | 1] * 1LL * mul[now] % mod;add[now << 1] = add[now << 1] * 1LL * mul[now] % mod, add[now << 1 | 1] = add[now << 1 | 1] * 1LL * mul[now] % mod;tree[now << 1] = tree[now << 1] * 1LL * mul[now] % mod, tree[now << 1 | 1] = tree[now << 1 | 1] * 1LL * mul[now] % mod;(add[now << 1] += add[now]) %= mod, (add[now << 1 | 1] += add[now]) %= mod;(tree[now << 1] += add[now] * 1LL * (mid - l + 1) % mod) %= mod, (tree[now << 1 | 1] += add[now] * 1LL * (r - mid) % mod) %= mod;add[now] = 0, mul[now] = 1;}void init(int now, int l, int r){//初始化mul[now] = 1;//乘法初始化为 1if (l == r){tree[now] = a[l];return;}int mid = l + r >> 1;init(now << 1, l, mid), init(now << 1 | 1, mid + 1, r);tree[now] = (tree[now << 1] + tree[now << 1 | 1]) % mod;}void multion(int now, int l, int r){//乘法if (x <= l && r <= y){mul[now] = (1LL * mul[now] * z) % mod, add[now] = (1LL * add[now] * z) % mod, tree[now] = (1LL * tree[now] * z) % mod;return;}int mid = l + r >> 1;down(now, l, r);if (x <= mid)multion(now << 1, l, mid);if (y > mid)multion(now << 1 | 1, mid + 1, r);tree[now] = (tree[now << 1] + tree[now << 1 | 1]) % mod;}void plusion(int now, int l, int r){//加法if (x <= l && r <= y){(add[now] += z) %= mod, (tree[now] += z * 1LL * (r - l + 1)) % mod;return;}int mid = l + r >> 1;down(now, l, r);if (x <= mid)plusion(now << 1, l, mid);if (y > mid)plusion(now << 1 | 1, mid + 1, r);tree[now] = (tree[now << 1] + tree[now << 1 | 1]) % mod;}int query(int now, int l, int r){//查询if (x <= l && r <= y)return tree[now];int ans = 0, mid = l + r >> 1;down(now, l, r);if (x <= mid)(ans += query(now << 1, l, mid)) %= mod;if (y > mid)(ans += query(now << 1 | 1, mid + 1, r)) %= mod;return ans;}int main(){ios::sync_with_stdio(0);cin.tie(0);cin >> n >> q >> mod;for (int i = 1; i <= n; i ++)cin >> a[i];init(1, 1, n);while (q -- && cin >> opt >> x >> y){if (opt == 1){cin >> z;multion(1, 1, n);}else if (opt == 2){cin >> z;plusion(1, 1, n);}else cout << query(1, 1, n) << '\n';}return 0;}```## 快读### 基本版(int)```cppvoid read(int &x){char ch = getchar_unlocked();bool f = 0;x = 0;while (ch < '0' || ch > '9')f |= ch == '-', ch = getchar_unlocked();while (ch >= '0' && ch <= '9')x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar_unlocked();if (f)x = -x;}```### 基本版(double)```cppvoid read(double &x){char ch = getchar();double now = 0.1;bool f = 0;x = 0;while (ch < '0' || ch > '9')f |= ch == '-', ch = getchar();while (ch >= '0' && ch <= '9')x = x * 10 + (ch ^ 48), ch = getchar();if (ch != '.'){x = (f ? -x : x);return;}ch = getchar();while (ch >= '0' && ch <= '9')x += now * (ch ^ 48), now *= 0.1, ch = getchar();x = (f ? -x : x);}```### fread 优化版(int)```cpp//使用方法:IO::cin,IO::cout//代码末尾加 IO::cout.Flush()namespace IO{const int BUFSIZE = 1 << 20;char ibuf[BUFSIZE], *is = ibuf, *it = ibuf, tmp[BUFSIZE];int cnt = 0;inline char getch(){if (is == it)it = (is = ibuf) + fread(ibuf, 1, BUFSIZE, stdin);return is == it ? EOF : *is ++;}int readInt(){int x = 0, y = 0;char c = 0;while (!isdigit(c))y |= c == '-', c = getch();while (isdigit(c))x = (x << 3) + (x << 1) + (c ^ 48), c = getch();return !y ? x : -x;}void flush(){fwrite(tmp, 1, cnt, stdout);cnt = 0;}void putch(char c){tmp[cnt ++] = c;if (cnt == BUFSIZE)flush();}void putint(int x){char Q[10];int tp = 0;if (x < 0)putch('-'), x = -x;if (!x)putch('0');while (x)Q[tp ++] = (x % 10 + 48), x /= 10;while (tp --)putch(Q[tp]);}struct Basic{Basic & operator >> (int &A){A = IO::readInt();return (*this);}Basic & operator << (const int &A){putint(A);return (*this);}void Flush(){flush();}}cin, cout;}```## 单源最短路### Dijkstra(堆优化)```cpp#include <bits/stdc++.h>using namespace std;int n, m, s, x, y, z, ans[100010];bool vis[100010];pair<int, int> now;vector<pair<int, int> > v[100010];priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q;int main(){cin >> n >> m >> s;//由s至全图的最短路for (int i = 1; i <= 1e5 + 1; i ++)ans[i] = 1e10;ans[s] = 0, q.push({0, s});for (int i = 1; i <= m && cin >> x >> y >> z; i ++)v[x].push_back({y, z});while (q.size()){now = q.top(), q.pop();if (vis[now.second])continue;vis[now.second] = 1;for (auto u : v[now.second]){if (ans[u.first] > ans[now.second] + u.second){ans[u.first] = ans[now.second] + u.second;if (!vis[u.first])q.push({ans[u.first], u.first});}}}for (int i = 1; i <= n; i ++)cout << ans[i] << " ";return 0;}```### SPFA```cpp#include <bits/stdc++.h>#define int long longusing namespace std;int n, m, s, x, y, z, ans[100010];bool vis[100010];vector<pair<int, int> > v[100010];bool spfa(){queue<int> q;q.push(s), vis[s] = 1, ans[s] = 0;while (q.size()){int now = q.front();q.pop(), vis[now] = 0;for (auto u : v[now]){if (ans[u.first] > ans[now] + u.second){ans[u.first] = ans[now] + u.second;if (!vis[u.first])vis[u.first] = 1, q.push(u.first);}}}return false;}signed main(){cin >> n >> m >> s;//由s至全图的最短路for (int i = 1; i <= m && cin >> x >> y >> z; i ++)v[x].push_back({y, z});for (int i = 1; i <= n; i ++)ans[i] = 2e9;spfa();for (int i = 1; i <= n; i ++)cout << (ans[i] == 2e9 ? -1 : ans[i]) << " ";return 0;}```## 多源最短路### Floyd```cpp#include <bits/stdc++.h>#define int long longusing namespace std;int n, m, q, x, y, z, ans[110][110];signed main(){cin >> n >> m >> q;for (int i = 1; i <= n; i ++){for (int j = 1; j <= n; j ++)ans[i][j] = (i == j ? 0 : 1e15);}for (int i = 1; i <= m && cin >> x >> y >> z; i ++)ans[x][y] = min(ans[x][y], z);for (int k = 1; k <= n; k ++){for (int i = 1; i <= n; i ++){for (int j = 1; j <= n; j ++)ans[i][j] = min(ans[i][j], ans[i][k] + ans[k][j]);}}while (q -- && cin >> x >> y)cout << (ans[x][y] == 1e15 ? -1 : ans[x][y]) << endl;return 0;}```## 割点### Tarjan```cpp#include <bits/stdc++.h>using namespace std;int n, m, x, y, num, sum, root, low[20010], dfn[20010];bool ans[20010];vector<int> v[20010];void dfs(int now){low[now] = dfn[now] = ++ num;int child = 0;for (auto u : v[now]){if (!dfn[u])child ++, dfs(u), low[now] = min(low[u], low[now]), sum += (!ans[now] && now != root && low[u] >= dfn[now]), ans[now] |= (now != root && low[u] >= dfn[now]);//判定1else low[now] = min(low[now], dfn[u]);}if (now == root && child >= 2)sum += !ans[now], ans[now] = 1;//判定2}int main(){cin >> n >> m;for (int i = 1; i <= m && cin >> x >> y; i ++)v[x].push_back(y), v[y].push_back(x);for (int i = 1; i <= n; i ++){if(!dfn[i])root = i, dfs(i);}cout << sum << endl;//数量for (int i = 1; i <= n; i ++){if (ans[i])cout << i << " ";}return 0;}```## 割边(桥)### Tarjan```cpp#include <bits/stdc++.h>using namespace std;int n, m, a, b, low[200], dfn[200], num;vector<int> v[200];priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q;//按照由小至大顺序输出void dfs(int now, int fa){low[now] = dfn[now] = ++ num;for (auto u : v[now]){if (fa == u)continue;if (!dfn[u]){dfs(u, now), low[now] = min(low[now], low[u]);if (low[u] > dfn[now])q.push({min(now, u), max(now, u)});//满足判定条件}else low[now] = min(low[now], dfn[u]);}}int main(){cin >> n >> m;for (int i = 1; i <= m && cin >> a >> b; i ++)v[a].push_back(b), v[b].push_back(a);//无向图存边dfs(1, 0);while (q.size()){cout << q.top().first << " " << q.top().second << endl;q.pop();}return 0;}```## 拓扑排序```cpp#include <bits/stdc++.h>using namespace std;int n, m, x, y, d[1010];vector<int> v[1010];queue<int> q;queue<int> ans;int main(){cin >> n >> m;for (int i = 1; i <= m && cin >> x >> y; i ++)v[x].push_back(y), d[y] ++;for (int i = 1; i <= n; i ++){if (!d[i])q.push(i);//统计入度为0的点}while (q.size()){int now = q.front();q.pop(), ans.push(now);for (auto u : v[now]){d[u] --;//删边if (!d[u])q.push(u);}}if (ans.size() < n){cout << -1;return 0;}while (ans.size()){cout << ans.front() << " ";ans.pop();}return 0;}```## 最小生成树### Kruskal```cpp#include <bits/stdc++.h>#define int long longusing namespace std;struct node{int x, y, z;}a[400010];int n, m, ans, sum, fa[5010];bool cmp(node b, node c){return b.z < c.z;//对权值进行排序}int find(int now){if (fa[now] == now)return now;return fa[now] = find(fa[now]);}signed main(){cin >> n >> m;for (int i = 1; i <= m && cin >> a[i].x >> a[i].y >> a[i].z; i ++)a[i + m] = node{a[i].y, a[i].x, a[i].z};//无向图存边sort(a + 1, a + 2 * m + 1, cmp);for (int i = 1; i <= n; i ++)fa[i] = i;for (int i = 1; i <= 2 * m; i ++){if (find(a[i].x) != find(a[i].y))fa[find(a[i].y)] = fa[find(a[i].x)], ans += a[i].z, sum ++;if (sum >= n - 1){//已添完所有边cout << ans;return 0;}}cout << "orz";//不连通return 0;}```### Prim```cpp#include <bits/stdc++.h>using namespace std;long long n, m, x, y, z, sum, cnt, d[5010];bool vis[5010];vector<pair<long long, long long> > v[5010];int main(){cin >> n >> m;for (int i = 1; i <= m && cin >> x >> y >> z; i ++)v[x].push_back({y, z}), v[y].push_back({x, z});for (int i = 0; i <= n; i ++)d[i] = 1e9;d[1] = 0;for (int i = 1; i <= n; i ++){long long id = 0;for (int j = 1; j <= n; j ++){if (!vis[j] && d[j] < d[id])id = j;}if (!id){cout << "orz";return 0;}vis[id] = 1, sum += d[id];for (auto u : v[id]){if (!vis[u.first])d[u.first] = min(d[u.first], 1LL * u.second);}}cout << sum;return 0;}```## 点双连通分量### Tarjan```cpp#include <bits/stdc++.h>using namespace std;int n, m, x, y, low[500010], dfn[500010], num, root, sum = 1;vector<int> v[500010], ans[500010];stack<int> sta;void dfs(int now, int fa){low[now] = dfn[now] = ++ num, sta.push(now);int child = 0;for (auto u : v[now]){if (u == fa)continue;if (!dfn[u]){child ++, dfs(u, now), low[now] = min(low[now], low[u]);if (low[u] >= dfn[now]){while (sta.top() != u)ans[sum].push_back(sta.top()), sta.pop();ans[sum].push_back(u), ans[sum ++].push_back(now), sta.pop();//找到割点,记录}}else low[now] = min(low[now], dfn[u]);}if (now == root && !child)ans[sum ++].push_back(now);//特判只有一个点}int main(){ios::sync_with_stdio(0);cin.tie();cout.tie();cin >> n >> m;for (int i = 1; i <= m && cin >> x >> y; i ++)v[x].push_back(y), v[y].push_back(x);for (int i = 1; i <= n; i ++){if (!dfn[i])root = i, dfs(i, 0);//图不保证连通}cout << -- sum << endl;//压行的奇怪手段for (int i = 1; i <= sum; i ++){cout << ans[i].size() << " ";for (auto u : ans[i])cout << u << " ";cout << endl;}return 0;}```## 边双连通分量### Tarjan```cpp#include <bits/stdc++.h>using namespace std;int n, m, x, y, low[500010], dfn[500010], num, sum;vector<int> ans[500010];stack<int> sta;vector<pair<int, int> > v[500010];void tarjan(int now, int fa){low[now] = dfn[now] = ++ num, sta.push(now);for (auto u : v[now]){if (u.second == (fa ^ 1))continue;//这里的fa表示边的编号if (!dfn[u.first]){tarjan(u.first, u.second), low[now] = min(low[now], low[u.first]);}else low[now] = min(low[now], dfn[u.first]);}if (low[now] == dfn[now]){//为割边(桥),记录ans[++ sum].push_back(now);while (sta.top() != now)ans[sum].push_back(sta.top()), sta.pop();sta.pop();}}int main(){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);cin >> n >> m;for (int i = 1; i <= m && cin >> x >> y; i ++)v[x].push_back({y, i << 1}), v[y].push_back({x, i << 1 | 1});//为每条边编号for (int i = 1; i <= n; i ++){if (!dfn[i])tarjan(i, 0);//图不保证连通}cout << sum << endl;for (int i = 1; i <= sum; i ++){cout << ans[i].size() << " ";for (auto u : ans[i])cout << u << " ";cout << endl;}return 0;}```## 强连通分量### Tarjan```cpp#include <bits/stdc++.h>using namespace std;int n, m, x, y, dfn[10010], low[10010], num, sum;bool vis[10010];vector<int> v[10010], ans[10010];stack<int> sta;void dfs(int now){dfn[now] = low[now] = ++ num, sta.push(now), vis[now] = true;for (auto u : v[now]){if (!dfn[u])dfs(u), low[now] = min(low[now], low[u]);else if (vis[u])low[now] = min(low[now], dfn[u]);}if (low[now] == dfn[now]){//割边ans[++ sum].push_back(now), vis[now] = false;while (sta.top() != now)ans[sum].push_back(sta.top()), vis[sta.top()] = false, sta.pop();sta.pop();}}int main(){cin >> n >> m;for (int i = 1; i <= m && cin >> x >> y; i ++)v[x].push_back(y);for (int i = 1; i <= n; i ++){if (!dfn[i])dfs(i);}cout << sum << endl;for (int i = 1; i <= sum; i ++){cout << ans[i].size() << " ";for (auto u : ans[i])cout << u << " ";cout << endl;}return 0;}```## 负(权)环### SPFA```cpp#include <bits/stdc++.h>using namespace std;int T, n, m, x, y, z, ans[2010], sum[2010];bool vis[2010];vector<pair<int, int> > v[2010];bool spfa(int now){for (int i = 1; i <= n; i ++)vis[i] = 0, sum[i] = 0, ans[i] = 1e9;//初始化queue<int> q;q.push(now), ans[now] = 0, vis[now] = 1;while (q.size()){int now = q.front();q.pop(), vis[now] = 0;for (auto u : v[now]){if (ans[u.first] > ans[now] + u.second){ans[u.first] = ans[now] + u.second, sum[u.first] = sum[now] + 1;if (sum[u.first] >= n)return true;//若经过的点超过整张图的点数则存在负(权)环if (!vis[u.first])vis[u.first] = 1, q.push(u.first);}}}return false;}int main(){cin >> n >> m;bool flag = false;for (int i = 1; i <= 2001; i ++)v[i].clear();for (int i = 1; i <= m && cin >> x >> y >> z; i ++)v[x].push_back({y, z});for (int i = 1; i <= n; i ++){//图不保证连通,需要每个子图都遍历一次if (!vis[i]){if (spfa(i)){cout << "YES" << endl;flag = true;break;}}}if (!flag)cout << "NO" << endl;return 0;}```## KMP```cpp#include <bits/stdc++.h>using namespace std;string s1, s2;int l1, l2, n[1000010];int main(){cin >> s1 >> s2;l1 = s1.size(), l2 = s2.size();for (int i = 1, j = 0; i < l2; i ++){while (j && s2[i] != s2[j])j = n[j - 1];n[i] = (j += (s2[i] == s2[j]));}for (int i = 0, j = 0; i < l1; i ++){while (j && s1[i] != s2[j])j = n[j - 1];if (s1[i] == s2[j])j ++;if (j == l2){cout << i - l2 + 2 << '\n';j = n[j - 1];}}return 0;}```## 哈希### 进制哈希```cpp#include <bits/stdc++.h>#define int unsigned long longusing namespace std;int n, ans, now, l = 132135461;//随意一个数(最好是质数)string s;map<int, bool> m;int Hash(string x){int sum = 0, len = x.size();for (int i = 0; i < len; i ++)sum = (sum * l) + x[i];//模拟进制return sum;}signed main(){ios::sync_with_stdio(0);cin.tie(0);cin >> n;for (int i = 1; i <= n && cin >> s; i ++)now = Hash(s), ans += !m[now], m[now] = 1;cout << ans;return 0;}```## ST表```cpp#include <bits/stdc++.h>using namespace std;int n, m, l, r, maxn[100010][18], k;//从第i个数开始长度为2^j的序列最大值int main(){cin >> n >> m;for (int i = 1; i <= n && cin >> maxn[i][0]; i ++);k = log2(n);for (int i = 1; i <= k; i ++){for (int j = 1; j + (1 << i) - 1 <= n; j ++)maxn[j][i] = max(maxn[j][i - 1], maxn[j + (1 << i - 1)][i - 1]);//O(nlogn)更新ST表}while (m -- && cin >> l >> r){k = log2(r - l + 1);cout << max(maxn[l][k], maxn[r - (1 << k) + 1][k]) << '\n';//找最大能覆盖的区间}return 0;}```## 树的重心```cpp#include <bits/stdc++.h>using namespace std;int n, x, y, son[50010], w[50010];vector<int> v[50010];priority_queue<int, vector<int>, greater<int> > ans;void dfs(int fa, int now){son[now] = 1, w[now] = 0;for (auto u : v[now]){if (u != fa)dfs(now, u), son[now] += son[u], w[now] = max(w[now], son[u]);}w[now] = max(w[now], n - son[now]);if (w[now] <= n / 2)ans.push(now);}int main(){cin >> n;for (int i = 1; i < n && cin >> x >> y; i ++)v[x].push_back(y), v[y].push_back(x);dfs(0, 1);while (ans.size()){cout << ans.top() << " ";ans.pop();}return 0;}```## 树的直径### 树形 dp```cpp#include <bits/stdc++.h>#define int long longusing namespace std;int n, x, y, z, f[500010], ans;vector<pair<int, int> > v[500010];void dfs(int fa, int now){for (auto u : v[now]){if (u.first != fa)dfs(now, u.first), ans = max(ans, f[now] + f[u.first]+ u.second), f[now] = max(f[now], f[u.first] + u.second);}}signed main(){ios::sync_with_stdio(0);cin.tie(0);cin >> n;for (int i = 1; i < n && cin >> x >> y >> z; i ++)v[x].push_back({y, z}), v[y].push_back({x, z});dfs(0, 1);cout << ans;return 0;}```### dfs```cpp#include <bits/stdc++.h>using namespace std;int n, x, y, d[100010], s, sum;vector<int> v[100010];void dfs(int fa, int now){for (auto u : v[now]){if (u != fa)d[u] = d[now] + 1, s = (d[u] > d[s] ? u : s), dfs(now, u);}}int main(){cin >> n;for (int i = 1; i < n && cin >> x >> y; i ++)v[x].push_back(y), v[y].push_back(x);dfs(0, 1), d[s] = 0, dfs(0, s);cout << d[s];return 0;}```## LCA### 倍增```cpp#include <bits/stdc++.h>using namespace std;int n, m, s, x, y, k, fa[500010][20], d[500010];vector<int> v[500010];void dfs(int now, int f){d[now] = d[f] + 1, fa[now][0] = f;for (int i = 1; (1 << i) <= d[now]; i ++)fa[now][i] = fa[fa[now][i - 1]][i - 1];//类似ST表预处理祖先节点for (auto u : v[now]){if (u != f)dfs(u, now);}}int solve(int a, int b){if (d[b] > d[a])swap(a, b);//始终让a在b后面(深度较大)for (int i = 19; i >= 0; i --){if (d[a] - (1 << i) >= d[b])a = fa[a][i];//把a的深度跳至b}if (a == b)return a;for (int i = 19; i >= 0; i --){if (fa[a][i] != fa[b][i])a = fa[a][i], b = fa[b][i];//a和b同时跳}return fa[a][0];}int main(){cin >> n >> m >> s;for (int i = 1; i < n && cin >> x >> y; i ++)v[x].push_back(y), v[y].push_back(x);dfs(s, 0);for (int i = 1; i <= m && cin >> x >> y; i ++)cout << solve(x, y) << '\n';return 0;}```### 树链剖分```cpp#include <bits/stdc++.h>using namespace std;const int N = 5e5 + 10;int n, m, r, x, y, d[N], fa[N], siz[N], son[N], top[N];vector<int> v[N];void dfs1(int now, int f){//找重儿子d[now] = d[f] + 1, fa[now] = f, siz[now] = 1;for (auto u : v[now])if (u != f)dfs1(u, now), siz[now] += siz[u], son[now] =!son[now] || siz[son[now]] < siz[u] ? u : son[now];}void dfs2(int now, int tnow){top[now] = tnow;if (!son[now])return;//叶子结点直接返回dfs2(son[now], tnow);//先递归重链for (auto u : v[now])if (u != fa[now] && u != son[now])dfs2(u, u);//以轻儿子为链头的重链}int LCA(){while (top[x] != top[y]){//不在同一重链上if (d[top[x]] < d[top[y]])swap(x, y);//让 x 所在重链的链头更深x = fa[top[x]];//跳链}return d[x] < d[y] ? x : y;}int main(){ios::sync_with_stdio(0), cin.tie(0);cin >> n >> m >> r;for (int i = 1; i < n && cin >> x >> y; i ++)v[x].push_back(y), v[y].push_back(x);dfs1(r, 0), dfs2(r, r);while (m -- && cin >> x >> y)cout << LCA() << '\n';return 0;}```## 拓展欧几里得```cpp#include <bits/stdc++.h>#define int long longusing namespace std;int x, y;void exgcd(int a, int b){//求特殊解if (!b){x = 1, y = 0;//ax + by = a 时的解return;}exgcd(b, a % b);//辗转相除int now = x;x = y, y = now - a / b * y;}signed main(){int a, b;cin >> a >> b;exgcd(a, b);cout << (x % b + b) % b;//最小解return 0;}```## 斜率优化(下凸包格式)```cpp#include <bits/stdc++.h>using namespace std;long long n, nx, bk = 1, fr = 1, f[1000010];long double X(int x){return ();}long double Y(int x){return ();}long double slope(int x, int y){return (Y(x) - Y(y)) / (X(x) - X(y));//求斜率}int main(){cin >> n;()//输入for (int i = 1; i <= n; i ++){//单调队列维护下凸壳while (fr < bk && slope(q[fr + 1], q[fr]) < ())fr ++;f[i] = ();while (fr < bk && slope(q[bk], q[bk - 1]) > slope(i, q[bk]))bk --;q[++ bk] = i;}cout << f[n];return 0;}```## 差分约束```cpp#include <bits/stdc++.h>using namespace std;long long n, m, x, y, z, d[5010], sum[5010];bool vis[5010];vector<pair<int, int> > v[5010];queue<int> q;bool spfa(){//跑最短路q.push(0), vis[0] = 1, sum[0] = 1;while (q.size()){int now = q.front();q.pop(), vis[now] = 0;for (auto u : v[now]){if (d[u.first] > d[now] + u.second){d[u.first] = d[now] + u.second, sum[u.first] = sum[now] + 1;if (sum[u.first] > n + 1)return true;//注意多了一个点if (!vis[u.first])vis[u.first] = 1, q.push(u.first);}}}return false;}int main(){cin >> n >> m;for (int i = 1; i <= m && cin >> x >> y >> z; i ++)v[y].push_back({x, z});//存边for (int i = 1; i <= n; i ++)d[i] = 1e9, v[0].push_back({i, 0});//建一个虚点if (spfa()){cout << "NO";return 0;}for (int i = 1; i <= n; i ++)cout << d[i] << " ";return 0;}```## 字典树```cpp#include <bits/stdc++.h>using namespace std;int T, n, q, cnt[3000010], t[3000010][65], num;string s;int g(char x){return (x >= 'A' && x <= 'Z' ? x - 'A' : (x >= 'a' && x <= 'z' ? x - 'a' + 26 : x - '0' + 52));}int main(){ios::sync_with_stdio(0);cin.tie(0);cin >> T;while (T -- && cin >> n >> q){for (int i = 1, np = 0; i <= n && cin >> s; np = 0, i ++){for (int j = 0, l = s.size(), c = g(s[j]); j < l; np = t[np][c], cnt[np] ++, c = g(s[++ j])){if (!t[np][c])t[np][c] = ++ num;//建树}}for (int i = 1, ans = 0; i <= q && cin >> s; ans = 0, i ++){for (int j = 0, l = s.size(), np = 0, c = g(s[j]); j < l; np = t[np][c], ans = cnt[np], c = g(s[++ j])){if (!t[np][c]){//查询ans = 0;break;}}cout << ans << '\n';}for (int i = 0; i <= num; cnt[i ++] = 0){for (int j = 0; j <= 64; j ++)t[i][j] = 0;}num = 0;//多测清空}return 0;}```## Manacher(马拉车)```cpp#include <bits/stdc++.h>using namespace std;int d[22000010], l, r, ans;string s;char now[22000010];int main(){ios::sync_with_stdio(0);cin.tie(0);cin >> s;now[0] = '#';for (int i = 0, l = s.size(); i < l; ++ i)now[i << 1 | 1] = s[i], now[(i << 1) + 2] = '#';//加间隔符for (int i = 0, n = (s.size() << 1) + 1; i < n; ans = max(ans, d[i ++])){int k = i > r ? 1 : min(d[l + r - i], r - i + 1);//找到当前最长的回文串长度(总长的一半)while (0 <= i - k && i + k < n && now[i - k] == now[i + k])k ++;//不断拓展d[i] = k --;if (i + k > r)l = i - k, r = i + k;//更新最大右端点}cout << ans - 1;return 0;}```## AC 自动机```cpp#include <bits/stdc++.h>using namespace std;struct node{int son[30], fail, cnt;}a[1000010];int n, num;string s;void insert(string now){//插入int id = 0, l = now.size();for (int i = 0; i < l; i ++)id = a[id].son[now[i] - 97] ? a[id].son[now[i] - 97] :a[id].son[now[i] - 97] = ++ num;a[id].cnt ++;//末尾标记}void getfail(){//bfs 构建失配指针queue<int> q;for (int i = 0; i < 26; i ++)if (a[0].son[i])//初始化第一层a[a[0].son[i]].fail = 0, q.push(a[0].son[i]);while (!q.empty()){int now = q.front(), f = a[now].fail;q.pop();for (int i = 0; i < 26; i ++)if (a[now].son[i])//存在儿子a[a[now].son[i]].fail = a[f].son[i], q.push(a[now].son[i]);//连到下一个并入队else a[now].son[i] = a[f].son[i];}}int query(){//查找文本串int id = 0, now = a[id].son[s[0] - 97], sum = 0, l = s.size();for (int i = 0; i < l; id = a[id].son[s[i ++] - 97], now = a[id].son[s[i] - 97])while (now && a[now].cnt + 1)//不断找当前字符一样的后缀sum += a[now].cnt, a[now].cnt = -1, now = a[now].fail;return sum;}int main(){ios::sync_with_stdio(0);cin.tie(0);cin >> n;while (n -- && cin >> s)insert(s);getfail();cin >> s;cout << query();return 0;}```## BSGS### BSGS(Baby Step,Gaint Step)算法给出 $a、b$ 和一个质数 $p$,求最小非负整数 $x$ 满足 $a ^ x \equiv b \pmod p$。4 行未改动于是把所有 $b \times a ^ j \bmod p$ 放入一个哈希表,再遍历 $i$ 查找 $a ^ {i \times t} \bmod p$ 即可做到 $\sqrt p$ 实现啦!注意特判若 $a = 0$ 且 $b \ne 0$ 时无解。```cpp#include <bits/stdc++.h>using namespace std;struct custom_hash{//哈希static uint64_t splitmix64(uint64_t x){x += 0x9e3779b97f4a7c15;x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;x=(x ^ (x >> 27)) * 0x94d049bb133111eb;23 行未改动cin >> p >> b >> n;return BSGS(b, n, p), 0;}```### exBSGS 算法若 $a$ 与 $p$ 不互质,则需要不断将 $a$、$b$ 和 $p$ 除去 $\gcd(a, \ p)$,其中若出现 $b$ 有余数的情况则必然无解。记每次的 $\gcd(a_i, \ p_i) = d_i$,则得到:$$\prod \limits _ {i = 1} ^ k \frac{a}{d_i} · a ^ {x - k} \equiv \frac{b}{\prod \limits _ {i = 1} ^ k d_i} \pmod p$$用 BSGS 求解即可。```cpp#include <bits/stdc++.h>#define int long longusing namespace std;22 行未改动while (cin >> a >> p >> b && a && b && p)exBSGS(a % p, b % p, p);return 0;}```12344 行未改动91011121314151617181920212223 行未改动46474849505152535455565758596061626364656622 行未改动8990919293## BSGS### BSGS(Baby Step,Gaint Step)算法给出 $a、b$ 和一个质数 $p$,求最小非负整数 $x$ 满足 $a ^ x \equiv b \pmod p$。4 行未改动于是把所有 $b \times a ^ j \bmod p$ 放入一个哈希表,再遍历 $i$ 查找 $a ^ {i \times t} \bmod p$ 即可做到 $\sqrt p$ 实现啦!注意特判若 $a = 0$ 且 $b \ne 0$ 时无解。:::success[code]```cpp#include <bits/stdc++.h>using namespace std;struct custom_hash{static uint64_t splitmix64(uint64_t x){x += 0x9e3779b97f4a7c15;x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;x=(x ^ (x >> 27)) * 0x94d049bb133111eb;23 行未改动cin >> p >> b >> n;return BSGS(b, n, p), 0;}```:::### exBSGS 算法若 $a$ 与 $p$ 不互质,则需要不断将 $a$、$b$ 和 $p$ 除去 $\gcd(a, \ p)$,其中若出现 $b$ 有余数的情况则必然无解。记每次的 $\gcd(a_i, \ p_i) = d_i$,则得到:$$\prod \limits _ {i = 1} ^ k \frac{a}{d_i} · a ^ {x - k} \equiv \frac{b}{\prod \limits _ {i = 1} ^ k d_i} \pmod p$$用 BSGS 求解即可。:::success[code]```cpp#include <bits/stdc++.h>#define int long longusing namespace std;22 行未改动while (cin >> a >> p >> b && a && b && p)exBSGS(a % p, b % p, p);return 0;}```:::