跳到正文

个人记录

Notes「AtCoder Library 指南」

denominator约 33 分钟
原文
文章信息与版本

文章信息

发布
2025年6月8日
收录
2026年1月12日
更新
2026年11月14日
篇幅
约 33 分钟

版本

  1. v2当前
  2. v1

官方文档

注意:以下代码需要使用

C++
#include <atcoder/all>
// 这个头文件可以换为 <atcoder/***> *** 为列出的二级标题的英文
using namespace atcoder;

声明

  • 如果使用函数时不满足性质,会有未定义行为发生。
  • 使用下面简写来表示元素的类型:
    • uint 对应 unsigned int;
    • ll 对应 long long;
    • ull 对应 unsigned long long。
  • 钦定 00=10^0=1。
  • 除非特别指出,图可以有重边或自环。

数据结构

树状数组(fenwicktree)

定义

C++
fenwick_tree<T> fw(int n)

定义了一个初始所有元素为 00 且长度为 nn 的树状数组。

限制:

  • T 只能是 int、uint、ll、ull 或 modint(在 <atcoder/modint> 中,下同);
  • 0≤n≤1080\leq n\leq10^8。

时空复杂度:O(n)\mathcal O(n)。

单点加

C++
void fw.add(int p, T x)

执行操作 a[p] += x,a 为树状数组的元素序列。

限制:

  • 0≤p<n0\leq p<n。注意 Atcoder 库中所有数据结构都是从 0\bm 0 开始标号的,下同。

时空复杂度:O(log⁡n)\mathcal O(\log n)。

区间求和

C++
T fw.sum(int l, int r)

返回 a[l] + a[l + 1] + ... + a[r - 1]。注意最后一项是 a[r - 1],下同。若 T 不是 modint,则自然溢出。

限制:

  • 0≤l≤r≤n0\leq l\leq r\leq n。

时空复杂度:O(log⁡n)\mathcal O(\log n)。

参考实现

practice2_b

不带懒标记的线段树(segtree)

定义

C++
segtree<S, op, e> seg(int n)
segtree<S, op, e> seg(vector<S> v)

以下内容要被定义:

  • 一个类型 S;
  • 一个二元操作 S op(S a, S b),返回 a⋅ba\cdot b,设此函数需要 O(T)\mathcal O(T) 时空;
  • 一个返回定义在 S 上的 op 单位元 S e(),设此函数需要 O(T)\mathcal O(T) 时空。

集合 SS、二元运算 ⋅\cdot 与单位元 ee 应满足:

  • ∀a,b,c∈S\forall a,b,c\in S,有 (a⋅b)⋅c=a⋅(b⋅c)(a\cdot b)\cdot c=a\cdot(b\cdot c);
  • ∀a∈S\forall a\in S,a⋅e=e⋅a=aa\cdot e=e\cdot a=a。

对于第一种定义方式,可以定义一个初始所有元素为 00 且长度为 nn 的线段树。

对于第二种定义方式,可以定义一个初始长度为 v.size(),元素为 v 中元素的线段树。

限制:

  • 0≤n≤1080\leq n\leq10^8。

时空复杂度:O(Tn)\mathcal O(Tn)。

单点修改

C++
void seg.set(int p, S x)

它将 a[p] 覆盖为 x。

限制:

  • 0≤p<n0\leq p<n。

时空复杂度:O(Tlog⁡n)\mathcal O(T\log n)。

单点查询

C++
S seg.get(int p)

它返回 a[p]。

限制:

  • 0≤p<n0\leq p<n。

时空复杂度:O(Tlog⁡n)\mathcal O(T\log n)。

区间 ⋅\bm\cdot 运算

C++
S seg.prod(int l, int r)

它返回 op(a[l], ..., a[r - 1])(如果上述结合律和单位元性质成立)。如果 l=rl=r,它返回单位元 e()。

限制:

  • 0≤l≤r≤n0\leq l\leq r\leq n。

时空复杂度:O(Tlog⁡n)\mathcal O(T\log n)。

全局 ⋅\bm\cdot 运算

C++
S seg.all_prod()

它返回 op(a[0], ..., a[n - 1])(如果上述结合律和单位元性质成立)。如果 n=0n=0,它返回单位元 e()。

限制:

  • 无。

时空复杂度:O(T)\mathcal O(T)。

在线段树右半部分上二分

C++
int seg.max_right<f>(int l)
int seg.max_right<F>(int l, F f)

对于第一种实现方式,函数 bool f(S x) 需要被定义。

对于第二种实现方式,一个给定 S 返回 bool 的函数类型 F 和函数 f。

设 f 函数需要 O(T)\mathcal O(T) 时空。

它返回一个位置 r 满足:

  • r = l 或 f(op(a[l], a[l + 1], ..., a[r - 1])) = true;
  • r = n 或 f(op(a[l], a[l + 1], ..., a[r])) = false。

若 f 单调,r 是满足 f(op(a[l], a[l + 1], ..., a[r - 1])) = true 的最大值。

限制:

  • 在不同时候对同一个 x 调用 f 函数,返回值不变。
  • f(e()) = true。
  • 0≤l≤n0\leq l\leq n。

时空复杂度:O(Tlog⁡n)\mathcal O(T\log n)。

在线段树左半部分上二分

C++
int seg.min_left<f>(int r)
int seg.min_left<F>(int r, F f)

对于第一种实现方式,函数 bool f(S x) 需要被定义。

对于第二种实现方式,一个给定 S 返回 bool 的函数类型 F 和函数 f。

设 f 函数需要 O(T)\mathcal O(T) 时空。

它返回一个位置 l 满足:

  • l = r 或 f(op(a[l], a[l + 1], ..., a[r - 1])) = true;
  • l = 0 或 f(op(a[l - 1], a[l], ..., a[r - 1])) = false。

若 f 单调,l 是满足 f(op(a[l], a[l + 1], ..., a[r - 1])) = true 的最小值。

限制:

  • 在不同时候对同一个 x 调用 f 函数,返回值不变。
  • f(e()) = true。
  • 0≤r≤n0\leq r\leq n。

时空复杂度:O(Tlog⁡n)\mathcal O(T\log n)。

参考实现

practice2_j

带懒标记的线段树(lazysegtree)

定义

C++
lazy_segtree<S, op, e, F, mapping, composition, id> seg(int n);
lazy_segtree<S, op, e, F, mapping, composition, id> seg(vector<T> v);

以下内容要被定义:

  • 一个元素的类型 S;
  • 一个二元操作 S op(S a, S b),返回 a⋅ba\cdot b,设此函数需要 O(T)\mathcal O(T) 时空;
  • 一个返回定义在 S 上的 op 单位元 S e(),设此函数需要 O(T)\mathcal O(T) 时空;
  • 一个映射的类型 F;
  • 一个映射函数 S mapping(F f, S x),返回 f(x)f(x),设此函数需要 O(T)\mathcal O(T) 时空;
  • 一个复合函数 F composition(F f, F g),返回 f∘gf\circ g,设此函数需要 O(T)\mathcal O(T) 时空;
  • 一个特征映射 F id(),设此函数需要 O(T)\mathcal O(T) 时空。

集合 SS、二元运算 ⋅\cdot、单位元 ee、映射集合 FF、复合函数 ∘\circ、特征映射 id\text{id} 应满足:

  • ∀a,b,c∈S\forall a,b,c\in S,有 (a⋅b)⋅c=a⋅(b⋅c)(a\cdot b)\cdot c=a\cdot(b\cdot c);
  • ∀a∈S\forall a\in S,a⋅e=e⋅a=aa\cdot e=e\cdot a=a。
  • ∀x∈S\forall x\in S,有 id(x)=x\text{id}(x)=x,且 id∈F\text{id}\in F;
  • ∀f,g∈F\forall f,g\in F,有 f∘g∈Ff\circ g\in F;
  • ∀f∈F\forall f\in F,且 x,y∈Sx,y\in S,有 f(x⋅y)=f(x)⋅f(y)f(x\cdot y)=f(x)\cdot f(y)。

对于第一种定义方式,可以定义一个初始所有元素为 00 且长度为 nn 的线段树。

对于第二种定义方式,可以定义一个初始长度为 v.size(),元素为 v 中元素的线段树。

限制:

  • 0≤n≤1080\leq n\leq10^8。

时空复杂度:O(Tn)\mathcal O(Tn)。

单点修改

C++
void seg.set(int p, S x)

它将 a[p] 覆盖为 x。

限制:

  • 0≤p<n0\leq p<n。

时空复杂度:O(Tlog⁡n)\mathcal O(T\log n)。

单点查询

C++
S seg.get(int p)

它返回 a[p]。

限制:

  • 0≤p<n0\leq p<n。

时空复杂度:O(Tlog⁡n)\mathcal O(T\log n)。

区间 ⋅\bm\cdot 运算

C++
S seg.prod(int l, int r)

它返回 op(a[l], ..., a[r - 1])(如果上述结合律和单位元性质成立)。如果 l=rl=r,它返回单位元 e()。

限制:

  • 0≤l≤r≤n0\leq l\leq r\leq n。

时空复杂度:O(Tlog⁡n)\mathcal O(T\log n)。

全局 ⋅\bm\cdot 运算

C++
S seg.all_prod()

它返回 op(a[0], ..., a[n - 1])(如果上述结合律和单位元性质成立)。如果 n=0n=0,它返回单位元 e()。

限制:

  • 无。

时空复杂度:O(T)\mathcal O(T)。

单点或区间 f\bm f 运算

C++
void seg.apply(int p, F f)
void seg.apply(int l, int r, F f)

对于第一种实现方式,它调用 a[p] = f(a[p])。

对于第二种实现方式,对于所有 l≤i<rl\leq i<r,它调用 a[i] = f(a[i])。

限制:

  • 对于第一种实现方式,0≤p<n0\leq p<n;
  • 对于第二种实现方式,0≤l≤r≤n0\leq l\leq r\leq n。

时空复杂度:O(Tlog⁡n)\mathcal O(T\log n)。

在线段树右半部分上二分

C++
int seg.max_right<g>(int l)
int seg.max_right<G>(int l, G g)

对于第一种实现方式,函数 bool g(S x) 需要被定义。

对于第二种实现方式,一个给定 S 返回 bool 的函数类型 G 和函数 g。

设 g 函数需要 O(T)\mathcal O(T) 时空。

它返回一个位置 r 满足:

  • r = l 或 g(op(a[l], a[l + 1], ..., a[r - 1])) = true;
  • r = n 或 g(op(a[l], a[l + 1], ..., a[r])) = false。

若 g 单调,r 是满足 g(op(a[l], a[l + 1], ..., a[r - 1])) = true 的最大值。

限制:

  • 在不同时候对同一个 x 调用 g 函数,返回值不变。
  • g(e()) = true。
  • 0≤l≤n0\leq l\leq n。

时空复杂度:O(Tlog⁡n)\mathcal O(T\log n)。

在线段树左半部分上二分

C++
int seg.min_left<g>(int r)
int seg.min_left<G>(int r, G g)

对于第一种实现方式,函数 bool g(S x) 需要被定义。

对于第二种实现方式,一个给定 S 返回 bool 的函数类型 F 和函数 g。

设 g 函数需要 O(T)\mathcal O(T) 时空。

它返回一个位置 l 满足:

  • l = r 或 g(op(a[l], a[l + 1], ..., a[r - 1])) = true;
  • l = 0 或 g(op(a[l - 1], a[l], ..., a[r - 1])) = false。

若 g 单调,l 是满足 g(op(a[l], a[l + 1], ..., a[r - 1])) = true 的最小值。

限制:

  • 在不同时候对同一个 x 调用 g 函数,返回值不变。
  • g(e()) = true。
  • 0≤r≤n0\leq r\leq n。

时空复杂度:O(Tlog⁡n)\mathcal O(T\log n)。

参考实现

practice2_k

practice2_l

字符串(string)

下面,s[l,r)s_{[l,r)} 指 slsl+1⋯sr−1s_ls_{l+1}\cdots s_{r-1},即这些元素依次连接。

SA

C++
vector<int> suffix_array(string s)
vector<int> suffix_array<T>(vector<T> s)
vector<int> suffix_array(vector<int> s, int upper)

它返回 sa 数组满足 ∀0≤i<n−1\forall 0\leq i< n-1 有 s[sai,n)<s[sai+1,n)s_{[\text{sa}_i,n)}<s_{[\text{sa}_{i+1},n)}。

限制:

  • 0≤n≤1080\leq n\leq10^8;
  • 在第二种实现方式中,T 只能是 int、uint、ll 或 ull;
  • 在第三种实现方式中,0≤upper≤1080\leq\text{upper}\leq10^8;
  • 在第三种实现方式中,∀0≤i<n\forall 0\leq i< n 有 0≤si≤upper0\leq s_i\leq\text{upper}。

时空复杂度:

  • 在第一种实现方式中,O(n)\mathcal O(n);
  • 在第二种实现方式中,时间 O(nlog⁡n)\mathcal O(n\log n),空间 O(n)\mathcal O(n);
  • 在第三种实现方式中,O(n+upper)\mathcal O(n+\text{upper})。

LCP 数组

需要先选择合理的 suffix_array 函数调用。

C++
vector<int> lcp_array(string s, vector<int> sa)
vector<int> lcp_array<T>(vector<T> s, vector<int> sa)

它返回 lcp 数组满足 ∀0≤i<n−1\forall 0\leq i< n-1 有 lcpi=∣lcp⁡(s[sai,n),s[sai+1,n))∣\text{lcp}_i=\left|\operatorname{lcp}(s_{[\text{sa}_i,n)},s_{[\text{sa}_{i+1},n)})\right|。现在才知道,这就是所谓的 height\textit{height} 数组……

限制:

  • sa 是 s 的 SA 数组;
  • 1≤n≤1081\leq n\leq10^8;
  • 在第二种实现方式中,T 只能是 int、uint、ll 或 ull。

时空复杂度:O(n)\mathcal O(n)。

Z 算法

C++
vector<int> z_algorithm(string s)
vector<int> z_algorithm<T>(vector<T> s)

它返回 z 数组满足 ∀0≤i<n\forall 0\leq i< n 有 zi=∣lcp⁡(s,s[i,n))∣z_i=\left|\operatorname{lcp}(s,s_{[i,n)})\right|。

限制:

  • 1≤n≤1081\leq n\leq10^8;
  • 在第二种实现方式中,T 只能是 int、uint、ll 或 ull。

时空复杂度:O(n)\mathcal O(n)。

参考实现

practice2_i

数学

数论(math)

快速幂

C++
ll pow_mod(ll x, ll n, int m)

返回 xn mod mx^n\bmod m。

限制:

  • n≥0n\geq 0;
  • m≥1m\geq 1。

时空复杂度:O(log⁡n)\mathcal O(\log n)。

求逆

C++
ll inv_mod(ll x, ll m)

返回 yy 满足 0≤y<m0\leq y<m 且 xy≡1(modm)xy\equiv1\pmod m。

限制:

  • x⊥mx\perp m(即 gcd⁡(x,m)=1\gcd(x,m)=1);
  • m≥1m\geq 1。

时空复杂度:O(log⁡m)\mathcal O(\log m)。

CRT

C++
pair<ll, ll> crt(vector<ll> r, vector<ll> m)

返回同余方程组(nn 为 r,mr,m 的长度)

x≡ri(modmi),∀0≤i<nx\equiv r_i\pmod{m_i},\forall 0\leq i<n

的解。

如果无解,返回 (0,0)(0,0)。

如果 n=1n=1,返回 (0,1)(0,1)。

否则返回 (y,z)(y,z),表示解为

x≡y(modz)x\equiv y\pmod z

限制:

  • ∣r∣=∣m∣|r|=|m|;
  • ∀0≤i<n\forall 0\leq i<n,有 mi≥0m_i\geq 0;
  • lcm⁡i=0n−1mi\mathop{\operatorname{lcm}}\limits_{i=0}^{n-1}m_i 可以用 ll 存下。

时空复杂度:O(nlog⁡lcm⁡i=0n−1mi)\mathcal O\left(n\log \mathop{\operatorname{lcm}}\limits_{i=0}^{n-1}m_i\right)。

类欧几里得算法

C++
ll floor_sum(ll n, ll m, ll a, ll b)

返回

∑i=0n−1⌊a⋅i+bm⌋\sum_{i=0}^{n-1}\left\lfloor\dfrac{a\cdot i+b}{m}\right\rfloor

自然溢出(对 2642^{64} 取模)。

限制:

  • 0≤n<2320\leq n<2^{32};
  • 1≤m<2321\leq m<2^{32}。

时空复杂度:O(log⁡m)\mathcal O(\log m)。

参考实现

practice2_c

卷积(convolution)

NTT 模数下的卷积

C++
vector<T> convolution<int m = 998244353>(vector<T> a, vector<T> b)
vector<static_modint<m>> convolution<int m>(vector<static_modint<m>> a, vector<static_modint<m>> b)

返回 aa 和 bb 的卷积序列

ci=∑j=0iajbi−jc_i=\sum_{j=0}^ia_jb_{i-j}

对 mm 取模的结果。若 a,ba,b 中任意一个是空的,返回值就是空的。第一种实现下,mm 默认为 998 244 353998\ 244\ 353。

限制:

  • 2≤m≤1092\leq m\leq10^9;
  • mm 为质数;
  • 存在整数 ll 使得 2l∣(m−1)2^l\mid(m-1),且 ∣a∣+∣b∣−1≤2l|a|+|b|-1\leq2^l;
  • 在第一种实现方式中,T 只能是 int、uint、ll 或 ull。

时空复杂度:O(nlog⁡n+log⁡m)\mathcal O(n\log n+\log m),其中 n=∣a∣+∣b∣n=|a|+|b|。

ll 范围内的卷积

C++
vector<ll> convolution_ll(vector<ll> a, vector<ll> b)

返回 aa 和 bb 的卷积序列

ci=∑j=0iajbi−jc_i=\sum_{j=0}^ia_jb_{i-j}

若 a,ba,b 中任意一个是空的,返回值就是空的。

限制:

  • ∣a∣+∣b∣−1≤224|a|+|b|-1\leq2^{24};
  • 卷积后,所有元素在 ll 内。

时空复杂度:O(nlog⁡n)\mathcal O(n\log n),其中 n=∣a∣+∣b∣n=|a|+|b|。

参考实现

practice2_f

Fp\bm{\mathbb{F}_p} 下的整数运算(modint)

定模数

可以使用 modint998244353、modint1000000007。

其他模数可使用 static_modint<p>,p 为模数。

不定模数

可以使用 setmod,见下。

还可以使用 dynamic_modint<p>,p 用于区分不同模数。


定义

C++
modint x()
modint x<T>(T y)

在第一种定义方式下,初始值为 00。

在第二种定义方式下,T 可以为 int、char、ull、bool 等,初始值为 y。

定义模数

C++
void modint::set_mod(int m)

定义了模数为 mm。必须事先调用。

限制:

  • 1≤m≤2×109+10001\leq m\leq2\times10^9+1000。

时空复杂度:O(1)\mathcal O(1)。

返回模数

C++
int modint::mod()

返回了模数。

限制:

  • 无。

时空复杂度:O(1)\mathcal O(1)。

返回储存的值

C++
int x.val();

返回了储存在 x 的值。

限制:

  • 无。

时空复杂度:O(1)\mathcal O(1)。

运算

可以支持以下运算。

C++
// 取相反数
-modint;

// 自增自减
modint++;
modint--;
++modint;
--modint;

// 四则运算
modint + modint;
modint - modint;
modint * modint;
modint / modint;

// 四则赋值运算
modint += modint;
modint -= modint;
modint *= modint;
modint /= modint;

// 关系运算
modint == modint;
modint != modint;

modint 与 int 运算时,int 类型会自动解析为 modint。

限制:

  • 做除法时,后者的值与模数互质。

时空复杂度:做除法为 O(log⁡mod)\mathcal O(\log\text{mod}),其余为 O(1)\mathcal O(1)。

快速幂(modint 下)

C++
modint x.pow(ll n)

返回 xnx^n。

限制:

  • n≥0n\geq 0。

时空复杂度:O(log⁡n)\mathcal O(\log n)

求逆(modint 下)

C++
modint x.inv()

返回 yy 满足 xy≡1(modm)xy\equiv1\pmod m。

限制:

  • x⊥modx\perp \text{mod}(即 gcd⁡(x,mod)=1\gcd(x,\text{mod})=1)。

时空复杂度:O(log⁡mod)\mathcal O(\log \text{mod})。

卡♂ 常♂ 大♂ 法

C++
modint modint::raw(int x)

前面说过,modint 与 int 运算时,int 类型会自动解析为 modint。这时,int 类型的量会对 mod 取模,即使值小于 mod。使用这个函数,可以直接把 x 转成 mod,减小常数。

限制:

  • 0≤x<mod0\leq x<\text{mod}。

图论

并查集(dsu)

定义

C++
dsu d(int n)

创建一个 nn 个点的空并查集。

限制:

  • 0≤n≤1080\leq n\leq 10^8。

时空复杂度:O(n)\mathcal O(n)。

合并

C++
int d.merge(int a, int b)

连接边 aa 和 bb,并返回 aa 和 bb 所在联通块代表节点的编号。

限制:

  • 0≤a,b<n0\leq a,b<n。

时空复杂度:均摊 O(α(n))\mathcal O(\alpha(n))。

查询两个点是否在同一联通块

C++
bool d.same(int a, int b)

返回 aa 和 bb 是否在同一联通块。

限制:

  • 0≤a,b<n0\leq a,b<n。

时空复杂度:均摊 O(α(n))\mathcal O(\alpha(n))。

查询联通块代表节点

C++
int d.leader(int a)

返回 aa 所在联通块的代表节点。

限制:

  • 0≤a<n0\leq a<n。

时空复杂度:均摊 O(α(n))\mathcal O(\alpha(n))。

查询联通块大小

C++
int d.size(int a)

返回 aa 所在联通块的大小。

限制:

  • 0≤a<n0\leq a<n。

时空复杂度:均摊 O(α(n))\mathcal O(\alpha(n))。

所有联通块

C++
vector<vector<int>> d.groups()

返回所有的联通块。具体地,返回联通块所组成的 vector,每一个 vector 内表示同一个联通块的所有点。

限制:

  • 无。

时空复杂度:O(n)\mathcal O(n)。

参考实现

practice2_a

网络最大流(maxflow)

定义

C++
mf_graph<Cap> graph(int n)

创建一个 nn 个点的空图。Cap 是容量的类型。

限制:

  • 0≤n≤1080\leq n\leq 10^8。
  • Cap 只能是 int 或 ll。

时空复杂度:O(n)\mathcal O(n)。

加边

C++
int graph.add_edge(int from, int to, Cap cap);

加一条从 from\text{from} 到 to\text{to},容量为 cap\text{cap} 的边。返回目前边数。

限制:

  • 0≤from,to<n0\leq\text{from},\text{to}<n;
  • cap≥0\text{cap}\geq0。

时空复杂度:均摊 O(1)\mathcal O(1)。

最大流

C++
Cap graph.flow(int s, int t);
Cap graph.flow(int s, int t, Cap flow_limit);

跑从 ss 到 tt 的网络流(限制流量为 flow_limit\text{flow\_limit})。

可以多次调用。

限制:

  • s≠ts\neq t;
  • 答案可以用 Cap 存下。

时空复杂度:(下令 mm 为边数,下同)

  • 如果每条边容量都为 11,O(min⁡(n23m,m32))\mathcal O(\min(n^{\frac23}m,m^{\frac32}));
  • 否则,O(n2m)\mathcal O(n^2m)。

构造最小割的方案

C++
vector<bool> graph.min_cut(int s)

若在残量网络上,ss 和 tt 可达,那么返回值 aa 中,ata_t 为真,否则为假。这对应一种最小割的方案。

限制:

  • 0≤s<n0\leq s<n。

时空复杂度:O(n+m)\mathcal O(n+m)。

遍历边

C++
mf_graph<Cap>::edge graph.get_edge(int i);
vector<mf_graph<Cap>::edge> graph.edges();

其中,mf_graph<Cap>::edge 的定义如下:

C++
struct mf_graph<Cap>::edge {
    int from, to;
    Cap cap, flow;
};

返回现在边的状态,和 add_edge 加入边的顺序相同。

限制:

  • 对于第一种实现方式,0≤i<m0\leq i<m。

时空复杂度:

  • 在第一种实现方式中,O(1)\mathcal O(1);
  • 在第二种实现方式中,O(m)\mathcal O(m)。

改变边的容量

C++
void graph.change_edge(int i, Cap new_cap, Cap new_flow);

将第 ii 条边的容量改为 new_cap\text{new\_cap},流量改为 new_flow\text{new\_flow}。

限制:

  • new_cap≥new_flow≥0\text{new\_cap}\geq\text{new\_flow}\geq0。

时空复杂度:O(n+m)\mathcal O(n+m)。

参考实现

practice2_d

最小费用最大流(mincostflow)

定义

C++
mcf_graph<Cap, Cost> graph(int n)

创建一个 nn 个点的空图。Cap 是容量的类型,Cost 是费用的类型。

限制:

  • 0≤n≤1080\leq n\leq 10^8。
  • Cap 和 Cost 只能是 int 或 ll。

时空复杂度:O(n)\mathcal O(n)。

加边

C++
int graph.add_edge(int from, int to, Cap cap, Cost cost)

加一条从 from\text{from} 到 to\text{to},容量为 cap\text{cap},费用为 cost\text{cost} 的边。返回目前边数。

限制:

  • 0≤from,to<n0\leq\text{from},\text{to}<n;
  • cap,cost≥0\text{cap},\text{cost}\geq0。

时空复杂度:均摊 O(1)\mathcal O(1)。

最小费用最大流

C++
pair<Cap, Cost> graph.flow(int s, int t)
pair<Cap, Cost> graph.flow(int s, int t, Cap flow_limit)

跑从 ss 到 tt 的最小费用最大流(限制流量为 flow_limit\text{flow\_limit}),返回流量与费用。

C++
vector<pair<Cap, Cost>> graph.slope(int s, int t)
vector<pair<Cap, Cost>> graph.slope(int s, int t, Cap flow_limit)

函数 g(x)g(x) 是流量为 xx 时的最小费用。它是线性分段函数。这个函数返回 gg 的所有整数值处的元素,且满足:

  • 第一个元素是 (0,0)(0,0);
  • 所有元素的第一项构成的序列递增,第二项也是如此;
  • 没有三个转折点共线;
  • 在第一种实现方式下,最后一个元素是 (x,g(x))(x,g(x)),其中 xx 是 s−ts-t 的最大流;
  • 在第一种实现方式下,最后一个元素是 (y,g(y))(y,g(y)),其中 xx 是 s−ts-t 的最大流,y=min⁡(x,flow_limit)y=\min(x,\text{flow\_limit})。

这两个函数不能多次调用。

限制:

  • s≠ts\neq t;
  • 流量可以用 Cap 存下;
  • 费用可以用 Cost 存下;
  • 如果 Cost 是 int,xx 是最大的容量,那么 0≤n⋅x≤2×109+10000\leq n\cdot x\leq2\times10^9+1000;
  • 如果 Cost 是 ll,xx 是最大的容量,那么 0≤n⋅x≤8×1018+10000\leq n\cdot x\leq8\times10^{18}+1000。

时空复杂度:O(F(n+m)log⁡(n+m))\mathcal O(F(n+m)\log(n+m)),其中 FF 为最大流。

遍历边

C++
mcf_graph<Cap, Cost>::edge graph.get_edge(int i)
vector<mcf_graph<Cap, Cost>::edge> graph.edges()

其中,mcf_graph<Cap, Cost>::edge 的定义如下:

C++
struct mcf_graph<Cap, Cost>::edge {
    int from, to;
    Cap cap, flow;
    Cost cost;
};

返回现在边的状态,和 add_edge 加入边的顺序相同。

限制:

  • 对于第一种实现方式,0≤i<m0\leq i<m。

时空复杂度:

  • 在第一种实现方式中,O(1)\mathcal O(1);
  • 在第二种实现方式中,O(m)\mathcal O(m)。

参考实现

practice2_e

强联通分量(scc)

定义

C++
scc_graph graph(int n)

创建一个 nn 个点的空图。

限制:

  • 0≤n≤1080\leq n\leq 10^8。

时空复杂度:O(n)\mathcal O(n)。

加边

C++
void graph.add_edge(int from, int to)

加一条从 from\text{from} 到 to\text{to} 的边。

限制:

  • 0≤from,to<n0\leq\text{from},\text{to}<n。

时空复杂度:均摊 O(1)\mathcal O(1)。

求强联通分量

C++
vector<vector<int>> graph.scc()

返回一个 vector 的列表,满足:

  • 0∼n−10\sim n-1 在所有 vector 中出现且仅出现一次;
  • 每个 vector 是强联通分量,其间的顺序不确定;
  • 所有强联通分量按拓扑序排序,即:若 u,vu,v 不在同一强联通分量,且存在一条从 uu 到 vv 的边,那么 uu 所在的强联通分量在 vv 所在的强联通分量之前出现。

限制:

  • 无。

时空复杂度:O(n+m)\mathcal O(n+m)。

参考实现

practice2_g

2-SAT(twosat)

定义

C++
two_sat ts(int n)

定义一个有 nn 个变量的空 2-SAT 规则。

限制:

  • 0≤n≤1080\leq n\leq10^8。

时空复杂度:O(n)\mathcal O(n)。

增加假设

C++
void ts.add_clause(int i, bool f, int j, bool g)

增加一个假设「xi=fx_i=f 或 xj=gx_j=g」。

限制:

  • 0≤i,j<n0\leq i,j<n。

时空复杂度:均摊 O(1)\mathcal O(1)。

检查是否有解

C++
bool ts.satisfiable()

如果有解,返回 true,否则返回 false。可以多次调用。

限制:

  • 无。

时空复杂度:O(n+m)\mathcal O(n+m),mm 为限制数。

返回解

C++
vector<bool> ts.answer()

如果上次 satisfiable 函数返回 true,则返回 nn 个 bool 表示答案。否则,返回 nn 个未定义的内容。

限制:

  • 无。

时空复杂度:O(n)\mathcal O(n)。

参考实现

practice2_h

原文