状态压缩codeforces 11 D

n个点m条边

m条边

求有几个环;

#pragma comment(linker, "/STACK:102400000,102400000")  
#include <iostream>  
#include <cstdio>  
#include <cstring>  
#include <string>  
#include <vector>  
#include <algorithm>  
#include <queue>  
#include <set>  
#include <map>  
using namespace std;  
  
typedef long long LL;  
typedef pair<int,int> PII;  
  
const int N=20;  
  
LL ans;  
int n,m,low;  
LL dp[600000][N];  
bool adj[N][N];  
  
  
int main()  
{  
    scanf("%d%d",&n,&m);  
  
    for(int i=0;i<m;i++)  
    {  
        int a,b; scanf("%d%d",&a,&b);  
        a--; b--;  
        adj[a][b]=adj[b][a]=1;  
    }  
  
    for(int i=0;i<n;i++) dp[1<<i][i]=1;  
  
    for(int i=1;i<(1<<n);i++)  
    {  
        int st=n+1,cnt=0;  
        for(int j=0;j<n;j++)        //这个状态中有多少个点 
     if(i&(1<<j)) { st=min(st,j); cnt++; } for(int j=0;j<n;j++)
   if(dp[i][j]) { if(adj[j][st]&&cnt>=3) //点>=三 又要能回去 { ans+=dp[i][j]; } for(int k=0;k<n;k++)
        if(adj[j][k]&&k>st) //st是最小的点 { if((i&(1<<k))==0) dp[i^(1<<k)][k]+=dp[i][j]; } } } cout<<ans/2<<endl; return 0; }
原文地址:https://www.cnblogs.com/cherryMJY/p/6142306.html