P3475 [POI2008]POD-Subdivision of Kingdom 题解
除了 dfs 枚举状态外,还可以直接循环利用 __builtin_popcount 枚举
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;
}