标题
增加 1 行删除 1 行
1模板(更新中)1关于 BSGS
正文
增加 73 行删除 1042 行
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062## 树状数组### 单点修改 + 单点查询```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>#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 (X(x) - X(y)) / (Y(x) - Y(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;}```123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293## BSGS### BSGS(Baby Step,Gaint Step)算法给出 $a、b$ 和一个质数 $p$,求最小非负整数 $x$ 满足 $a ^ x \equiv b \pmod p$。首先根据鸽笼原理得到 $x \le p$,所以令 $x = i \times t - j$,其中 $t = \left \lceil \sqrt p \right \rceil$,$0 \le j < t$,$0 \le i \le t$。那么原方程变为 $a ^ {i \times t - j} \equiv b \pmod p$,移项得 $a ^ {i \times t} \equiv b \times a ^ j \pmod p$。于是把所有 $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;return x ^ (x >> 31);}size_t operator()(uint64_t x) const{static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();return splitmix64(x + FIXED_RANDOM);}};void BSGS(int a, int b, int p){unordered_map<int, int, custom_hash> vis;int t = ceil(sqrt(p)), val = 1;for (int i = 0; i < t; i ++)vis[1LL * b * val % p] = i, val = 1LL * val * a % p;if (!a && b){cout << "no solution";return;}a = val, val = 1;for (int i = 0, j; i <= t; i ++){j = vis.find(val) == vis.end() ? - 1 : vis[val];if (~ j && i * t - j >= 0){cout << i * t - j;return;}val = 1LL * val * a % p;}cout << "no solution";}int main(){int p, b, n;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;void exBSGS(int a, int b, int p){if (p == 1 || b == 1){cout << "0\n";return;}if (!a){cout << (b ? "No Solution\n" : "1\n");return;}int d, k = 0, D = 1;while ((d = __gcd(a, p)) != 1){if (b % d){cout << "No Solution\n";return;}b /= d, p /= d, D = D * a / d % p, k ++;if (D == b){cout << k << '\n';return;}}unordered_map<int, int> vis;int t = ceil(sqrt(p)), i, val = b, v = 1;for (i = 0; i < t; v = v * a % p, i ++)vis[val] = i, val = val * a % p;for (i = 1, val = D; i <= t; i ++){if (vis.count(val = val * v % p)){cout << i * t - vis[val] + k << '\n';return;}}cout << "No Solution\n";}signed main(){ios::sync_with_stdio(0), cin.tie(0);int p, a, b;while (cin >> a >> p >> b && a && b && p)exBSGS(a % p, b % p, p);return 0;}```:::