independent set 1

 independent set 1

Time limit: C / C ++ 1 second, 2 seconds languages other
space restrictions: C / C ++ 102400K, other languages 204800K
64bit the IO the Format: LLD%

Title Description

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′⊆V

* edge (a,b)∈E′ if and only if a∈V′,b∈V′, and edge (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 2^n possible induced subgraphs of the given graph. Please calculate the sum of the 2^n such cardinalities.

Enter a description:

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.

Output Description:

Print one line, containing one integer represents the answer.

Entry

3 2
0 1
0 2

Export

9

Explanation

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. 
link: https://ac.nowcoder.com/acm/contest/885/E
source: cattle off network

Meaning of the questions: Seeking a graph n ^ maximum point of view of an independent set 2 seed.
Ideas:

 • We can use a binary n-bit 2 represented by a set of points integers, 1 is the i-th bit represents the set of points comprising the i-th point, if 0 is not included 
 • each point adjacent points can also be used a n-bit 2 binary integer representation, namely, do ci, if the i-th point and the j-th point adjacent, ci j-th bit is 1, otherwise it is 0
 the least significant bit of x and • remember that bit 1 of the location is lbx
 • make dp largest independent set of size [x] represents the set of points x, then we are able to list the following relationship according to whether the point lbx maximum independent set:
 dp [x] = max (dp [x - ( 1 << lbx)], dp [ x & (~ clb_x)] + 1) ( the use of language operators c)

 • Efficient bit computing reference blog: https://blog.csdn.net/yuer158462008/article/details/46383635

#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;
}
View Code

 

Guess you like

Origin www.cnblogs.com/tian-luo/p/11286222.html