题解:CF2025F Choose Your Queries

· · 题解

Solution

可以发现如果 i 操作了 d_i 次,最后它会变成 d_i\bmod 2。

将 x_i,y_i 连边,问题变成初始全是白点,每条边选一个端点翻转颜色,最小化黑点数。

这个不好做,考虑先翻转 x_i 的颜色,问题就变成同时翻转两个点的颜色或不操作。这个是经典问题,对于每个连通块,只考虑 DFS 树上的边,自底向上考虑,每个点都将儿子颜色全调整为白色,容易证明这样到达下界。

Code

#include <bits/stdc++.h>
#define REP(i, l, r) for (int i = (l); i <= (r); ++ i)
#define DEP(i, r, l) for (int i = (r); i >= (l); -- i)
#define fi first
#define se second
#define pb emplace_back
#define mems(x, v) memset((x), (v), sizeof(x))
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
using namespace std;
namespace Milkcat {
    typedef long long LL;
    typedef pair<LL, LL> pii;
    const int N = 1e6 + 5;
    int n, m, A[N], B[N], vs[N], c[N], t[N];
    vector<pii> G[N];
    void dfs(int x) {
        vs[x] = 1;
        for (auto [y, i] : G[x]) {
            if (vs[y]) continue;
            dfs(y);
            if (c[y]) t[i] = 1, c[y] ^= 1, c[x] ^= 1;
        }
    }
    int main() {
        cin >> n >> m;
        REP(i, 1, m) {
            cin >> A[i] >> B[i], c[A[i]] ^= 1;
            G[A[i]].pb(B[i], i), G[B[i]].pb(A[i], i);
        }
        REP(i, 1, n)
            if (!vs[i]) dfs(i), c[i] = 0;
        REP(i, 1, m) {
            cout << (t[i] ? 'y' : 'x');
            int x = (t[i] ? B[i] : A[i]);
            cout << (c[x] ? '-' : '+') << '\n';
            c[x] ^= 1;
        }
        return 0;
    }
}
int main() {
    cin.tie(0)->sync_with_stdio(0);
    int T = 1;
    while (T --) Milkcat::main();
    return 0;
}