Codeforces Beta Round #11 D. A Simple Task(状压DP)
来源:互联网 发布:问道手游辅助软件 编辑:程序博客网 时间:2024/05/22 01:29
Given a simple graph, output the number of simple cycles in it. A simple cycle is a cycle with no repeated vertices or edges.InputThe 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 m lines contains two integers a and b, (1 ≤ a, b ≤ n, a ≠ b) indicating that vertices a and b are connected by an undirected edge. There is no more than one edge connecting any pair of vertices.OutputOutput the number of cycles in the given graph.Examplesinput4 61 21 31 42 32 43 4output7NoteThe example graph is a clique and contains four cycles of length 3 and three cycles of length 4.
分析:求简单环个数,n很小,但是爆搜是不行的。。因为结果是阶乘级别的,考虑状压dp,f[i][s][t]表示当前链中点的集合为i,起点为s终点为t的所有方案数,因为s,t必定属于i,而且一个简单环有多重表示方法,所以我们考虑精简状态,f[i][t]表示起点s 为 i 中编号最小点,终点为t的所有链方案数,最后如果edg(s,t)存在就将f[i][t]记入答案,因为环上每点有两个相邻点,所以最后答案要除以二。
#include<iostream>#include<string>#include<algorithm>#include<cstdlib>#include<cstdio>#include<set>#include<map>#include<vector>#include<cstring>#include<stack>#include<queue>#define INF 2147483640#define eps 1e-9#define MAXN 0x3f#define N 20using namespace std;int n,m,u,v,fh[1<<19];unsigned long long ans,dp[1<<19][N];bool edg[N][N];int f(int x){return 1<<(x-1);}int main(){cin>>n>>m;for(int i = 1;i <= n;i++) fh[f(i)] = i;for(int i = 1;i <= m;i++){cin>>u>>v;edg[u][v] = edg[v][u] = true;}for(int i = 1;i <= (1<<n)-1;i++) for(int t = fh[i & (-i)] + 1;t <= n;t++) if(f(t) & i) { int s = fh[i & (-i)],sta = i - f(t) - f(s); if(!sta) { dp[i][t] = edg[s][t]; continue; } for(int j = sta;j;j -= j & (-j)) if(edg[fh[j & (-j)]][t]) dp[i][t] += dp[i - f(t)][fh[j & (-j)]]; if(edg[s][t]) ans += dp[i][t]; }cout<<ans/2<<endl;}
0 0
- Codeforces Beta Round #11 D. A Simple Task(状压DP)
- Codeforces Beta Round #11, problem: (D) A Simple Task 状态压缩DP+记忆化搜素DP
- [无向图简单环计数 状压DP] Codeforces 11D Beta Round #11 D. A Simple Task
- Codeforces Beta Round #11 D. A Simple Task
- Codeforces Beta Round #11 D. A Simple Task
- Codeforces 11D - A Simple Task (状压DP)
- Codeforces 11D A Simple Task (状态压缩DP)
- CodeForces 11D - A Simple Task 状态压缩DP
- 状态DP求哈密顿回路个数 CodeForces 11D——A Simple Task
- CodeForces 11D A Simple Task (DP解哈密顿路径数目)
- Codeforces Beta Round #10 D. LCIS(DP)
- Codeforces Beta Round #51 D 数位DP
- Codeforces Beta Round #51 D 数位DP
- Codeforces Round #312 (Div. 2)-A Simple Task
- CF 11D A Simple Task
- Codeforces Beta Round #35 (Div. 2), problem: (D) Animals DP
- Codeforces Beta Round #51 D. Beautiful numbers (数位dp)
- Codeforces Beta Round #10 D. LCIS(DP&LCIS)
- shiro整合springmvc
- [转]如何更改AD域安全策略-密码必须符合复杂性要求
- jQuery学习总结
- CDH集群迁移
- listener TNS-01189 问题
- Codeforces Beta Round #11 D. A Simple Task(状压DP)
- 1069. The Black Hole of Numbers (20)
- (转)android Activity runOnUiThread() 方法使用
- linux权限问题
- nginx 安装、配置、参数介绍
- BZOJ 1007, 水平可见直线
- MYSQL的八大缺陷
- Sublime Text 3鼠标选中时背景色的修改方法
- 窄带物联网(NB-IoT)深入了解