题解:P16964 [SCCPC 2026] 最大权独立集问题

· · 题解

省赛的签到题

解析

两点之间有边当且仅当它们二进制表示中 1 的个数的奇偶性不同。因此图是一个完全二分图,同一奇偶类内部无边,最大权独立集即为选取全部奇类或全部偶类中权值较大的那一类。

下面是对完全二分图的一个粗略证明。

证明:

对于任意非负整数 x,记 f(x) = \operatorname{popcount}(x)。对于任意两个整数 a, b,有性质:

f(a \oplus b) \equiv f(a) + f(b) \pmod{2}

那么边 (i, j) 存在当且仅当 f(W_i \oplus W_j) 为奇数,即:

f(W_i) + f(W_j) \equiv 1 \pmod{2}

这意味着 f(W_i) 与 f(W_j) 的奇偶性不同。

于是,我们可以将所有点按照 f(W_i) 的奇偶性分成两类:

A = \{ i \mid f(W_i) \text{ is odd} \} B = \{ i \mid f(W_i) \text{ is even} \}

因此,图 G 是一个完全二分图,其两部分分别为 A 和 B。

\square

AC Code

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

void solve(void){
    int n;
    cin >> n;

    vector <int> arr(n);
    for (auto &num : arr) cin >> num;

    int odd = 0,even = 0;
    for (const auto &num : arr){
        if (__builtin_popcount(num) & 1) odd += num;
        else even += num;
    }
    cout << max(odd,even) << '\n';
}

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    int t;
    cin >> t;

    while (t--) solve();
    return 0;
}