C++模板合集(持续更新中)
读写
快读快写火车头(+modint)
#include <bits/stdc++.h>
using namespace std;
namespace Tool {
#define ttt template<typename T>
#define ll long long
#define i128 __int128
#define lld long double
#define For(i, a, b) for(int i = (a); i <= (b); ++i)
#define Rep(i, a, b) for(int i = (a); i >= (b); --i)
#define IOS ios::sync_with_stdio(0)
#define While(x) while(x)
#define eps (1e-9)
#define endl ('\n')
namespace FSIO {
static constexpr int BUF_SIZE = 1 << 20;
char in_buf[BUF_SIZE], out_buf[BUF_SIZE], *in_ptr = in_buf, *in_end = in_buf, *out_ptr = out_buf, ch, a[40];
static constexpr lld qp[25] = { 1, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21 };
static int db_num_digit = 8, f = 1;
class FSI {
void load_input() { in_ptr = in_buf, in_end = in_buf + cin.rdbuf()->sgetn(in_buf, BUF_SIZE); }
char get() { return (in_ptr == in_end) ? (load_input(), in_ptr == in_end ? EOF : *in_ptr++) : *in_ptr++; }
void getdec(i128& x, i128& cnt) { While(ch != EOF && isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = get(), ++cnt; }
ttt void getnum(T& x) { While(ch != EOF && isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = get(); }
ttt void getsign(T& f) { While((ch = get()) != EOF && !isdigit(ch)) f = (ch == '-' ? -1 : f); }
public:
FSI& operator>>(char& ch) {
while((ch = get()) != EOF && isblank(ch));
return *this;
}
FSI& operator>>(char* s) {
while((ch = get()) != EOF && (isblank(ch) || ch == '\n'));
if(ch == EOF) return (*s = '\0'), *this;
while(ch != EOF && !isblank(ch) && ch != '\n') *s++ = ch, ch = get();
return (*s = '\0'), *this;
}
FSI& operator>>(string& s) {
s.clear();
while((ch = get()) != EOF && (isblank(ch) || ch == '\n'));
if(ch == EOF) return *this;
while(ch != EOF && !isblank(ch) && ch != '\n') s.push_back(ch), ch = get();
return *this;
}
FSI& operator>>(lld& res) {
i128 s = 0, f = 1, cnt = 0, t = 0, scale = 1;
return getsign(f), getnum(s), (ch == '.' ? (void)(ch = '0', cnt = -1, getdec(t, cnt)) : (void)(0)), scale = (cnt <= 20 ? qp[cnt] : powl(10, cnt)), (res = ((lld)s + (static_cast<lld>(t) / static_cast<lld>(scale))) * f), *this;
}
ttt FSI& operator>>(T& res) { return res = 0, ch = '\0', f = 1, getsign(f), getnum(res), res *= f, *this; }
} sci;
class FSO {
void flush_output() { fwrite(out_buf, 1, out_ptr - out_buf, stdout), out_ptr = out_buf; }
void put(char ch) { return (out_ptr >= out_buf + BUF_SIZE ? flush_output() : void()), *out_ptr++ = ch, void(); }
ttt void write(T res, ll cnt = 0) {
char* p = a + sizeof(a);
while(res) *--p = (res % 10) ^ 48, res /= 10, --cnt;
while(cnt > 0) *--p = 48, --cnt;
while(p != a + sizeof(a)) put(*p++);
}
void putstring(const char* s) { While(*s) put(*s++); }
public:
FSO() {
static FSO* instance = this;
atexit([] { instance->flush_output(); });
}
void db_digit(const int num) const { db_num_digit = num; }
void flush() { flush_output(); }
FSO& operator<<(const char ch) { return put(ch), *this; }
FSO& operator<<(const string s) {
for(char ch: s) put(ch);
return *this;
}
FSO& operator<<(char* s) { return putstring(s), *this; }
FSO& operator<<(const char* s) { return putstring(s), *this; }
FSO& operator<<(lld res) { return (res < 0 ? put('-'), res = -res + eps : res += eps), write(static_cast<ll>(res), 1), res -= static_cast<ll>(res), (db_num_digit ? put('.'), (write(static_cast<ll>(res * qp[db_num_digit]), (ll)db_num_digit)) : (void)(0)), *this; }
FSO& operator<<(const bool res) { return put(res ^ 48), *this; }
ttt FSO& operator<<(const T res) { return (res < 0 ? put('-'), write(-1ll * res, 1) : write(res, 1)), *this; }
} sco;
#define cin Tool::FSIO::sci
#define cout Tool::FSIO::sco
#define fsi Tool::FSIO::FSI
#define fso Tool::FSIO::FSO
}
struct modint {
private:
static constexpr int mod = 1e9 + 7;
int x;
public:
modint(): x(0) {}
modint(ll num) { x = (num >= 0 ? (num >= mod ? num % mod : num) : (num % mod + mod) % mod); }
int operator!() { return !x; }
modint& operator=(const modint other) { return x = other.x, *this; }
modint operator+(const modint other) const { return modint(0ll + x + other.x); }
modint operator-(const modint other) const { return modint(0ll + x - other.x); }
modint operator*(const modint other) const { return modint(1ll * x * other.x); }
modint operator/(const modint other) const { return *this * qpow(other, mod - 2); }
modint operator+=(const modint other) { return *this = *this + other; }
modint operator-=(const modint other) { return *this = *this - other; }
modint operator*=(const modint other) { return *this = *this * other; }
modint operator/=(const modint other) { return *this = *this / other; }
bool operator==(const modint other) const { return x == other.x; }
bool operator!=(const modint other) const { return !(*this == other); }
modint qpow(modint x, ll k) const {
modint res = 1;
while(k) ((k & 1) ? res *= x : modint()), k >>= 1, x *= x;
return res;
}
int val() { return x; }
friend fsi& operator>>(fsi& in, modint& num) { return in >> num.x, in; }
friend fso& operator<<(fso& out, modint num) { return out << num.x; }
#define modint Tool::modint
};
}
signed main() {
IOS;
return 0;
}关流模板
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
#define For(i, a, b) for(int i = (a); i <= (b); ++i)
#define Rep(i, a, b) for(int i = (a); i >= (b); --i)
signed main() {
ios::sync_with_stdio(0), cin.tie(0);
return 0;
}简单数据结构
栈(stack)
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
#define For(i, a, b) for(int i = (a); i <= (b); ++i)
#define Rep(i, a, b) for(int i = (a); i >= (b); --i)
template<typename Tp>
class Stack {
vector<Tp> h;
int siz;
public:
Stack() { h.clear(), h.push_back(Tp()), siz = 0; }
void push(Tp x) { h.push_back(x), siz++; }
bool pop() { return siz == 0 ? 0 : (h.pop_back(), siz--, 1); }
Tp top() { return h[siz]; }
Tp size() { return siz; }
Tp empty() { return siz == 0; }
};
void solve() {
int n;
cin >> n;
Stack<unsigned ll> st;
string s;
unsigned ll x;
For(i, 1, n) {
cin >> s;
if(s == "push") cin >> x, st.push(x);
if(s == "query") {
if(st.empty()) cout << "Anguei!" << endl;
else
cout << st.top() << endl;
}
if(s == "pop") cout << (st.pop() ? "" : "Empty\n");
if(s == "size") cout << st.size() << endl;
}
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0);
int T;
cin >> T;
while(T--) solve();
return 0;
}队列(queue)
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
#define For(i, a, b) for(int i = (a); i <= (b); ++i)
#define Rep(i, a, b) for(int i = (a); i >= (b); --i)
template<typename Tp>
class Queue {
static const int M = 1000000;
int h = 1, t = 0, siz = 0;
Tp que[M + 10];
public:
void push(Tp x) {
que[++t] = x, siz++;
if(t == M) t = 0;
}
bool pop() {
if(!siz) return 0;
h++, siz--;
if(h == M) h = 1;
return 1;
}
Tp front() { return !siz ? -1 : que[h]; }
bool empty() { return !siz; }
int size() { return siz; }
};
signed main() {
ios::sync_with_stdio(0), cin.tie(0);
int n, op, x;
cin >> n;
Queue<int> q;
For(i, 1, n) {
cin >> op;
if(op == 1) {
cin >> x, q.push(x);
} else if(op == 2) {
if(!q.pop()) cout << "ERR_CANNOT_POP" << endl;
} else if(op == 3) {
if(q.empty()) cout << "ERR_CANNOT_QUERY" << endl;
else
cout << q.front() << endl;
} else
cout << q.size() << endl;
}
return 0;
}双端队列(deque)
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
#define For(i, a, b) for(int i = (a); i <= (b); ++i)
#define Rep(i, a, b) for(int i = (a); i >= (b); --i)
template<typename Tp>
class Deque {
int siz = 0;
list<Tp> data;
public:
void push_back(Tp x) { data.push_back(x), siz++; }
void pop_back() { siz ? data.pop_back(), siz-- : 0; }
void push_front(Tp x) { data.insert(data.begin(), x), siz++; }
void pop_front() { siz ? data.erase(data.begin()), siz-- : 0; }
int size() { return siz; }
Tp front() { return siz ? data.front() : -1; }
Tp back() { return siz ? data.back() : -1; }
};
Deque<int> dq[1000010];
signed main() {
ios::sync_with_stdio(0), cin.tie(0);
int m, q, a, x;
cin >> q;
string op;
while(q--) {
cin >> op >> a;
if(op == "push_back") cin >> x, dq[a].push_back(x);
if(op == "pop_back") dq[a].pop_back();
if(op == "push_front") cin >> x, dq[a].push_front(x);
if(op == "pop_front") dq[a].pop_front();
if(op == "size") cout << dq[a].size() << endl;
if(op == "front") dq[a].front() == -1 ? void() : void(cout << dq[a].front() << endl);
if(op == "back") dq[a].back() == -1 ? void() : void(cout << dq[a].back() << endl);
}
return 0;
}二分堆(优先队列)
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
#define For(i, a, b) for(int i = (a); i <= (b); ++i)
#define Rep(i, a, b) for(int i = (a); i >= (b); --i)
template<typename T, typename _Compare = less<T>>
class priority_Queue {
vector<T> h;
int siz;
void swap(T& x, T& y) {
T t = x;
x = y, y = t;
}
_Compare cmp;
public:
priority_Queue() { h.clear(), h.push_back(T()), siz = 0; }
void push(T x) {
h.push_back(x), siz++;
int now = siz;
while(now >> 1) {
ll nx = now >> 1;
if(cmp(h[now], h[nx])) swap(h[nx], h[now]);
else
break;
now = nx;
}
}
void pop() {
swap(h[1], h[siz--]);
h.pop_back();
int now = 1;
while((now << 1) <= siz) {
int nx = now << 1;
if(nx + 1 <= siz && h[nx + 1] < h[nx]) nx++;
if(cmp(h[nx], h[now])) swap(h[nx], h[now]);
else
break;
now = nx;
}
}
T top() { return h[1]; }
bool empty() { return siz == 0; }
};
signed main() {
ios::sync_with_stdio(0), cin.tie(0);
int n;
cin >> n;
priority_Queue<int> q;
int op, x;
For(i, 1, n) {
cin >> op;
switch(op) {
case 1: cin >> x, q.push(x); break;
case 2: cout << q.top() << endl; break;
case 3: q.pop(); break;
}
}
return 0;
}树链剖分
HLD
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define For(i, a, b) for(int i = (a); i <= (b); ++i)
const int N = 2e5 + 10;
int n, m, r, mod, w[N];
int id[N], res = 0, wt[N];
struct SegTree {//线段树
struct segtree {
int l, r;
ll a, laz;
} t[N << 2];
void add(int k, ll f) {
t[k].a = (t[k].a + f * (t[k].r - t[k].l + 1)) % mod, t[k].laz += f;
}
void pushdown(int k) {
add(k << 1, t[k].laz), add(k << 1 | 1, t[k].laz), t[k].laz = 0;
}
void pushup(int k) { t[k].a = (t[k << 1].a + t[k << 1 | 1].a) % mod; }
void build(int k, int l, int r) {
t[k] = { l, r, 0, 0 };
if(l == r) return t[k].a = wt[l], void();
int mid = (l + r) >> 1;
build(k << 1, l, mid), build(k << 1 | 1, mid + 1, r);
pushup(k);
}
#define mid (t[k].l + t[k].r) >> 1
void query(int k, int u, int v) {
if(u <= t[k].l && t[k].r <= v) {
res = (res + t[k].a) % mod;
return;
}
pushdown(k);
if(u <= mid) query(k << 1, u, v);
if(mid < v) query(k << 1 | 1, u, v);
}
void update(int k, int u, int v, int f) {
if(u <= t[k].l && t[k].r <= v) {
t[k].a += f * (t[k].r - t[k].l + 1), t[k].laz += f;
return;
}
pushdown(k);
if(u <= mid) update(k << 1, u, v, f);
if(mid < v) update(k << 1 | 1, u, v, f);
pushup(k);
}
#undef mid
} st;
struct Query_on_a_tree {
int tot, head[N], son[N], dn, fa[N], dep[N], siz[N], top[N];
struct edge {
int to, nx;
} e[N << 1];
void add(int u, int v) { e[++tot] = { v, head[u] }, head[u] = tot; }
void dfs1(int u, int f, int deep) {
dep[u] = deep, fa[u] = f, siz[u] = 1;
int maxson = -1;
for(int i = head[u]; i; i = e[i].nx) {
int v = e[i].to;
if(v == f) continue;
dfs1(v, u, deep + 1);
siz[u] += siz[v];
if(siz[v] > maxson) son[u] = v, maxson = siz[v];
}
}
void dfs2(int u, int topf) {
id[u] = ++dn, wt[dn] = w[u], top[u] = topf;
if(!son[u]) return;
dfs2(son[u], topf);
for(int i = head[u]; i; i = e[i].nx) {
int v = e[i].to;
if(v == fa[u] || v == son[u]) continue;
dfs2(v, v);
}
}
void updSon(int u, int k) { st.update(1, id[u], id[u] + siz[u] - 1, k); }
int LCA(int u, int v) {
while(top[u] != top[v]) {
if(dep[top[u]] < dep[top[v]]) swap(u, v);
u = fa[top[u]];
}
if(dep[u] > dep[v]) swap(u, v);
return u;
}
void updRange(int u, int v, int k) {
k %= mod;
while(top[u] != top[v]) {
if(dep[top[u]] < dep[top[v]]) swap(u, v);
st.update(1, id[top[u]], id[u], k);
u = fa[top[u]];
}
if(dep[u] > dep[v]) swap(u, v);
st.update(1, id[u], id[v], k);
}
int qRange(int u, int v) {
int ans = 0;
while(top[u] != top[v]) {
if(dep[top[u]] < dep[top[v]]) swap(u, v);
res = 0, st.query(1, id[top[u]], id[u]);
ans = (ans + res) % mod, u = fa[top[u]];
}
if(dep[u] > dep[v]) swap(u, v);
res = 0, st.query(1, id[u], id[v]), ans = (ans + res) % mod;
return ans;
}
int qSon(int u) {
res = 0, st.query(1, id[u], id[u] + siz[u] - 1);
return res;
}
} tr;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m >> r >> mod;
For(i, 1, n) cin >> w[i];
for(int i = 1, u, v; i < n; i++) cin >> u >> v, tr.add(u, v), tr.add(v, u);
tr.dfs1(r, 0, 1), tr.dfs2(r, r);
st.build(1, 1, n);
while(m--) {
int k, u, v, z;
cin >> k;
if(k == 1) cin >> u >> v >> z, tr.updRange(u, v, z);
if(k == 2) cin >> u >> v, cout << tr.qRange(u, v) << endl;
if(k == 3) cin >> u >> v, tr.updSon(u, v);
if(k == 4) cin >> u, cout << tr.qSon(u) << endl;
}
return 0;
}Tarjan
割点
#include <bits/stdc++.h>
using namespace std;
// #define gdb 1//Debug FSIO
const int N = 1e5 + 10;
#define endl '\n'
#define ll long long
#define For(i, a, b) for(int i = (a); i <= (b); ++i)
#define Rep(i, a, b) for(int i = (a); i >= (b); --i)
int n, m, head[N], tot;
struct edge {
int to, nx;
} e[N << 1];
int idx, dfn[N], low[N];
set<int> ans;
void tj(int rt, int u) {
low[u] = dfn[u] = ++idx;
int child = 0;
for(int i = head[u]; i; i = e[i].nx) {
int v = e[i].to;
if(!dfn[v]) {
tj(rt, v), low[u] = min(low[u], low[v]);
if(low[v] >= dfn[u]) ++child;
} else
low[u] = min(low[u], dfn[v]);
}
if(rt == u && child >= 2) ans.insert(u);
if(rt != u && child >= 1) ans.insert(u);
}
void add(int u, int v) { e[++tot] = { v, head[u] }, head[u] = tot; }
signed main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
for(int i = 1, u, v; i <= m; ++i) cin >> u >> v, add(u, v), add(v, u);
For(i, 1, n) if(!dfn[i]) idx = 0, tj(i, i);
cout << ans.size() << endl;
for(int u: ans) cout << u << " ";
return 0;
}桥
#include <bits/stdc++.h>
using namespace std;
#define For(i, a, b) for(int i = (a); i <= (b); ++i)
const int N = 160;
int n, m, head[N], tot, idx;
int dfn[N], low[N], mp[N][N];
void add(int u, int v) { mp[u][v] = 1; }
struct node {
int u, v;
bool operator<(const node& other) const { return u == other.u ? v < other.v : u < other.u; }
};
vector<node> a;
void tj(int u, int fa) {
bool flag = 0;
dfn[u] = low[u] = ++idx;
For(v, 1, n) {
if(!mp[u][v]) continue;
if(!dfn[v]) {
tj(v, u);
low[u] = min(low[u], low[v]);
if(low[v] > dfn[u]) a.push_back({ min(u, v), max(u, v) });
}
if(dfn[v] && v != fa) low[u] = min(low[u], dfn[v]);
}
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
for(int i = 1, u, v; i <= m; ++i) cin >> u >> v, add(u, v), add(v, u);
For(i, 1, n) if(!dfn[i]) tj(i, i);
sort(a.begin(), a.end());
for(auto [u, v]: a) cout << u << " " << v << endl;
return 0;
}字典树(Trie)
code
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define For(i, a, b) for(int i = (a); i <= (b); ++i)
#define Rep(i, a, b) for(int i = (a); i >= (b); --i)
const int N = 3e6 + 10, M = 65;
class Trie {
char ch[128];
int t[N][M], tot, count[N], siz[N];
public:
void clear() {
For(i, 0, tot) For(j, 0, M - 1) t[i][j] = 0;
For(i, 0, tot) count[i] = 0;
tot = 0;
}
void pre() {
For(i, 0, 9) ch[i + '0'] = i;
For(i, 0, 25) ch[i + 'A'] = i + 10, ch[i + 'a'] = i + 36;
}
void insert(const string& s) {
int p = 0;
for(char c: s) p = !t[p][ch[c]] ? t[p][ch[c]] = ++tot : t[p][ch[c]], count[p]++;
}
int find(const string& s) {
int p = 0;
for(char c: s) {
if(!t[p][ch[c]]) return 0;
p = t[p][ch[c]];
}
return count[p];
}
} trie;
int n, q;
string s;
void solve() {
cin >> n >> q;
trie.clear();
For(i, 1, n) cin >> s, trie.insert(s);
while(q--) {
cin >> s;
cout << trie.find(s) << endl;
}
}
signed main() {
trie.pre();
int T;
cin >> T;
while(T--) solve();
return 0;
}AC自动机
简单版
#include <bits/stdc++.h>
using namespace std;
#define For(i, a, b) for(int i = (a); i <= (b); ++i)
#define Rep(i, a, b) for(int i = (a); i >= (b); --i)
#define ll long long
#define endl '\n'
class AC_automaton {
static const int Bit = 1e6 + 10;
struct Tree {
int son[26], idx, fail;
void init() { memset(son, 0, sizeof(son)), idx = fail = 0; }
} tr[Bit];
int tot;
public:
void init() { tot = 0, tr[0].init(); }
void build() {
queue<int> q;
For(i, 0, 25) if(tr[0].son[i]) q.push(tr[0].son[i]);
while(!q.empty()) {
int u = q.front();
q.pop();
For(i, 0, 25) {
if(tr[u].son[i]) tr[tr[u].son[i]].fail = tr[tr[u].fail].son[i], q.push(tr[u].son[i]);
else
tr[u].son[i] = tr[tr[u].fail].son[i];
}
}
}
void insert(char s[]) {
int u = 0;
for(int i = 1; s[i]; ++i) {
int& son = tr[u].son[s[i] - 'a'];
if(!son) son = ++tot, tr[son].init();
u = son;
}
tr[u].idx++;
}
int query(char t[]) {
int u = 0, res = 0;
for(int i = 1; t[i]; ++i) {
u = tr[u].son[t[i] - 'a'];
for(int j = u; j && tr[j].idx != -1; j = tr[j].fail)
res += tr[j].idx, tr[j].idx = -1;
}
return res;
}
} ac;
const int Len = 1e6 + 10;
int n;
char s[Len];
signed main() {
ios::sync_with_stdio(0), cin.tie(0);
ac.init();
cin >> n;
For(i, 1, n) cin >> (s + 1), ac.insert(s);
ac.build();
cin >> (s + 1);
cout << ac.query(s) << endl;
return 0;
}简单版II
#include <bits/stdc++.h>
using namespace std;
#define For(i, a, b) for(int i = (a); i <= (b); ++i)
#define Rep(i, a, b) for(int i = (a); i >= (b); --i)
#define ll long long
#define endl '\n'
class AC_automaton {
static const int Bit = 1e6 + 10;
struct Tree {
int son[26], fail, idx;
void init() { memset(son, 0, sizeof(son)), idx = fail = 0; }
} tr[Bit];
int tot;
public:
void init() { tot = 0, tr[0].init(); }
void build() {
queue<int> q;
For(i, 0, 25) if(tr[0].son[i]) q.push(tr[0].son[i]);
while(!q.empty()) {
int u = q.front();
q.pop();
For(i, 0, 25) {
if(tr[u].son[i]) tr[tr[u].son[i]].fail = tr[tr[u].fail].son[i], q.push(tr[u].son[i]);
else
tr[u].son[i] = tr[tr[u].fail].son[i];
}
}
}
void insert(char s[], int idx) {
int u = 0;
for(int i = 1; s[i]; ++i) {
int& son = tr[u].son[s[i] - 'a'];
if(!son) son = ++tot, tr[son].init();
u = son;
}
tr[u].idx = idx;
}
int query(char t[], int cnt[]) {
int u = 0, res = 0;
for(int i = 1; t[i]; ++i) {
u = tr[u].son[t[i] - 'a'];
for(int j = u; j && tr[j].idx != -1; j = tr[j].fail) ++cnt[tr[j].idx];
}
For(i, 0, tot) if(tr[i].idx) res = max(res, cnt[tr[i].idx]);
return res;
}
} ac;
const int N = 160, M = 75, Len = 1e6 + 10;
int n, cnt[N];
char s[N][M], t[Len];
signed main() {
ios::sync_with_stdio(0), cin.tie(0);
while((cin >> n) && n != 0) {
ac.init();
For(i, 1, n) cin >> (s[i] + 1), ac.insert(s[i], i), cnt[i] = 0;
ac.build();
cin >> (t + 1);
int x = ac.query(t, cnt);
cout << x << endl;
For(i, 1, n) if(cnt[i] == x) cout << s[i] + 1 << endl;
}
return 0;
}AC自动机
#include <bits/stdc++.h>
using namespace std;
#define For(i, a, b) for(int i = (a); i <= (b); ++i)
#define ll long long
#define endl '\n'
const int N = 2e5 + 10, Len = 2e6 + 10;
class AC_automaton {
static const int Bit = 2e5 + 10;
struct Tree {
int son[26], fail, idx, ans;
void init() { memset(son, 0, sizeof(son)), ans = idx = fail = 0; }
} tr[Bit];
int tot, pidx;
vector<int> g[Bit];
public:
int ans[N];
void init() { tot = pidx = 0, tr[0].init(); }
void build() {
queue<int> q;
For(i, 0, 25) if(tr[0].son[i]) q.push(tr[0].son[i]), g[0].push_back(tr[0].son[i]);
while(!q.empty()) {
int u = q.front();
q.pop();
For(i, 0, 25) {
if(tr[u].son[i])
tr[tr[u].son[i]].fail = tr[tr[u].fail].son[i],
g[tr[tr[u].fail].son[i]].push_back(tr[u].son[i]),
q.push(tr[u].son[i]);
else
tr[u].son[i] = tr[tr[u].fail].son[i];
}
}
}
void insert(char s[], int& idx) {
int u = 0;
for(int i = 1; s[i]; ++i) {
int& son = tr[u].son[s[i] - 'a'];
if(!son) son = ++tot, tr[son].init();
u = son;
}
if(!tr[u].idx) tr[u].idx = ++pidx;
idx = tr[u].idx;
}
void dfs(int u) {
for(int v: g[u]) dfs(v), tr[u].ans += tr[v].ans;
ans[tr[u].idx] = tr[u].ans;
}
void query(char t[]) {
int u = 0;
for(int i = 1; t[i]; ++i) u = tr[u].son[t[i] - 'a'], tr[u].ans++;
}
} ac;
int n, cnt[N];
char s[Len];
signed main() {
ios::sync_with_stdio(0), cin.tie(0);
ac.init();
cin >> n;
For(i, 1, n) cin >> (s + 1), ac.insert(s, cnt[i]), ac.ans[i] = 0;
ac.build();
cin >> (s + 1);
ac.query(s);
ac.dfs(0);
For(i, 1, n) cout << ac.ans[cnt[i]] << endl;
return 0;
}最短路
Floyd
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
#define For(i, a, b) for(int i = (a); i <= (b); ++i)
#define Rep(i, a, b) for(int i = (a); i >= (b); --i)
const int N = 110, inf = 1e9;
int n, m, f[N][N];
signed main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
For(i, 1, n) For(j, 1, n) f[i][j] = inf;
For(i, 1, n) f[i][i] = 0;
for(int i = 1, u, v, w; i <= m; ++i) cin >> u >> v >> w, f[u][v] = f[v][u] = min(f[u][v], w);
For(k, 1, n) For(i, 1, n) For(j, 1, n) f[i][j] = min(f[i][j], f[i][k] + f[k][j]);
For(i, 1, n) {
For(j, 1, n) cout << f[i][j] << " ";
cout << endl;
}
return 0;
}SPFA
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
#define For(i, a, b) for(int i = (a); i <= (b); ++i)
#define Rep(i, a, b) for(int i = (a); i >= (b); --i)
const int N = 2e3 + 10, M = 3e3 + 10, inf = 1e9;
int n, m, head[N], tot, dis[N], cnt[N], vis[N];
struct edge {
int to, nx;
ll w;
} e[M << 1];
void add(int u, int v, int w) { e[++tot] = { v, head[u], w }, head[u] = tot; }
bool spfa(int s) {
For(i, 1, n) dis[i] = inf, vis[i] = 0, cnt[i] = 0;
queue<int> q;
dis[s] = 0, vis[s] = 1, q.push(s);
while(!q.empty()) {
int u = q.front();
q.pop(), vis[u] = 0;
for(int i = head[u]; i; i = e[i].nx) {
int v = e[i].to, w = e[i].w;
if(dis[v] > dis[u] + w) {
dis[v] = dis[u] + w, cnt[v] = cnt[u] + 1;
if(cnt[v] > n) return 0;// 出现负环
if(!vis[v]) q.push(v), vis[v] = 1;
}
}
}
return 1;
}
void solve() {
cin >> n >> m;
tot = 0;
For(i, 1, n) head[i] = 0;
for(int i = 1, u, v, w; i <= m; ++i) cin >> u >> v >> w, add(u, v, w), w >= 0 ? add(v, u, w) : void();
if(spfa(1)) cout << "NO" << endl;
else
cout << "YES" << endl;
For(i, 1, tot) e[i] = { 0, 0, 0 };
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0);
int T;
cin >> T;
while(T--) solve();
return 0;
}Dijkstra
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
#define For(i, a, b) for(int i = (a); i <= (b); ++i)
#define Rep(i, a, b) for(int i = (a); i >= (b); --i)
const ll N = 5e5 + 10, inf = 1e18;
int n, m, s, head[N], tot;
ll dis[N];
struct edge {
int to, nx;
ll w;
} e[N << 1];
void add(int u, int v, int w) { e[++tot] = { v, head[u], w }, head[u] = tot; }
struct node {
int u;
ll w;
bool operator<(const node& other) const { return w > other.w; }
};
priority_queue<node> q;
void dij(int s) {
bitset<N> vis;
For(i, 1, n) dis[i] = INT_MAX;
dis[s] = 0, q.push({ s, 0 });
while(!q.empty()) {
auto [u, w] = q.top();
q.pop();
if(vis[u]) continue;
vis[u] = 1;
for(int i = head[u]; i; i = e[i].nx) {
auto [v, nx, w] = e[i];
if(dis[v] > dis[u] + w) dis[v] = dis[u] + w, q.push({ v, dis[v] });
}
}
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m >> s;
for(int i = 1, u, v, w; i <= m; ++i) cin >> u >> v >> w, add(u, v, w);
dij(s);
For(i, 1, n) cout << dis[i] << " ";
return 0;
}Johnson
#include <bits/stdc++.h>
using namespace std;
#define ttt template<typename T>
#define ll long long
#define For(i, a, b) for(int i = (a); i <= (b); ++i)
#define endl ('\n')
const int N = 6e3 + 10, M = 1e4 + 10;
const ll inf = 1e9, Inf = 2e9;
int n, m, tot, head[N], cnt[N];
struct edge {
int to, w, nx;
} e[M];
ll sum, dis[N], h[N];
void add(int u, int v, int w) { e[++tot] = { v, w, head[u] }, head[u] = tot; }
bool vis[N];
bool spfa() {
For(i, 0, n) h[i] = LLONG_MAX / 2;// 初始化
queue<int> q;
h[0] = 0, vis[0] = 1, q.push(0);
while(!q.empty()) {
int u = q.front();
q.pop(), vis[u] = 0;
for(int i = head[u]; i; i = e[i].nx) {
int v = e[i].to, w = e[i].w;
if(h[v] > h[u] + w) {
h[v] = h[u] + w, cnt[v] = cnt[u] + 1;
if(cnt[v] > n) return 0;// 出现负环
if(!vis[v]) q.push(v), vis[v] = 1;
}
}
}
return 1;
}
struct node {
int v;
ll w;
bool operator<(const node b) const { return w > b.w; }
};
void dij(int s) {
For(i, 1, n) vis[i] = 0, dis[i] = Inf;
priority_queue<node> q;
dis[s] = 0, q.push({ s, 0 });
while(!q.empty()) {
auto [u, w] = q.top();
q.pop();
if(vis[u]) continue;
vis[u] = 1;
for(int i = head[u]; i; i = e[i].nx) {
int v = e[i].to, w = e[i].w + h[u] - h[v];// 重力势能差值
if(dis[v] > dis[u] + w) dis[v] = dis[u] + w, q.push({ v, dis[v] });
}
}
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> m;
for(int i = 1, u, v, w; i <= m; ++i) cin >> u >> v >> w, add(u, v, w);
// 建立超级源点
For(i, 1, n) add(0, i, 0);
// 跑 spfa 计算重力势能+判负环
if(!spfa()) return cout << -1, 0;
For(i, 1, n) {// n 遍 dij 计算全源最短路
dij(i), sum = 0;
For(j, 1, n) if(i != j)
sum += j * (dis[j] > inf ? inf : ((ll)dis[j] - h[i] + h[j]));
cout << sum << endl;
}
return 0;
}并查集
DSU
#include <bits/stdc++.h> using namespace std; #define endl ‘\n’ #define ll long long #define For(i, a, b) for(int i = (a); i <= (b); ++i) #define Rep(i, a, b) for(int i = (a); i >= (b); —i) class DSU { int n; vector<int> fa; public: DSU(int _n): n(_n), fa(n + 1) { For(i, 0, n) fa[i] = i; } int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } bool isunion(int x, int y) { return find(x) == find(y); } bool unite(int x, int y) { return (x = find(x)) == (y = find(y)) ? 0 : (fa[x] = y, 1); } }; signed main() { ios::sync_with_stdio(0), cin.tie(0); return 0; }
最小生成树
Kruskal
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
#define For(i, a, b) for(int i = (a); i <= (b); ++i)
#define Rep(i, a, b) for(int i = (a); i >= (b); --i)
const int M = 1e6 + 10;
class DSU {
int n;
vector<int> fa;
public:
DSU(int _n): n(_n), fa(n + 1) { For(i, 0, n) fa[i] = i; }
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
bool isunion(int x, int y) { return find(x) == find(y); }
bool unite(int x, int y) { return (x = find(x)) == (y = find(y)) ? 0 : (fa[x] = y, 1); }
};
struct line {
int u, v, w;
bool operator<(const line& other) const { return w < other.w; }
} lin[M];
int n, m, ans;
signed main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
For(i, 1, m) cin >> lin[i].u >> lin[i].v >> lin[i].w;
sort(lin + 1, lin + m + 1);
DSU dsu(n);
For(i, 1, m) {
auto [u, v, w] = lin[i];// C++17
if(dsu.unite(u, v)) ans += w;
}
cout << ans << endl;
return 0;
}Prim
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
const int N = 5e3 + 10, M = 2e5 + 10, inf = 2e9;
int n, m, now = 1, res, head[N], dis[N], tot, cnt;
struct edge {
int v, nx, w;
} e[M << 1];
void add(int u, int v, int w) { e[++tot] = { v, head[u], w }, head[u] = tot; }
struct node {
int u, w;
bool operator<(const node y) const { return w > y.w; }
};
priority_queue<node> q;
bool vis[N];
void prim() {
memset(dis, 0x3f, sizeof(dis)), dis[1] = 0, q.push({ 1, 0 });
while(!q.empty()) {
if(cnt >= n) break;
int u = q.top().u, w = q.top().w;
q.pop();
if(vis[u]) continue;
vis[u] = 1, ++cnt, res += w;
for(int i = head[u], v = e[i].v, w = e[i].w; i; i = e[i].nx, v = e[i].v, w = e[i].w)
if(w < dis[v]) dis[v] = w, q.push({ v, w });
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
for(int i = 1, u, v, w; i <= m; i++) cin >> u >> v >> w, add(u, v, w), add(v, u, w);
prim();
if(cnt == n) cout << res;
if(cnt != n) cout << "orz";
return 0;
}欧拉函数
单值
ll phi(ll x) { ll res = x; for(int i = 2; i * i <= x; ++i) if(x % i == 0) { res -= res / i; while(x % i == 0) x /= i; } if(x > 1) res -= res / x; return res; }
线性
#define For(i, a, b) for(int i = (a); i <= (b); ++i)
vector<int> phi, pr;
void Phi(int n) {
phi.resize(n + 1);
phi[1] = 1;
For(i, 2, n) {
if(!phi[i]) phi[i] = i - 1, pr.push_back(i);
for(int j: pr) {
if(i * j > n) break;
phi[i * j] = phi[i] * (j - (i % j != 0));
if(j * j > i) break;
}
}
}