跳到正文

休闲·娱乐

ACM 模板

pan_g约 45 分钟
原文
文章信息与版本

文章信息

发布
2024年3月31日
收录
2025年12月12日
更新
2026年11月22日
篇幅
约 45 分钟
获赞
1

版本

  1. v1当前

ACM 模板

I/O

C++
// 在太阳西斜的这个世界里,置身天上之森。等这场战争结束之后,不归之人与望眼欲穿的众人, 人人本着正义之名,长存不灭的过去、逐渐消逝的未来。我回来了,纵使日薄西山,即便看不到未来,此时此刻的光辉,盼君勿忘。————世界上最幸福的女孩

#include <bits/stdc++.h>
using namespace std;
typedef __int128_t bll;
typedef unsigned long long ull;
typedef long long ll;
typedef double lb;
const ll GenShin = 114514191981054188;
const ll mod = 998244353;
const int homo = 1145141919;
const int INF = 0x3f3f3f3f;
inline ll read(){
	ll res = 0, f = 1;
	char c = getchar();
	for(;!isdigit(c); c = getchar()) if(c == '-') f *= -1;
	for(;isdigit(c); c = getchar()) res = (res << 3) + (res << 1) + (c ^ 48);
	return res * f;
}
inline void write(ll x){
	if(x < 0) putchar('-'), x *= -1;
	if(x > 9) write(x/10);
	putchar(x%10+48); return ;
}
signed main(){
	return 0;
}

Mod Int

C++
typedef long long ll;
template <class T> constexpr T Pow(T a, ll b){
    T res = 1;
    for( ; b ; b >>= 1, a *= a) if(b & 1) res *= a;
    return res;
}
template <int P> struct mint{
    int x;
    constexpr mint() : x{} {}
    constexpr mint(ll x) : x(Set(x % P)) {}
    constexpr int Set(int x) const {
        if(x < 0) x += P;
        if(x >= P) x -= P;
        return x;
    }
    constexpr int val() const { return x; }
    explicit constexpr operator int() const { return x; }
    constexpr mint operator-() const { mint res; res.x = Set(P - x); return res; }
    constexpr mint inv() const { assert(x != 0); return Pow(*this, P - 2); }
    constexpr mint operator*=(mint a) { x = 1ll * x * a.x % P; return *this; }
    constexpr mint operator+=(mint a) { x = Set(x + a.x); return *this; }
    constexpr mint operator-=(mint a) { x = Set(x - a.x); return *this; }
    constexpr mint operator/=(mint a) { return *this *= a.inv(); }
    constexpr friend mint operator*(mint a, mint b) { mint res = a; res *= b; return res; }
    constexpr friend mint operator+(mint a, mint b) { mint res = a; res += b; return res; }
    constexpr friend mint operator-(mint a, mint b) { mint res = a; res -= b; return res; }
    constexpr friend mint operator/(mint a, mint b) { mint res = a; res /= b; return res; }
    constexpr friend bool operator==(mint a, mint b) { return a.val() == b.val(); }
    constexpr friend bool operator!=(mint a, mint b) { return a.val() != b.val(); }
};
// use: mint<mod> Name;

Rational Number

C++
constexpr i64 gcd(i64 a, i64 b){ return b == 0 ? a : gcd(b, a % b); }
constexpr i64 Abs(i64 x){ return (x < 0 ? -x : x); }
struct Q{
    i64 p, q;
    constexpr Q() : p(0), q(1) {}
    constexpr Q(i64 x) : p(x), q(1) {}
    constexpr Q(i64 x, i64 y) : p(x), q(y){ Set(); }
    constexpr void Set(){
        assert(q != 0);
        if(q < 0) p = -p, q = -q;
        i64 g = gcd(Abs(p), Abs(q));
        p /= g, q /= g;
        return ;
    }
    constexpr Q operator*=(Q a) { p *= a.p, q *= a.q, Set(); return *this; }
    constexpr Q operator/=(Q a) { p *= a.q, q *= a.p, Set(); return *this; }
    constexpr Q operator+=(Q a) { p = p * a.q + q * a.p, q *= a.q, Set(); return *this; }
    constexpr Q operator-=(Q a) { p = p * a.q - q * a.p, q *= a.q, Set(); return *this; }
    constexpr Q friend operator+(Q a, Q b){ Q res = a; res += b; return res; }
    constexpr Q friend operator-(Q a, Q b){ Q res = a; res -= b; return res; }
    constexpr Q friend operator*(Q a, Q b){ Q res = a; res *= b; return res; }
    constexpr Q friend operator/(Q a, Q b){ Q res = a; res /= b; return res; }
    constexpr bool friend operator<(Q a, Q b){ return a.p * b.q < b.p * a.q; }
    constexpr bool friend operator>(Q a, Q b){ return a.p * b.q > b.p * a.q; }
    constexpr bool friend operator==(Q a, Q b){ return a.p * b.q == b.p * a.q; }
    constexpr bool friend operator!=(Q a, Q b){ return a.p * b.q != b.p * a.q; }
    friend std::ostream &operator<<(std::ostream &os, Q &x){
        os << x.p << '/' << x.q;
        return os;
    }
};

Combinom

C++
template<int V, int P> constexpr mint<P> CInv = mint<P>(V).inv();
constexpr int P = 998244353;
using Z = mint<P>;
struct Comb{
    int n ;
    vector<Z> Fac, Fav, Inv;
    Comb() : n{0}, Fac{1}, Fav{1}, Inv{0} {}
    Comb(int n) : Comb(){ init(n); }
    void init(int m){
        if(m <= n) return ;
        Fac.resize(m + 1), Fav.resize(m + 1), Inv.resize(m + 1);
        for(int i = n + 1;i <= m;i++) Fac[i] = Fac[i - 1] * i;
        Fav[m] = Fac[m].inv();
        for(int i = m;i > n;i--){
            Fav[i - 1] = Fav[i] * i;
            Inv[i] = Fav[i] * Fac[i - 1];
        }
        n = m;
    }
    Z fac(int m) { if(m > n) init(m << 1); return Fac[m]; }
    Z fav(int m) { if(m > n) init(m << 1); return Fav[m]; }
    Z inv(int m) { if(m > n) init(m << 1); return Inv[m]; }
    Z C(int n, int m){
        if(n < m || m < 0) return 0;
        return fac(n) * fav(m) * fav(n - m);
    }
    Z A(int n, int m){
        if(n < m || m < 0) return 0;
        return fac(n) * fav(n - m);
    }
}comb;

Primes

C++
vector<int> sieve(int n){
    vector<bool> np(n + 1, false);
    vector<int> prime;
    for(int i = 2;i <= n;i++){
        if(!np[i]) prime.emplace_back(i);
        for(auto p : prime){
            if(1ll * i * p > n) break;
            np[i * p] = true;
            if(i % p == 0) break;
        }
    }
    return prime;
} // Eular Sieve

SegmentTree without LazyTag

C++
template<class Info> struct SegmentTree {
	public:
		SegmentTree() : SegmentTree(0) {}
		explicit SegmentTree(int _n) : SegmentTree(vector<Info>(_n)) {}
		explicit SegmentTree(const vector<Info> &V) : n((int)V.size()){
			if(n == 0) return ;
			a.assign(n << 2, Info{});
			build(1, 1, n, V);
		}
		void modify(int p, Info x){
			assert(1 <= p && p <= n);
			Update(1, 1, n, p, x);
		}
		Info query(int l, int r){
			if(l > r) return Info{};
			assert(1 <= l && l <= r && r <= n);
			return Ask(1, 1, n, l, r);
		}
	private:
		int n; vector<Info> a;
		# define ls(p) ((p) << 1)
		# define rs(p) ((p) << 1 | 1)
		void build(int p, int l, int r, const vector<Info> &V){
			if(l == r){ a[p] = V[l - 1]; return ; }
			int mid = l + r >> 1;
			build(ls(p), l, mid, V);
			build(rs(p), mid + 1, r, V);
			a[p] = a[ls(p)] + a[rs(p)];
			return ;
		}
		void Update(int p, int l, int r, int aim, Info x){
			if(l == r) { a[p] = x; return ; }
			int mid = l + r >> 1;
			if(aim <= mid) Update(ls(p), l, mid, aim, x);
			if(aim > mid) Update(rs(p), mid + 1, r, aim, x);
			a[p] = a[ls(p)] + a[rs(p)];
		}
		Info Ask(int p, int l, int r, int ql, int qr){
			if(ql <= l && r <= qr) return a[p];
			int mid = l + r >> 1;
            Info res{};
			if(ql <= mid) res = res + Ask(ls(p), l, mid, ql, qr);
			if(qr > mid) res = res + Ask(rs(p), mid + 1, r, ql, qr);
			return res;
		}
};
struct Info {
	// values
	Info friend operator+(Info a, Info b){
		// pull up
	}
};
// use: SegmentTree<Info> Name;

SegmentTree with LazyTag

C++
template<class Data, Data (*op)(Data, Data), Data (*e)(),
         class Tag, Data (*Dapply)(Data, Tag), Tag (*Tapply)(Tag, Tag), Tag (*tag)()>
struct LazySeg {
    public:
        LazySeg() : LazySeg(0) { }
        explicit LazySeg(int _n) : LazySeg(vector<Data>(_n, e())) { }
        explicit LazySeg(const vector<Data> &V) : n((int)V.size()){
            if(n == 0) return ; Root = nullptr;
            auto build = [&](auto &&self, Node *&p, int l, int r) -> void {
                p = new Node(l, r); int m = l + r >> 1;
                if(l == r){ p->x = V[l - 1]; return ; }
                self(self, p->ls, l, m), self(self, p->rs, m + 1, r);
                p->x = op(p->ls->x, p->rs->x); return ;
            };
            build(build, Root, 1, n);
        }
        inline void modify(int p, const Data &x){
            assert(1 <= p && p <= n);
            modify(Root, p, x);
        }
        inline void update(int l, int r, const Tag &t){
            assert(1 <= l && l <= r && r <= n);
            rangeApply(Root, l, r, t);
        }
        inline Data query(int l, int r){
            assert(1 <= l && l <= r && r <= n);
            return rangeQuery(Root, l, r);
        }
    private:
        struct Node{
            int l, r; Node *ls, *rs; Data x; Tag t;
            Node(int l, int r) : l(l), r(r), ls(nullptr), rs(nullptr), x(e()), t(tag()) { }
        };
        Node *Root; int n;
        inline void apply(Node *&p, const Tag &t){ p->x = Dapply(p->x, t), p->t = Tapply(p->t, t); }
        inline void push(Node *&p){ apply(p->ls, p->t), apply(p->rs, p->t), p->t = tag(); }
        inline void pull(Node *&p){ p->x = op(p->ls->x, p->rs->x); }
        inline void modify(Node *&p, int aim, const Data &x){
            if(p->l == p->r){ p->x = x; return ; }
            push(p); int m = p->l + p->r >> 1;
            if(aim <= m) modify(p->ls, aim, x);
            else modify(p->rs, aim, x);
            pull(p);
        }
        inline void rangeApply(Node *&p, int l, int r, const Tag &t){
            if(l <= p->l && p->r <= r){ apply(p, t); return ; }
            push(p); int m = p->l + p->r >> 1;
            if(l <= m) rangeApply(p->ls, l, r, t);
            if(r > m) rangeApply(p->rs, l, r, t);
            pull(p);
        }
        inline Data rangeQuery(Node *&p, int l, int r){
            if(l <= p->l && p->r <= r) return p->x;
            push(p); int m = p->l + p->r >> 1; Data res = e();
            if(l <= m) res = op(res, rangeQuery(p->ls, l, r));
            if(r > m) res = op(res, rangeQuery(p->rs, l, r));
            return res;
        }
};
struct Data{
    // Datas
};
struct Tag{
    // Lazy Tags
};
inline Data pull(Data x, Data y){
    // to get father
}
inline Data e(){
    // enum of Data
}
inline Data DataApply(Data x, Tag t){
    // Data merge Tag
}
inline Tag TagApply(Tag x, Tag y){
    // Tag merge Tag
}
inline Tag tag(){
    // enum of Tag
}
// use: LazySeg<Data, pull, e, Tag, DataApply, TagApply, tag> name(Data array(vector))

Fenwick

C++
template<class T> struct Fenwick{
	int n; vector<T> a;
	Fenwick() : Fenwick(0) { }
	explicit Fenwick(int _n) : n(_n){ a.assign(n, T{}); };
	inline void add(int p, const T &x){
		assert(1 <= p && p <= n);
		for(p;p <= n;p += p & -p) a[p - 1] = a[p - 1] + x;
	}
	inline T sum(int l, int r){
		assert(1 <= l && l <= r && r <= n);
		return query(r) - query(l - 1);
	}
	inline T query(int p){
		T res{};
		for(p;p > 0;p -= p & -p) res = res + a[p - 1];
		return res;
	}
};
// use: Fenwick<key of values> name;

Spare Table

C++
template<class T> struct Spare_Table{
    int n;
    vector<vector<T> > st;
    Spare_Table(const vector<T> &V) : n(V.size()), st(__lg(n) + 1){
        st[0] = V;
        for(int i = 1;i <= __lg(n);i++){
            for(int j = 0;j + (1 << i) - 1 < n;j++){
                st[i].emplace_back(st[i - 1][j] + st[i - 1][j + (1 << i - 1)]);
            }
        }
    }
    T query(int l, int r){
        int f = __lg(r - l + 1);
        return st[f][l] + st[f][r - (1 << f) + 1];
    }
};
struct Info{
    // values
    Info friend operator+(Info a, Info b){
        //merge
    }
};
// use : Spare_Table<Info> name(Info array);

Max Flow

C++
struct Net{
    int n;
    struct Edge{
        int to; ll cap;
        Edge(int to, ll c) : to(to), cap(c) { }
    };
    vector<Edge> E;
    vector<vector<int> > G;
    Net(int x) : n(x), G(x) {}
    vector<int> dep, cur;
    void add(int u, int v, ll c){
        assert(0 <= u && u < n);
        assert(0 <= v && v < n);
        assert(0 <= c);
        G[u].emplace_back(E.size());
        E.emplace_back(v, c);
        G[v].emplace_back(E.size());
        E.emplace_back(u, 0);
    }
    inline bool BFS(int s, int t){
        dep.assign(n, -1);
        queue<int> q;
        dep[s] = 0; q.emplace(s);
        while(!q.empty()){
            int u = q.front(); q.pop();
            for(auto i : G[u]){
                int v = E[i].to;
                ll c = E[i].cap;
                if(!c || dep[v] != -1) continue;
                dep[v] = dep[u] + 1;
                if(v == t) return true;
                q.emplace(v);
            }
        }
        return false;
    }
    ll DFS(int u, int t, ll f){
        if(u == t) return f;
        ll r = f;
        for(int &i = cur[u];i < (int)G[u].size();i++){
            int j = G[u][i];
            int v = E[j].to;
            ll c = E[j].cap;
            if(!c || dep[v] != dep[u] + 1) continue;
            ll k = DFS(v, t, min(r, c));
            E[j].cap -= k, E[j ^ 1].cap += k, r -= k;
            if(r == 0) break;
        }
        return f - r;
    }
    ll MF(int S, int T){
        assert(0 <= S && S < n);
        assert(0 <= T && T < n);
        assert(S != T);
        ll maxflow = 0;
        while(BFS(S, T)){
            cur.assign(n, 0);
            maxflow += DFS(S, T, GenShin);
        }
        return maxflow;
    }
    vector<bool> MC(int S){
        vector<bool> vis(n, false);
        queue<int> q; q.push(S);
        while(!q.empty()){
            int u = q.front(); q.pop(); vis[u] = true;
            for(auto i : G[u]){
                int v = E[i].to;
                ll c = E[i].cap;
                if(!c || vis[v]) continue;
                vis[v] = true, q.push(v);
            }
        }
        return vis;
    }
};

Min Cost Max Flow

C++
struct Net{
    struct Edge{
        int to;
        ll cap, cost;
        Edge(int to, ll c, ll w) : to(to), cap(c), cost(w) {}
    };
    vector<Edge> E;
    vector<vector<int> > G;
    int n;
    Net(int x) : G(x), n(x) { }
    void add(int u, int v, ll cap, ll cost){
        assert(0 <= u < n && 0 <= v < n);
        G[u].emplace_back(E.size());
        E.emplace_back(v, cap, cost);
        G[v].emplace_back(E.size());
        E.emplace_back(u, 0, -cost);
    }
    vector<ll> dis;
    vector<int> cur;
    vector<bool> vis;
    bool spfa(int s, int t){
        queue<int> q;
        dis.assign(n, GenShin);
        vis.assign(n, false);
        cur.assign(n, 0);
        q.emplace(s), dis[s] = 0, vis[s] = true;
        while(!q.empty()){
            int u = q.front(); q.pop();
            vis[u] = false;
            for(auto i : G[u]){
                auto [v, c, w] = E[i];
                if(!c || dis[v] <= dis[u] + w) continue;
                dis[v] = dis[u] + w;
                if(!vis[v]) q.emplace(v), vis[v] = true;
            }
        }
        return dis[t] != GenShin;
    }
    pair<ll, ll> MaxFlow_and_MinCost(int s, int t){
        assert(0 <= s < n && 0 <= t < n);
        ll Mc = 0;
        auto DFS = [&](auto &&self, int u, ll flow) -> ll {
            if(u == t) return flow;
            vis[u] = true;
            ll res = 0, k;
            for(int &i = cur[u];i < (int)G[u].size();i++){
                int j = G[u][i];
                auto [v, c, w] = E[j];
                if(vis[v] || !c || dis[v] != dis[u] + w) continue;
                k = self(self, v, min(c, flow - res));
                res += k, E[j].cap -= k, E[j ^ 1].cap += k;
                Mc += w * k;
            }
            vis[u] = false;
            return res;
        };
        ll Mf = 0;
        while(spfa(s, t)) Mf += DFS(DFS, s, GenShin);
        return {Mf, Mc};
    }
};

Union Find

C++
struct UF{
	int n; vector<int> fa, siz;
	UF() { }
	UF(int _n) : n(_n){
		fa.resize(_n);
		iota(fa.begin(), fa.end(), 0);
		siz.assign(n , 1);
	}
	inline int find(int x){
		assert(0 <= x && x < n);
		while(x != fa[x]) x = fa[x] = fa[fa[x]];
		return x;
	}
	inline bool same(int x, int y){ return find(x) == find(y); }
	inline bool mer(int x, int y){
		x = find(x), y = find(y);
		if(x == y) return true;
		siz[x] += siz[y], fa[y] = x;
		return false;
	}
	inline int size(int x){ return siz[find(x)]; }
};
// use: UF name(size + 1);

LCA

C++
struct LCA{
    vector<vector<int> > G; int n;
    vector<int> son, top, fa, siz, dep;
    LCA() : n(0) {}
    LCA(vector<vector<int> > _G) : G(_G), n((int)_G.size() - 1){
        son.resize(n + 1), top.resize(n + 1), fa.resize(n + 1), siz.resize(n + 1), dep.resize(n + 1);
        fill(son.begin(), son.end(), 0), fill(siz.begin(), siz.end(), -1);
        init();
    }
    inline void init(){
        auto dfs = [&](auto self, int u, int Fa)->void {
            siz[u] = 1; fa[u] = Fa; dep[u] = dep[Fa] + 1;
            for(auto v : G[u]){
                if(v == Fa) continue;
                self(self, v, u);
                siz[u] += siz[v];
                if(siz[v] > siz[son[u]]) son[u] = v;
            }
            return ;
        };
        dfs(dfs, 1, 0);
        
        auto DFS = [&](auto self, int u, int t)->void {
            top[u] = t;
            if(son[u]) self(self, son[u], t);
            for(auto v : G[u]){
                if(v == fa[u] || v == son[u]) continue;
                self(self, v, v);
            }
            return ;
        };
        DFS(DFS, 1, 1);
    }
    inline int lca(int x, int y){
        while(top[x] != top[y]){
            if(dep[top[x]] <= dep[top[y]]) swap(x, y);
            x = fa[top[x]];
        }
        return dep[x] < dep[y] ? x : y;
    }
};

DSU

C++
struct DSU{
	int n; vector<int> fa, siz, par;
	vector<vector<int> > G;
	DSU() { }
	DSU(int _n) : n(_n){
		fa.resize(_n + 1), siz.resize(_n + 1), G.resize(_n + 1), par.resize(_n + 1);
		for(int i = 1;i <= _n;i++) fa[i] = i, siz[i] = 1, par[i] = -1;
	}
	int find(int x){ return fa[x] = (x == fa[x] ? x : find(fa[x])); }
	void mer(int x, int y){
		x = find(x), y = find(y);
		if(x == y) return ;
		fa[x] = y, siz[y] += siz[x];
	}
	void dsu(int x, int y){
		if(siz[find(x)] > siz[find(y)]) swap(x, y);
		auto dfs = [&](auto self, int u, int Fa) -> void {
			par[u] = Fa;
			for(auto v : G[u]){
				if(v == Fa) continue;
				self(self, v, u);
			}
		};
		dfs(dfs, x, y); mer(x, y);
		G[x].push_back(y), G[y].push_back(x);
	}
};

Graph

C++
template<class T, T (*e)()> struct Graph{
    public:
        Graph() : Graph(0) { }
        Graph(int _n) : n(_n) { G.resize(_n); }
        void add(int u, int v, T w){
            assert(0 <= u && u < n);
            assert(0 <= v && v < n);
            G[u].push_back(make_pair(v, w));
        }
        vector<vector<T> > Floyd(){
            vector<vector<T> > dis(n, vector<T>(n, e()));
            for(int i = 0;i < n;i++) dis[i][i] = 0;
            for(int i = 0;i < n;i++){
                for(auto [j, w]: G[i]){
                    dis[i][j] = min(dis[i][j], w);
                }
            }
            for(int k = 0;k < n;k++){
                for(int i = 0;i < n;i++){
                    for(int j = 0;j < n;j++){
                        dis[i][j] = min(dis[i][k] + dis[k][j], dis[i][j]);
                    }
                }
            }
            return dis;
        }
        vector<T> Dijkstra(int s){
            assert(0 <= s && s < n);
            priority_queue<pair<T, int> > q;
            vector<bool> vis(n , false);
            vector<T> dis(n, e());
            dis[s] = 0, q.emplace(0, s);
            while(!q.empty()){
                int u = q.top().second; q.pop();
                if(vis[u]) continue; vis[u] = true;
                for(auto [v, w] : G[u]){
                    if(dis[v] <= dis[u] + w) continue;
                    dis[v] = dis[u] + w;
                    if(!vis[v]) q.emplace(-dis[v], v);
                }
            }
            return dis;
        }
        vector<T> spfa(int s){
            assert(0 <= s && s < n);
            queue<int> q;
            vector<T> dis(n, e());
            vector<bool> vis(n , false);
            dis[s] = 0, q.push(s);
            while(!q.empty()){
                int u = q.front(); q.pop(); vis[u] = false;
                for(auto [v, w] : G[u]){
                    if(dis[v] <= dis[u] + w) continue;
                    dis[v] = dis[u] + w;
                    if(!vis[v]) vis[v] = true, q.push(v);
                }
            }
            return dis;
        }
        bool Negative_Ring(){
            queue<int> q;
            vector<T> dis(n, e());
            vector<bool> vis(n , false);
            vector<int> cnt(n, 0);
            dis[0] = 0; q.push(0);
            while(!q.empty()){
                int u = q.front(); q.pop(); vis[u] = false;
                for(auto [v, w] : G[u]){
                    if(dis[v] <= dis[u] + w) continue;
                    dis[v] = dis[u] + w; cnt[v] = cnt[u] + 1;
                    if(cnt[v] >= n) return true;
                    if(!vis[v]) vis[v] = true, q.push(v);
                }
            }
            return false;
        }
        vector<int> TopoSort(){
            vector<int> deg(n, 0); vector<int> q; int cur = 0;
            for(int u = 0;u < n;u++) for(auto [v, w] : G[u]) deg[v] ++;
            for(int i = 0;i < n;i++) if(deg[i] == 0) q.emplace_back(i);
            while(cur != n){
                int u = q[cur ++];
                for(auto [v, w] : G[u]){
                    deg[v] --;
                    if(deg[v] == 0) q.emplace_back(v);
                }
            }
            return q;
        }
    private:
        int n; vector<vector<pair<int, T> > > G;
};
int e(){ return INF; } // update maximum
// use: Graph<Edge's type, e> name;

Minimum Spawning Tree

C++
template<class T> struct Graph{
    public:
        Graph() : Graph(0) { }
        Graph(int _n) : n(_n) { }
        void add(int u, int v, T w){
            assert(0 <= u && u < n);
            assert(0 <= v && v < n);
            E.push_back({u, v, w});
        }
        T MinSpawnTree(){
            UF uf(n); int tot = 0;
            T res = 0;
            sort(E.begin(), E.end(), [&](auto x, auto y){ return x.w < y.w; });
            for(auto e : E){
                if(tot == n - 1) return res;
                int u = e.from, v = e.to;
                if(uf.find(u) == uf.find(v)) continue;
                uf.mer(u, v); tot ++; res += e.w;
            }
            return tot != n - 1 ? -1 : res;
        }
    private:
        int n;
        struct Edge{ int from, to; T w; };
        vector<Edge> E;
};
// use: Graph<Edge's type> name;

Prefix array

C++
vector<int> Prefix(string s){
	int n = s.size(), j = 0;
	vector<int> fail(n, 0);
	for(int i = 1;i < n;i++){
		while(j > 0 && s[i] != s[j]) j = fail[j - 1];
		if(s[i] == s[j]) j++;
		fail[i] = j;
	}
	return fail;
}

Palindrome

C++
vector<int> Palindrome(string s){
	int n = s.size();
	vector<int> R(n, 0);
	for(int p = 1, mx = 1, id = 1;p < n;p++){
		int k = p >= mx ? 1 : min(mx - p, R[(id << 1) - p]);
		while(s[p - k] == s[p + k]) k ++;
		R[p] = k;
		if(p + k > mx) mx = p + k, id = p;
	}
	return R;
}

int Manacher(string s){
	string p = "!@";
	for(int i = 0;i < s.size();i++){
		p += s[i];
		p += '@';
	}
	auto R = Palindrome(p);
	return *max_element(R.begin() + 1, R.end()) - 1;
}
原文