题解:P14759 [Opoi 2025] CCD 的异或 II

· · 题解

T613841 [Opoi 2025] CCD 的异或 II —— 题解

一、问题转化

给定一个长度为 n 的 01 序列 A,定义前缀异或:

S_0 = 0,\quad S_i = A_1 \oplus A_2 \oplus \cdots \oplus A_i \quad (1 \le i \le n)

那么有显然的关系:

A_i = S_{i-1} \oplus S_i

二、约束的等价形式

题目给出 m 个限制 (l,r),要求:

A_l \oplus A_{l+1} \oplus \cdots \oplus A_r = 1

代入前缀异或:

S_r \oplus S_{l-1} = 1

因此,每个限制都等价于 在 S_{l-1} 与 S_r 之间连一条权值为 1 的边。

我们得到一个包含 n+1 个点(S_0 \sim S_n)的无向图,每条边表示两点异或差为 1。

三、判定是否有解(图的二分性)

在图中,每条边 (u,v) 要求:

S_u \oplus S_v = 1

这相当于一个 异或约束图。
我们可以对每个连通块做一次 DFS / BFS,给每个点赋一个 0/1 的值:

如果在遍历过程中出现矛盾(即已访问的点不满足约束),则 无解。

四、方案数计算

假设最终图中有 k 个连通块。

但注意:S_0 = 0 被固定,因此包含 S_0 的那个连通块 不能翻转。

所以总方案数为:

\text{ans} = 2^{k-1} \pmod{998244353}

五、构造字典序最小的 A

回忆:

A_i = S_{i-1} \oplus S_i

我们已经通过 BFS 得到了一个基准解 S_i^{(0)}。
设每个连通块有一个“翻转变量” t_c \in \{0,1\},则真实解为:

S_i = S_i^{(0)} \oplus t_{\text{comp}(i)}

于是:

A_i = S_{i-1} \oplus S_i = \big(S_{i-1}^{(0)} \oplus S_i^{(0)}\big) \oplus t_{\text{comp}(i-1)} \oplus t_{\text{comp}(i)}

贪心策略

从 i=1 到 n,依次决定 A_i:

这可以用一个 带异或信息的并查集(DSU with parity) 来维护连通块之间的翻转关系。

这样保证了:

六、算法复杂度

满足 n,m \le 10^6 的要求。

七、小结

本题的核心思想是:

  1. 前缀异或建模
  2. 异或约束图 + 判定可行性
  3. 连通块自由度 = 方案数
  4. DSU + 贪心构造字典序最小解

这是一个非常经典且优雅的「前缀异或 + 图论 + 并查集」综合题。

七、代码

// Problem: T613841 [Opoi 2025] CCD 的异或 II
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/T613841?contestId=194087
// Memory Limit: 512 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
static const int MOD = 998244353;
int modpow(int a, int e)
{
    long long r = 1,x = a;
    while(e)
    {
        if(e & 1) r = r * x % MOD;
        x = x * x % MOD;
        e >>= 1;
    }
    return (int)r;
}
struct DSUParity
{
    int n;
    vector<int> p, rk, xr;
    DSUParity(){}
    DSUParity(int n_){init(n_);}
    void init(int n_)
    {
        n = n_;
        p.resize(n + 1);
        rk.assign(n + 1, 0);
        xr.assign(n + 1, 0);
        for(int i = 1; i <= n; i++)
        {
            p[i] = i;
            xr[i] = 0;
        }
    }
    pair<int,int> findp(int a)
    {
        if(p[a] == a) 
            return 
            {
                a,0
            };
        auto pr = findp(p[a]);
        p[a] = pr.first;
        xr[a] ^= pr.second;
        return {p[a],xr[a]};
    }
    bool unite(int a, int b, int val)
    {
        auto pa = findp(a);
        auto pb = findp(b);
        int ra = pa.first, rb = pb.first;
        int xa = pa.second, xb = pb.second;
        if(ra == rb)
        {
            return ((xa ^ xb) == val);
        }
        if(rk[ra] < rk[rb])
        {
            swap(ra, rb);
            swap(xa, xb);
        }
        p[rb] = ra;
        xr[rb] = xa ^ xb ^ val;
        if(rk[ra] == rk[rb]) rk[ra]++;
        return true;
    }
    bool same(int a, int b, int &par)
    {
        auto pa = findp(a);
        auto pb = findp(b);
        if(pa.first != pb.first) return false;
        par = pa.second ^ pb.second;
        return true;
    }
};

int main()
{
    int n, m;
    cin>>n>>m;
    int N = n+1;
    vector<int> head(N, -1);
    vector<int> to(2LL * m);
    vector<int> nx(2LL * m);
    vector<unsigned char> w(2LL * m);
    int ec = 0;
    auto add_edge = [&](int u, int v, int ww)
    {
        to[ec] = v;
        w[ec] = (unsigned char)ww;
        nx[ec] = head[u];
        head[u] = ec++;
    };
    for(int i=0;i<m;i++)
    {
        int l, r;
        cin >> l >> r;
        int u = l - 1;
        int v = r;
        add_edge(u, v, 1);
        add_edge(v, u, 1);
    }
    vector<int> compId(N, 0);
    vector<unsigned char> dist(N, 0);
    vector<unsigned char> vis(N, 0);
    int comps = 0;
    bool ok = true;
    auto bfs_comp = [&](int s)
    {
        comps++;
        compId[s] = comps;
        dist[s] = 0;
        vis[s] = 1;
        vector<int> st;
        st.reserve(1024);
        st.push_back(s);
        while(!st.empty() && ok)
        {
            int u = st.back();
            st.pop_back();
            for(int e = head[u]; e != -1; e = nx[e])
            {
                int v = to[e];
                int ww = (int)w[e];
                if(!vis[v])
                {
                    vis[v] = 1;
                    compId[v] = comps;
                    dist[v] = dist[u] ^ (unsigned char)ww;
                    st.push_back(v);
                }
                else
                {
                    if(((dist[u] ^ dist[v]) & 1) != ww)
                    {
                        ok = false;
                        return;
                    }
                }
            }
        }
    };
    bfs_comp(0);
    for(int i=1;i<N&&ok;i++)
    {
        if(!vis[i]) 
            bfs_comp(i);
    }
    if(!ok)
    {
        cout<<0;
        return 0;
    }
    int ways = modpow(2,comps - 1);
    cout<<ways<<endl;
    DSUParity dsu(comps);
    string ans;
    ans.reserve(n);
    for(int i = 1; i <= n; i++)
    {
        int cu = compId[i - 1];
        int cv = compId[i];
        int base = ((dist[i]^dist[i - 1]) & 1);
        if(cu == cv)
        {
            ans.push_back(char('0' + base));
        }
        else
        {
            int wantParity = base;
            int curParity;
            if(dsu.same(cu, cv, curParity))
            {
                int bit = (curParity == wantParity) ? 0:1;
                ans.push_back(char('0'+bit));
            }
            else
            {
                dsu.unite(cu, cv, wantParity);
                ans.push_back('0');
            }
        }
    }

    cout<<ans;
    return 0;
}