题解:P16535 [THUPC 2026 决赛] 供电网络

· · 题解

伟大的 Purslane 随手场切了这题!

下文用 \oplus 表示异或。

令 x_i 表示 i 被划到了哪个模块中,把关于 i 的限制刻画为方程,有:

e_{i, 1}[x_1 = x_i] \oplus e_{i, 2}[x_2 = x_i] \oplus \dots \oplus e_{i, n}[x_n = x_i] = s_i

你发现 [x_1 = x_i] = x_1 \oplus x_i \oplus 1,记录 deg_i 表示 i 的度数模 2,方程又可以表示为:

e_{i, 1}x_1 \oplus e_{i, 2}x_2 \oplus \dots \oplus e_{i, i - 1}x_{i - 1} \oplus deg_i x_i \oplus e_{i, i + 1}x_{i + 1} \oplus \dots \oplus e_{i, n}x_n = s_i \oplus deg_i

题目中的树形结构提示我们要重点考虑方程组的合并问题。注意到初始我们有 n 个方程组,每消一次,总有一个方程组,满足:

均摊看来,只会有效合并 O(n^2) 次。考虑如果方程有解,那么答案应当是:n 减去系数矩阵的秩。

现在判断有没有解,拉出来没有用的方程组,用消之后的方程组表示这个方程组,如果能维护出方程组集合 S_u,则能在 O(\frac{n^2}{w}) 的时间内轻松判断是否合法。

维护难点在于方程组合并时,合并到另一个方程组的方程被消掉了,但是这只会发生 n 次,逐一暴力修改即可。总复杂度 O(\frac{(n + q)n^2}{w})。

::::info[代码]

// Think TWICE, code ONCE
#include <bits/stdc++.h>
#define ll long long
using namespace std;
inline ll Read() {
    int sig = 1; ll num = 0; char c = getchar();
    while(!isdigit(c)) { if(c == '-') sig = -1; c = getchar(); }
    while(isdigit(c)) num = (num << 3) + (num << 1) + (c ^ 48), c = getchar();
    return num * sig;
}
void Write(ll x) {
    if(x < 0) putchar('-'), x = -x;
    if(x > 9) Write(x / 10);
    putchar((x % 10) ^ 48);
}
const int N = 3005;
const ll Mod = 1e9 + 7;
int n, m, lbt[N], cnt[N], o[N], fa[N], deg[N];
bitset<N> f[N], g[N], e[N], p[N], q[N], vis;
ll ans[N], pw[N];
bool cmp(int x, int y) { return cnt[x] > cnt[y]; }
struct Eq {
    vector<int> vecf, vecg;
    void Ins(int x) {
        if(lbt[x] > n) { vecg.emplace_back(x); return ; }
        int i, siz = vecf.size();
        for(i = 0; i < siz; i++) {
            if(lbt[x] < lbt[vecf[i]]) { vecf.insert(vecf.begin() + i, x); return ; }
            if(lbt[x] == lbt[vecf[i]]) {
                f[x] ^= f[vecf[i]], g[x] ^= g[vecf[i]], lbt[x] = f[x]._Find_first();
                if(lbt[x] > n) { vecg.emplace_back(x); return ; }
            }
        }
        vecf.emplace_back(x);
    }
}a[N];
void Merge(int x, int y) {
    int i, j, siz = a[y].vecf.size();
    for(i = 0; i < siz; i++) {
        int t = a[y].vecf[i]; a[x].Ins(t);
        if(lbt[t] > n) {
            for(j = i + 1; j < siz; j++) if(g[a[y].vecf[j]][t]) g[a[y].vecf[j]] ^= g[t];
            for(auto w : a[y].vecg) if(g[w][t]) g[w] ^= g[t];
        }
    }
    for(auto v : a[y].vecg) a[x].vecg.emplace_back(v);
}
int main() {
    int i, j; n = Read(), m = Read(), pw[0] = 1;
    for(i = 1; i <= n; i++) pw[i] = pw[i - 1] * 2 % Mod;
    for(i = 1; i <= n; i++) {
        char c = getchar();
        while(!isdigit(c)) c = getchar();
        for(j = 1; j <= n; j++) e[i][j] = c == '1', c = getchar();
        deg[i] = e[i].count();
    }
    for(i = 1; i <= m; i++) {
        char c = getchar();
        while(!isdigit(c) && c != '?') c = getchar();
        for(j = 1; j <= n; j++) p[i][j] = (c == '1') ^ (deg[j] & 1), q[i][j] = isdigit(c), c = getchar();
        cnt[i] = q[i].count(), o[i] = i;
    }
    sort(o + 1, o + m + 1, cmp);
    for(i = 1; i <= n; i++) {
        f[i] = e[i], f[i][i] = deg[i] & 1;
        g[i][i] = 1, lbt[i] = f[i]._Find_first();
    }
    for(i = 1; i <= m; i++) for(j = i - 1; j; j--) if((q[o[i]] & q[o[j]]) == q[o[i]]) { fa[o[i]] = o[j]; break; }
    for(i = m; i; i--) {
        for(j = 1; j <= n; j++) if(q[o[i]][j] && !vis[j]) a[o[i]].Ins(j), vis[j] = true;
        ans[o[i]] = pw[n - a[o[i]].vecf.size()];
        for(auto v : a[o[i]].vecg) if((g[v] & p[o[i]]).count() & 1) { ans[o[i]] = 0; break; }
        if(fa[o[i]]) Merge(fa[o[i]], o[i]);
    }
    for(i = 1; i <= m; i++) Write(ans[i]), putchar('\n');
}

::::