zoukankan      html  css  js  c++  java
  • CodeForces

    Discription

    Given a simple graph, output the number of simple cycles in it. A simple cycle is a cycle with no repeated vertices or edges.

    Input

    The first line of input contains two integers n and m (1 ≤ n ≤ 19, 0 ≤ m) – respectively the number of vertices and edges of the graph. Each of the subsequent mlines contains two integers a and b, (1 ≤ a, b ≤ na ≠ b) indicating that vertices aand b are connected by an undirected edge. There is no more than one edge connecting any pair of vertices.

    Output

    Output the number of cycles in the given graph.

    Examples

    Input
    4 6
    1 2
    1 3
    1 4
    2 3
    2 4
    3 4
    Output
    7

    Note

    The example graph is a clique and contains four cycles of length 3 and three cycles of length 4.

        好像是很经典的一个问题呢。。。

        状压dp,设 f[S][i] 为 从S的二进制最低位作为起点, 且经过S集合中的点,目前走到i的路径种类。我们转移的时候枚举的点的编号 都必须大于 S的二进制最低位,这样就可以避免重复计算了。

        然后因为一个环会被正反走两次,所以最后还要除以2。

    #include<bits/stdc++.h>
    #define ll long long
    using namespace std;
    const int maxn=600005;
    int ci[35],n,m;
    bool v[35][35];
    ll ans,f[maxn][20];
    
    inline void solve(){
    	for(int i=0;i<n;i++) f[ci[i]][i]=1;
    	for(int S=1,now;S<ci[n];S++){
    		now=S&-S;
    		for(int i=0;i<n;i++) if(ci[i]==now){
    			now=i;
    			break;
    		}
    		
    		for(int i=0;i<n;i++) if(f[S][i]){
    			if(v[now][i]) ans+=f[S][i];
    		    for(int j=now+1;j<n;j++) if(!(ci[j]&S)&&v[i][j]) f[S|ci[j]][j]+=f[S][i];
    		}
    	}
        
    	ans=(ans-m)>>1;
    }
    
    int main(){
    	ci[0]=1;
    	for(int i=1;i<=20;i++) ci[i]=ci[i-1]<<1;
    	int uu,vv;
    	scanf("%d%d",&n,&m);
    	for(int i=1;i<=m;i++){
    		scanf("%d%d",&uu,&vv),uu--,vv--;
    		v[uu][vv]=v[vv][uu]=1;
    	}
    	solve();
    	cout<<ans<<endl;
    	return 0;
    }
    

      

  • 相关阅读:
    [转载]混合高斯模型
    威流IIS日志分析器1.2版本发布
    获取datagridview列中button点击事件
    C#引用winwebmail的dll
    威流网站监控系统新增飞信提醒
    关于Server.MapPath()
    把"\"转换成"/"
    asp.net站点常见问题绵集
    动态显示系统时间
    得到一个文件夹下的文件,并将文件删除
  • 原文地址:https://www.cnblogs.com/JYYHH/p/8867197.html
Copyright © 2011-2022 走看看