跳到正文

题解

题解:P5827 点双连通图计数

catrix约 18 分钟
原文
文章信息与版本

文章信息

发布
2025年8月17日
收录
2025年12月12日
更新
2026年11月18日
篇幅
约 18 分钟
获赞
4
题目
P5827

版本

  1. v1当前

思路

别问为什么没有题目大意,问就是题目意思很明确了,说人话就是演都不想演了。前置 Cheese: 图的强连通性以及强连通分量(能来写黑题的应该都会把),首先,这问题一眼组合数学 + 生成函数没有问题,分步骤来思考就是:

  1. 首先,一个 nn 顶点的代表好无向连通图的数量是 2(n2)2^\binom{n}{2}, 因为每一个顶点之间可以选择是否连接一条边。
  2. 其次,连通图的数量,根据容斥原理,一个无向图中连通图的数量,就是所有子图的数量减去不连通的子图数量,这个可以通过指数函数 + 生成函数的方式表示,我们设 G(x)G(x) 为所有无向图的 EGF, C(x)C(x) 是连通图的 EGF, 可以推导公式如下:G(x)=exp⁡(C(x))  ⟹  C(x)=ln⁡(G(x))G(x) = \exp(C(x)) \implies C(x) = \ln(G(x))。
  3. 点双连通图计数,这个是最复杂的,我们可以利用概念,也就是对于一个连通图,可以分解为点双连通分量,而且点双连通分量之间的连接就是割点,我们也称做块 - 割点树,也就是 block-cut-tree , 其中块是点双连通分量,而割点是连接这些分量的点。

然而知道这些还不够,为了计算点双连通图,我们需要进行生成函数的推导: 首先,我们假设 D(x)D(x) 是点双连通图的 EGF。C(x)C(x) 是连通图的 EGF, G(x)G(x) 是所有无向图的 EGF。
由于连通图可以看作是有一个点双连通分量的“拓展”,也就是说,一个连通图可以看作是一个点双连通分量,其中每一个顶点都被替换成新的连通图(可能是一个单点),因此我们可以建立关系如:

C(x)=x⋅exp⁡(∑k≥1d(k)(C(x))k!)C(x) = x \cdot \exp(\sum_{k \geq 1} \frac{d^{(k)}(C(x))}{k!})

其中 D(k)(x)D^{(k)}(x) 表示为 D(x)D(x) 的第 kk 阶导数,表示点双连通分量中顶点被替换为其他连通图的方式。那么开始推导,有:

F(x)=exp⁡(∑k≥1D(k)(xF(x))k!F(x) = \exp(\sum_{k\geq 1}\frac{D^{(k)}(xF(x))}{k!}

取对数之后:

ln⁡(F(x))=∑k≥1D(k)(xF(x))k!\ln(F(x)) = \sum_{k \geq 1}\frac{D^{(k)}(xF(x))}{k!}

接下来,设 y=xF(x)y = xF(x) 则 x=yF(x)x = \frac{y}{F(x)}, 我们需要求:

ln⁡(F(x))=∑k≥1D(k)(y)k!\ln(F(x)) = \sum_{k \geq 1}\frac{D^{(k)}(y)}{k!}

利用拉格朗日反演公式,可以得到 D(y)D(y) 的表达式。接下来提取 D(x)D(x) 的系数:

dn=1n[xn−1](xC−1(x))nd_n = \frac{1}{n}[x^{n-1}](\frac{x}{C^{-1}(x)})^n

其中显然的 C−1(x)C^{-1}(x) 是 C(x)C(x) 的反函数。 然后经过推导之后,显然有

D(x)=x⋅exp⁡(ddxln⁡(D(x)))D(x) = x \cdot \exp(\frac{d}{dx} \ln(D(x)))

然后我们就可以利用对数导数和生成指数函数的方法得到 D(x)D(x) 的表达式,这里思路有一点乱,顺序没想好,可以自行调整一下。然后接下来,我们可以先预处理阶乘和逆阶乘,利用后续计算,然后通过对 G(x)G(x) 取对数得到 C(x)=ln⁡G(x)C(x) = \ln G(x)。 然后接下来对 C(x)C(x) 进行变换之后就可以得到 D(x)D(x), 然后利用拉格朗日反演公式从 D(x)D(x) 中提取 [xn]D(x)[x^n]D(x), 即 nn 个顶点点双连通图的数量。最后,我们只需要用 NTT 把对数,乘法,指数的操作整完之后题目就做出来。

Code:

C++
#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
const int MOD = 998244353;
const int G = 3;
const int MAX_DEGREE = 1 << 17;

namespace Math {
    int add(int a, int b) { return (a + b >= MOD) ? a + b - MOD : a + b; }
    int subtract(int a, int b) { return (a - b < 0) ? a - b + MOD : a - b; }
    int multiply(int a, int b) { return (LL)a * b % MOD; }
    
    int power(int base, LL exponent) {
        int result = 1;
        while (exponent) {
            if (exponent & 1) result = multiply(result, base);
            base = multiply(base, base);
            exponent >>= 1;
        }
        return result;
    }
    
    int inved(int x) { return power(x, MOD - 2); }
}

namespace Poly {
    using namespace Math;
    
    void tand(vector<int>& Poly, bool inved_tand) {
        size_t n = Poly.size();
        for (size_t i = 0, j = 0; i < n; i++) {
            if (i < j) swap(Poly[i], Poly[j]);
            size_t k = n >> 1;
            while (k && (k & j)) j ^= k, k >>= 1;
            j ^= k;
        }
        
        for (size_t segment = 1; segment < n; segment <<= 1) {
            int root = power(G, (MOD - 1) / (segment << 1));
            for (size_t j = 0; j < n; j += segment << 1) {
                int current = 1;
                for (size_t k = 0; k < segment; k++, current = multiply(current, root)) {
                    int x = Poly[j + k];
                    int y = multiply(Poly[segment + j + k], current);
                    Poly[j + k] = add(x, y);
                    Poly[segment + j + k] = subtract(x, y);
                }
            }
        }
        
        if (inved_tand) {
            reverse(Poly.begin() + 1, Poly.end());
            int inv_n = inved(n);
            for (size_t i = 0; i < n; i++) 
                Poly[i] = multiply(Poly[i], inv_n);
        }
    }
    
    void completx(const vector<int>& Input, vector<int>& output) {
        if (Input.size() == 1) {
            output.resize(1);
            output[0] = inved(Input[0]);
            return;
        }
        
        size_t half_size = (Input.size() + 1) / 2;
        vector<int> trhalf(Input.begin(), Input.begin() + half_size);
        vector<int> inhalf;
        completx(trhalf, inhalf);
        
        size_t poly_size = 1;
        while (poly_size < Input.size() * 2) poly_size <<= 1;
        
        vector<int> temp = Input;
        temp.resize(poly_size, 0);
        inhalf.resize(poly_size, 0);
        
        tand(temp, false);
        tand(inhalf, false);
        
        for (size_t i = 0; i < poly_size; i++)
            inhalf[i] = multiply(subtract(2, multiply(temp[i], inhalf[i])), inhalf[i]);
        
        tand(inhalf, true);
        inhalf.resize(Input.size());
        output = inhalf;
    }
    
    void comDer(const vector<int>& Input, vector<int>& output) {
        output.resize(Input.size());
        for (size_t i = 1; i < Input.size(); i++)
            output[i - 1] = multiply(Input[i], i);
        output.back() = 0;
    }
    
    void compinte(const vector<int>& Input, vector<int>& output) {
        output.resize(Input.size());
        output[0] = 0;
        for (size_t i = 1; i < Input.size(); i++)
            output[i] = multiply(Input[i - 1], inved(i));
    }
    
    void compute_ln(const vector<int>& Input, vector<int>& output) {
        vector<int> derdied, inved_Input;
        comDer(Input, derdied);
        completx(Input, inved_Input);
        
        size_t compl_size = 1;
        while (compl_size < Input.size() * 2) compl_size <<= 1;
        
        derdied.resize(compl_size, 0);
        inved_Input.resize(compl_size, 0);
        
        tand(derdied, false);
        tand(inved_Input, false);
        
        for (size_t i = 0; i < compl_size; i++)
            derdied[i] = multiply(derdied[i], inved_Input[i]);
        
        tand(derdied, true);
        compinte(derdied, output);
        output.resize(Input.size());
    }
    
    void nxtcomp(const vector<int>& Input, vector<int>& output) {
        if (Input.size() == 1) {
            output.resize(1, 1);
            return;
        }
        
        size_t half_size = (Input.size() + 1) / 2;
        vector<int> trhalf(Input.begin(), Input.begin() + half_size);
        vector<int> exphalf;
        nxtcomp(trhalf, exphalf);
        exphalf.resize(Input.size());
        
        vector<int> lihai;
        compute_ln(exphalf, lihai);
        
        size_t nxt_size = 1;
        while (nxt_size < Input.size() * 2) nxt_size <<= 1;
        
        lihai.resize(nxt_size, 0);
        exphalf.resize(nxt_size, 0);
        vector<int> temp = Input;
        temp.resize(nxt_size, 0);
        
        for (size_t i = 0; i < Input.size(); i++)
            lihai[i] = subtract(temp[i], lihai[i]);
        lihai[0] = add(lihai[0], 1);
        
        tand(exphalf, false);
        tand(lihai, false);
        
        for (size_t i = 0; i < nxt_size; i++)
            exphalf[i] = multiply(exphalf[i], lihai[i]);
        
        tand(exphalf, true);
        exphalf.resize(Input.size());
        output = exphalf;
    }
}

class Graphcnt {
private:
    vector<int> factot;
    vector<int> invfac;
    vector<int> contg;
    vector<int> log_contg;
    vector<int> lgder;
    
    void init() {
        factot.resize(MAX_DEGREE + 1);
        factot[0] = 1;
        for (int i = 1; i <= MAX_DEGREE; i++)
            factot[i] = Math::multiply(factot[i - 1], i);
        
        invfac.resize(MAX_DEGREE + 1);
        invfac[MAX_DEGREE] = Math::inved(factot[MAX_DEGREE]);
        for (int i = MAX_DEGREE; i >= 1; i--)
            invfac[i - 1] = Math::multiply(invfac[i], i);
    }
    
    void preget() {
        vector<int> all_graphs(MAX_DEGREE);
        for (int i = 0; i < MAX_DEGREE; i++) {
            LL edges = (LL)i * (i - 1) / 2;
            all_graphs[i] = Math::multiply(
                Math::power(2, edges % (MOD - 1)), 
                invfac[i]
            );
        }
        
        Poly::compute_ln(all_graphs, contg);
        
        for (size_t i = 0; i < MAX_DEGREE - 1; i++)
            contg[i] = Math::multiply(contg[i + 1], i + 1);
        contg[MAX_DEGREE - 1] = 0;
        
        Poly::compute_ln(contg, log_contg);
        
        Poly::comDer(log_contg, lgder);
        
        size_t size = 1 << 18;
        lgder.resize(size, 0);
        Poly::tand(lgder, false);
    }
    
public:
    Graphcnt() {
        init();
        preget();
    }
    
    int count_(LL n) {
        using namespace Math;
        
        n--;
        if (n == 0) return 1;
        
        vector<int> exparg(MAX_DEGREE);
        for (size_t i = 0; i < MAX_DEGREE; i++)
            exparg[i] = multiply(log_contg[i], subtract(0, n));

        vector<int> expres;
        Poly::nxtcomp(exparg, expres);
        
        size_t size = 1 << 18;
        expres.resize(size, 0);
        Poly::tand(expres, false);
        
        vector<int> result(lgder.begin(), lgder.end());
        for (size_t i = 0; i < size; i++)
            result[i] = multiply(result[i], expres[i]);
        
        Poly::tand(result, true);
        
        return multiply(result[n - 1], factot[n - 1]);
    }
};

int main() {
    Graphcnt cnt;
    
    for (int i = 1; i <= 5; i++) {
        LL n;
        cin >> n;
        cout << cnt.count_(n) << endl;
    }
    
    return 0;
}

总时间复杂度 O(nlog⁡n)O(n \log n) 常数还可以,在本题可以接受。

原文