题解:CF2194F2 Again Trees... (hard version)

· · 题解

竞选最奇异搞笑做法。

建立 \{b_i\} 形成的异或线性基 B。令树以 r 为根。设 S_u 为 u 子树中所有点的权值异或和。那么只有 S_u 能被 B 表出时才可能断开 u \to \operatorname{father}(u) 的边。称这些点为关键点。考虑建立这些点的父子关系树,即将具有祖先关系且中间没有其它关键点的两个关键点之间连边。将每个点的权值贡献给它最近的关键祖先,则接下来只用考虑这颗父子关系树上的断边方案。

设计 DP。设 f(u,S) 表示以 u 为根,u 所在连通块的权值异或和为 S(若断开 u \to \operatorname{father}(u) 则 S=0)。转移为:

\begin{aligned} &f(u,S) \gets \sum_{S_1\oplus S_2\oplus \dots\oplus S_{|son(u)|}=S} \prod_{v \in \operatorname{son(u)}} f(v,S_i) \\ &\forall 1\le i\le k:f(u,0) \gets f(u,b_i) \end{aligned}

答案为 \sum f(r,b_i)。注意 r 不需要做第二类转移。

注意到 S 可能很大,但实际有用的只有 2^{|B|} 个。可以将 b_i 全部用线性基表出。

上面一行就是异或卷积,容易使用 FWT 实现。但是每次做 FWT 和 IFWT 复杂度是 O(n k 2^{|B|}) 的,仍不够优秀。

考虑在过程中始终保持 FWT 的状态。那么我们需要实现:提取出一个序列 IFWT 后的某个点值(即求出 f(u,b_i));将 IFWT 后的序列上 0 位置上的值加上某个数,再 FWT 回去。

第二种操作容易做到 O(2^{|B|})。第一种操作若是 O(2^{|B|}) 地做,那么复杂度中仍然带 k。考虑令有 |B| 个 b_i 的 popcount 为 1(这可以通过在插入线性基时,若和线性基中当前元素线性不相关,那么就直接把它原值插进去)。那么我们实际上要提取出 IFWT 后所有 popcount 为 1 的位置,还有 k-|B| 个其它的位置。我们可以同时提取 popcount 为 1 的位置的值之和,剩下的每个 O(2^{|B|}) 提取。同时提取的式子如下:

\begin{aligned} &\sum_{i=0}^{|B|-1} \sum_{j=0}^{2^{|B|}-1} (-1)^{|\{i\} \cap j|} \hat{f}(j)\\ =&\sum_{j=0}^{2^{|B|}-1} \hat{f}(j) \sum_{i=0}^{|B|-1} (-1)^{|\{i\} \cap j|}\\ =&\sum_{j=0}^{2^{|B|}-1} \hat{f}(j) (|B|-2|j|) \end{aligned}

于是可以 O(2^{|B|}) 提取出 IFWT 后所有 popcount 为 1 的位置。此时复杂度优化到 O(n (k-|B|+1) 2^{|B|}) \le O(n 2^k),可以通过。

#include <bits/stdc++.h>
using namespace std;

using i64 = long long;
template<class T>

constexpr T power(T a, i64 b) {
    T res = 1;
    for (; b; b /= 2, a *= a) {
        if (b % 2) {
            res *= a;
        }
    }
    return res;
}

template<int P>

struct MInt {
    int x;
    constexpr MInt() : x{} {}
    constexpr MInt(i64 x) : x{norm(x % getMod())} {}

    static int Mod;
    constexpr static int getMod() {
        if (P > 0) {
            return P;
        } else {
            return Mod;
        }
    }
    constexpr static void setMod(int Mod_) {
        Mod = Mod_;
    }
    constexpr int norm(int x) const {
        if (x < 0) {
            x += getMod();
        }
        if (x >= getMod()) {
            x -= getMod();
        }
        return x;
    }
    constexpr int val() const {
        return x;
    }
    explicit constexpr operator int() const {
        return x;
    }
    constexpr MInt operator-() const {
        MInt res;
        res.x = norm(getMod() - x);
        return res;
    }
    constexpr MInt inv() const {
        assert(x != 0);
        return power(*this, getMod() - 2);
    }
    constexpr MInt &operator*=(MInt rhs) & {
        x = 1LL * x * rhs.x % getMod();
        return *this;
    }
    constexpr MInt &operator+=(MInt rhs) & {
        x = norm(x + rhs.x);
        return *this;
    }
    constexpr MInt &operator-=(MInt rhs) & {
        x = norm(x - rhs.x);
        return *this;
    }
    constexpr MInt &operator/=(MInt rhs) & {
        return *this *= rhs.inv();
    }
    friend constexpr MInt operator*(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res *= rhs;
        return res;
    }
    friend constexpr MInt operator+(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res += rhs;
        return res;
    }
    friend constexpr MInt operator-(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res -= rhs;
        return res;
    }
    friend constexpr MInt operator/(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res /= rhs;
        return res;
    }
    friend constexpr std::istream &operator>>(std::istream &is, MInt &a) {
        i64 v;
        is >> v;
        a = MInt(v);
        return is;
    }
    friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a) {
        return os << a.val();
    }
    friend constexpr bool operator==(MInt lhs, MInt rhs) {
        return lhs.val() == rhs.val();
    }
    friend constexpr bool operator!=(MInt lhs, MInt rhs) {
        return lhs.val() != rhs.val();
    }
};

constexpr int P = 1e9 + 7;
using Z = MInt<P>;

int len;

int w(int i, int j) {
    return __builtin_parity(i & j);
}


struct SPoly {
    static const int N = 10;
    Z a[1 << N];
    void fwt() {
        for (int k = 2; k <= 1 << len; k *= 2) {
            int l = k >> 1;
            for (int i = 0; i < 1 << len; i += k)
                for (int j = 0; j < l; j++) {
                    a[i + j] += a[i + j + l];
                    a[i + j + l] = a[i + j] - a[i + j + l] * 2;
                }
        }
    }
    void Ifwt() {
        for (int k = 2; k <= 1 << len; k *= 2) {
            int l = k >> 1;
            for (int i = 0; i < 1 << len; i += k)
                for (int j = 0; j < l; j++) {
                    a[i + j] += a[i + j + l];
                    a[i + j + l] = a[i + j] - a[i + j + l] * 2;
                }
        }
        for (int i = 0; i < (1 << len); i++)
            a[i] /= (1 << len);
    }
    void add(int p, Z v) {
        for (int i = 0; i < 1 << len; i++) {
            if (w(i, p))
                a[i] -= v;
            else
                a[i] += v;
        }
    }
    Z get(int p) {
        Z res = 0;
        for (int i = 0; i < 1 << len; i++) {
            if (w(i, p))
                res -= a[i];
            else
                res += a[i];
        }
        return res / (1 << len);
    }
    Z sum_pc1() {
        Z res = 0;
        for (int i = 0; i < 1 << len; i++) {
            int p = __builtin_popcount(i);
            res += (len - p * 2) * a[i];
        }
        return res / (1 << len);
    }
};
const int N = 100005;

struct Base {
    vector<int> b;
    map<int, int> expr;
    int size() {
        return b.size();
    }
    int insert(int x) {
        expr[0] = 0;
        if (expr.count(x))
            return expr[x];
        b.push_back(x);
        for (int S = 0; S < 1 << b.size(); S++) {
            int sum = 0;
            for (int i = 0; i < b.size(); i++)
                if (S >> i & 1)
                    sum ^= b[i];
            expr[sum] = S;
        }
        return expr[x];
    }
    int express(int x) {
        return expr.count(x) ? expr[x] : -1;
    }
    void clr() {
        b.clear(), expr.clear();
    }
} b;


int n, k, val[N], B[11], epr[N];
vector<int> E[N], T[N];
SPoly f[N];

void dfs(int u, int f) {
    for (int it : E[u])
        if (it ^ f)
            dfs(it, u), val[u] ^= val[it];
    epr[u] = b.express(val[u]);
}

void efs(int u, int f, int up) {
    if (~epr[u]) {
        if (up)
            T[up].push_back(u);
        up = u;
    }
    for (int it : E[u])
        if (it ^ f)
            efs(it, u, up);
}

void hfs(int u) {
    for (int it : T[u])
        epr[u] ^= epr[it], hfs(it);
}

void gfs(int u) {
    f[u].add(epr[u], 1);
    for (int it : T[u]) {
        gfs(it);
        for (int i = 0; i < (1 << len); i++)
            f[u].a[i] *= f[it].a[i];
    }
    if (u ^ 1) {
        Z S = f[u].sum_pc1();
        for (int i = 0; i < k; i++)
            if (__builtin_popcount(B[i]) ^ 1)
                S += f[u].get(B[i]);
        f[u].add(0, S);
    }
}

void solve() {
    for (int i = 1; i <= n; i++)
        E[i].clear(), T[i].clear();
    cin >> n >> k, len = k;
    for (int i = 1, u, v; i < n; i++)
        cin >> u >> v, E[u].push_back(v), E[v].push_back(u);
    for (int i = 1; i <= n; i++)
        cin >> val[i], memset(f[i].a, 0, sizeof(f[i].a));
    b.clr();
    for (int i = 0, v; i < k; i++)
        cin >> v, b.insert(v), B[i] = v;
    len = b.size(), dfs(1, 0);
    if (!~b.express(val[1])) {
        cout << "0\n";
        return;
    }
    for (int i = 0; i < k; i++)
        B[i] = b.express(B[i]);
    efs(1, 0, 0), hfs(1), gfs(1), f[1].Ifwt();
    Z res = 0;
    for (int i = 0; i < k; i++)
        res += f[1].a[B[i]];
    cout << res << '\n';
}

int main() {
    int T;
    cin >> T;
    while (T--)
        solve();
    return 0;
}