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
 2 #include
 3 #include
 4 #include
 5 #include
 6 #include
 7 #include<set>
 8 #include
 9 #include
10 #include
11 #include
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 }

 

转载于:https://www.cnblogs.com/mjtcn/p/10069887.html

你可能感兴趣的:(php)