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;
}

  

CodeForces - 11D A Simple Task的更多相关文章

  1. [CodeForces 11D] A Simple Task - 状态压缩入门

    状态压缩/Bitmask 在动态规划问题中,我们会遇到需要记录一个节点是否被占用/是否到达过的情况.而对于一个节点数有多个甚至十几个的问题,开一个巨型的[0/1]数组显然不现实.于是就引入了状态压缩, ...

  2. Codeforces 11D A Simple Task 统计简单无向图中环的个数(非原创)

    太难了,学不会.看了两天都会背了,但是感觉题目稍微变下就不会了.dp还是摸不到路子. 附ac代码: 1 #include<iostream> 2 #include<cstdio> ...

  3. 计数排序 + 线段树优化 --- Codeforces 558E : A Simple Task

    E. A Simple Task Problem's Link: http://codeforces.com/problemset/problem/558/E Mean: 给定一个字符串,有q次操作, ...

  4. Codeforces 558E A Simple Task (计数排序&&线段树优化)

    题目链接:http://codeforces.com/contest/558/problem/E E. A Simple Task time limit per test5 seconds memor ...

  5. Codeforces C. A Simple Task(状态压缩dp)

    题目描述:  A Simple Task time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  6. Codeforces 558E A Simple Task(权值线段树)

    题目链接  A Simple Task 题意  给出一个小写字母序列和若干操作.每个操作为对给定区间进行升序排序或降序排序. 考虑权值线段树. 建立26棵权值线段树.每次操作的时候先把26棵线段树上的 ...

  7. CodeForces 588E A Simple Task(线段树)

    This task is very simple. Given a string S of length n and q queries each query is on the format i j ...

  8. Codeforces J. A Simple Task(多棵线段树)

    题目描述: Description This task is very simple. Given a string S of length n and q queries each query is ...

  9. Codeforces 588E. A Simple Task (线段树+计数排序思想)

    题目链接:http://codeforces.com/contest/558/problem/E 题意:有一串字符串,有两个操作:1操作是将l到r的字符串升序排序,0操作是降序排序. 题解:建立26棵 ...

随机推荐

  1. Aizu 2450 Do use segment tree 树链剖分

    题意: 给出一棵\(n(1 \leq n \leq 200000)\)个节点的树,每个节点有一个权值. 然后有\(2\)种操作: \(1 \, a \, b \, c\):将路径\(a \to b\) ...

  2. org.hibernate.AnnotationException: No identifier specified for entity: com.example1.demo1.Entity.User错误

    最近在公司带人,他们问我的问题在这里也顺便总结下. 此项目为SpringDataJpa项目. 出现的错误如下: Caused by: org.hibernate.AnnotationException ...

  3. git仓库删除所有提交历史记录

    stackoverflow原问题地址:http://stackoverflow.com/questions/13716658/how-to-delete-all-commit-history-in-g ...

  4. Python 前端 js基础

    Javascript 概述 JavaScript是一门编程语言,浏览器内置了JavaScript语言的解释器,所以在浏览器上按照JavaScript语言的规则编写相应代码之,浏览器可以解释并做出相应的 ...

  5. 《Nginx高性能Web服务器详解》

    第1章 Nginx初探 第2章 Nginx服务器的安装部署 第3章 Nginx服务器架构初探 第4章 Nginx服务器的高级设置 第5章 Nginx服务器的Gzip压缩 第6章 Nginx服务器的Re ...

  6. day05_01 鸡汤+内容回顾

    推荐电影: 1.被解救的姜戈 2.华尔街之狼 3.阿甘正传 4.辛德勒的名单 5.肖申克的救赎 6.上帝之城 7.焦土之城 8.绝美之城 打印多行 msg = "hello 1 hello ...

  7. Django模板导入和替换、以及对数据库的增加、查看

    静态文件引入的3中方式:例如对html模板里面对css样式的引入 STATIC_URL = '/static666/'STATICFILES_DIR=[ os.path.join(BASE_DIR,' ...

  8. [python IO学习篇] 补充.py文件是中文, .ini文件内容是中文

    python 代码文件的编码.py文件默认是ASCII编码,中文在显示时会做一个ASCII到系统默认编码的转换,这时就会出错:SyntaxError: Non-ASCII character.需要在代 ...

  9. Uiautomator ---(1) 封装代码

    http://www.cnblogs.com/by-dream/p/4996000.html  上面是别人的写法 我自己的写法: package qq.test; import android.con ...

  10. TOJ4203: Domino Piece

    4203: Domino Piece  Time Limit(Common/Java):1000MS/3000MS     Memory Limit:65536KByteTotal Submit: 5 ...