時間限制:C/C++ 1秒,其餘語言2秒
空間限制:C/C++ 102400K,其餘語言204800K
64bit IO Format: %lldhtml
The first line contains two integers n and m (2≤n≤26,0≤m≤n×(n−1)) --- the number of vertices and the number of edges, respectively. Next m lines describe edges: the i-th line contains two integers xi,yi (0≤xi<yi<n) --- the indices (numbered from 0 to n - 1) of vertices connected by the i-th edge.
The graph does not have any self-loops or multiple edges.
Print one line, containing one integer represents the answer.
3 2 0 1 0 2
9
The cardinalities of the maximum independent set of every subset of vertices are: {}: 0, {0}: 1, {1}: 1, {2}: 1, {0, 1}: 1, {0, 2}: 1, {1, 2}: 2, {0, 1, 2}: 2. So the sum of them are 9.
連接:https://ac.nowcoder.com/acm/contest/885/E
來源:牛客網
題意:求一個圖的2^n種子圖的最大點獨立集。
思路:c++
•咱們能夠用一個 n-bit 2 進制整數來表示一個點集,第 i 個 bit 是 1 就表明點集包含第 i 個 點,如果 0 則不包含
• 每一個點相鄰的點也能夠用一個 n-bit 2 進制整數表示,計作 ci,若第 i 個點和第 j 個點相鄰, ci 的第 j 個 bit 是 1,不然是 0
• 記 x 的最低位且是 1 的 bit 的位置是 lbx
• 令 dp[x] 表明點集 x 的最大獨立集 size,那麼咱們可以根據點 lbx 是否屬於最大獨立集來列 出如下關係式:
dp[x] = max(dp[x - (1<<lbx)], dp[x & (~clb_x)] + 1) (使用 c 語言運算符)ide
•高效位運算參考博客:https://blog.csdn.net/yuer158462008/article/details/46383635oop
#include<bits/stdc++.h> using namespace std; char dp[1<<26]; int Map[26]={0}; int max(char a,int b) { if(a>b)return a; return b; } int main() { int n,m; scanf("%d %d",&n,&m); while(m--) { int u,v; scanf("%d %d",&u,&v); Map[u]|=(1<<v); Map[v]|=(1<<u); } for(int i=0;i<n;i++)Map[i]|=1<<i; int upper=1<<n; long long ans=0; for(int i=1;i<upper;i++) { int lbx=__builtin_ctz(i); dp[i] = max(dp[i - (1<<lbx)] , dp[i & (~Map[lbx])] + 1); ans+=dp[i]; } printf("%lld\n",ans); return 0; }