链接:https://ac.nowcoder.com/acm/contest/885/E
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 102400K,其他语言204800K
64bit IO Format: %lld
题目描述
Note: For C++ languages, the memory limit is 100 MB. For other languages, the memory limit is 200 MB.
In graph theory, an independent set is a set of nonadjacent vertices in a graph. Moreover, an independent set is maximum if it has the maximum cardinality (in this case, the number of vertices) across all independent sets in a graph.
An induced subgraph G'(V', E') of a graph G(V, E) is a graph that satisfies:
* V′⊆VV' \subseteq VV′⊆V
* edge (a,b)∈E′(a,b) \in E'(a,b)∈E′ if and only if a∈V′,b∈V′a \in V', b \in V'a∈V′,b∈V′, and edge (a,b)∈E(a, b) \in E(a,b)∈E;
Now, given an undirected unweighted graph consisting of n vertices and m edges. This problem is about the cardinality of the maximum independent set of each of the 2n2^n2n possible induced subgraphs of the given graph. Please calculate the sum of the 2n2^n2n such cardinalities.
输入描述:
The first line contains two integers n and m (2≤n≤26,0≤m≤n×(n−1)22 \le n \le 26, 0 \le m \le \frac{n \times (n-1)}{2}2≤n≤26,0≤m≤2n×(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,yix_i, y_ixi,yi (0≤xi<yi<n0 \le x_i < y_i < n0≤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.
示例1
输入
复制
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.
示例2
输入
复制
7 5
0 5
3 4
1 2
2 5
0 2
输出
复制
328
状压dp,对于每个状态集合x,我们设pos为任意一位(程序中选择最高位)状态为1的点,这个状态x的最大独立子集就是有pos在内的最大独立子集和没有pos在内的最大独立子集的最大值~。先考虑没有pos的情况,这时候最大独立子集就是 dp[x-(1<<pos)],因为这个(x-(1<<pos))数比x小所以一定之前处理过且有正确值;考虑有pos的情况,那就是当前dp[状态x与所有pos不临近的点的状态的交集的最大独立子集]+1(pos本身);
#include<bits/stdc++.h>
using namespace std;
int n, m;
int G[26];char dp[1 << 26];
int main() {
ios::sync_with_stdio(0);
cin >> n >> m;
while (m--) {
int a, b;
cin >> a >> b;
G[a] |= (1 << b);
G[b] |= (1 << a);
}
for (int i = 0; i < n; i++) {
G[i] |= 1 << i;
G[i] = ~G[i];
}
int ans = 0;
int pos = 0;
for (int i = 1; i < (1 << n); i++) {
if (!(i&(1 << pos)))pos++;
dp[i] = max((int)dp[i - (1 << pos)], dp[i&(G[pos])] + 1);
cout << i << " " << (i&G[pos]) << "\n";
ans += dp[i];
}
cout << ans << "\n";
return 0;
}