跳到正文

题解

P3475 [POI2008]POD-Subdivision of Kingdom 题解

andyli约 2 分钟
原文
文章信息与版本

文章信息

发布
2023年2月18日
收录
2026年9月26日
更新
2026年11月17日
篇幅
约 2 分钟
获赞
1
题目
P3475

版本

  1. v1当前

除了 dfs 枚举状态外,还可以直接循环利用 __builtin_popcount 枚举 [0,2n)\left[0, 2^n\right) 中二进制下 11 的个数为 n2\dfrac{n}{2} 的状态,此时 SS 中的 11 表示一个集合中的点,∼S\sim S 中前 nn 位的 11 表示另一集合中的点,对于每个点 uu 计算与 uu 有边连接的点集和 SS 或 ∼S\sim S (取决于 uu 是否在 SS 中)按位与后 11 的个数,累加计算贡献。

C++
int main() {
    dR(int, n, m);
    std::vector<int> e(n);
    while (m--) {
        dR(int, u, v), u--, v--;
        e[u] |= 1 << v;
        e[v] |= 1 << u;
    }
    int ans = 0, v = 1e9;
    for (int S = 0; S < (1 << n); S++)
        if (__builtin_popcount(S) == n >> 1) {
            int c = 0;
            for (int i = 0; i < n; i++)
                if (S & (1 << i))
                    c += __builtin_popcount(~S & e[i]);
                else
                    c += __builtin_popcount(S & e[i]);
            if (c < v)
                v = c, ans = S;
        }
    std::vector<int> a;
    for (int i = 0; i < n; i++)
        if (ans & (1 << i))
            a.push_back(i + 1);
    io.displayArray(a);
    return 0;
}
原文