题解:CF2109D D/D/D

· · 题解

注意到图是无向图,因此可以来回走一条边刷步数,但是这样步数的奇偶性不变。

建分层图,把每个点拆成奇点和偶点,连边 (i,j) 的时候 i 的奇点和 j 的偶点连边,i 的偶点和 j 的奇点连边,然后从 1 的偶点开始跑最短路,这样就能处理出从 1 开始到每个点经过奇数条边和经过偶数条边的最短路。

设到第 i 个点,经过奇数条边的最短路为 dis_i,再在 A 中找到一个子集,满足这个子集元素和为奇数且最大,设为 t,如果 dis_i \le t 说明可以从点 1 开始经过奇数条边先到达 i 然后不断刷步数最后凑到 t 步。经过偶数条边同理。

:::success[CODE]

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 3e+6 + 6;
int n, m, T, tot, k;
int dis[maxn], a[maxn], h[maxn];
bool vis[maxn];
struct V {
    int to, nxt;
}e[maxn];
void add(int x, int y) {
    e[++tot] = {y, h[x]};
    h[x] = tot;
}
void AE(int x, int y) {
    add(x, y), add(y, x);
}
struct P {
    int p, w;
    friend bool operator < (const P &x, const P &y) {
        return x.w > y.w;
    }
};
priority_queue<P> q;
void GOGOGO() {
    cin >> n >> m >> k;
    int res = 1e18, sum = 0;
    for (int i = 1; i <= k; i++) cin >> a[i], sum += a[i];
    sort(a + 1, a + k + 1);
    for (int i = 1; i <= k; i++) {
        if (a[i] & 1) {
            res = a[i];
            break;
        }
    }
    tot = 0;
    for (int i = 1; i <= n * 2; i++) {
        h[i] = 0;
    }
    for (int i = 1; i <= m; i++) {
        int x, y;
        cin >> x >> y;
        AE(x, y + n), AE(y, x + n);
    }
    for (int i = 1; i <= n * 2; i++) {
        dis[i] = 1e18;
        vis[i] = 0;
    }
    dis[1] = 0;
    q.push({1, 0});
    while (!q.empty()) {
        int p = q.top().p;
        q.pop();
        if (vis[p]) continue;
        vis[p] = 1;
        for (int i = h[p]; i; i = e[i].nxt) {
            int j = e[i].to;
            if (dis[j] > dis[p] + 1) {
                dis[j] = dis[p] + 1;
                q.push({j, dis[j]});
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        if (sum & 1) {
            if (dis[i + n] <= sum || dis[i] <= sum - res) cout << 1;
            else cout << 0;
        } else {
            // cout << dis[i] << ' ' << dis[i + n] << '\n';
            if (dis[i] <= sum || dis[i + n] <= sum - res) cout << 1;
            else cout << 0;
        }
    }
    cout << '\n';
}
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> T;
    while (T--) GOGOGO();
    return 0;
}

:::