1. 程式人生 > >1130: [POI2008]POD Subdivision of Kingdom

1130: [POI2008]POD Subdivision of Kingdom

1130: [POI2008]POD Subdivision of Kingdom

https://lydsy.com/JudgeOnline/problem.php?id=1130

分析:

  有效狀態為$C_{26}^{13}$,所以直接搜尋就好了。

程式碼:

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<cmath>
 6 #include<cctype>
 7
#include<set> 8 #include<queue> 9 #include<vector> 10 #include<map> 11 #include<bitset> 12 using namespace std; 13 typedef long long LL; 14 15 inline int read() { 16 int x=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1; 17 for
(;isdigit(ch);ch=getchar())x=x*10+ch-'0';return x*f; 18 } 19 20 const int N = 10010; 21 22 int e[100], num[N]; 23 int All, n, mn = 1e9, ans; 24 25 int Calc(int x) { 26 return num[x >> 13] + num[x & ((1 << 13) - 1)]; 27 } 28 void dfs(int x,int last,int sta,int cnt) { // 當前已經有多少點,上一個點,狀態,邊數
29 if (x == n / 2) { 30 if (cnt < mn) mn = cnt, ans = sta; 31 return ; 32 } 33 for (int i = last + 1; i < n; ++i) { 34 if ((sta >> i) & 1) continue; 35 dfs(x + 1, i, sta | (1 << i), cnt - Calc(sta & e[i]) + Calc((All ^ sta) & e[i])); // ^和&的優先順序!! 36 } 37 } 38 int main() { 39 n = read();int m = read(); 40 All = (1 << n) - 1; 41 for (int i = 1; i <= m; ++i) { 42 int u = read() - 1, v = read() - 1; 43 e[u] |= (1 << v), e[v] |= (1 << u); 44 } 45 for (int i = 1; i < (1 << 13); ++i) num[i] = num[i >> 1] + (i & 1); 46 dfs(0, -1, 0, 0); 47 if (!(ans & 1)) ans = ((1 << n) - 1) ^ ans; 48 for (int i = 0; i < n; ++i) 49 if ((ans >> i) & 1) cout << i + 1 << " "; 50 return 0; 51 }