hihocoder 1048 Hamiltonian Cycle 状态压缩dp入门题5

描述

Given a directed graph containing n vertice (numbered from 1 to n) and m edges. Can you tell us how many different Hamiltonian Cycles are there in this graph?

A Hamiltonian Cycle is a cycle that starts from some vertex, visits each vertex (except for the start vertex) exactly once, and finally ends at the start vertex.

Two Hamiltonian Cycles C1, C2 are different if and only if there exists some vertex i that, the next vertex of vertex i in C1 is different from the next vertex of vertex i in C2.

输入

The first line contains two integers n and m. 2 <= n <= 12, 1 <= m <= 200.

Then follows m line. Each line contains two different integers a and b, indicating there is an directed edge from vertex a to vertex b.

输出

Output an integer in a single line -- the number of different Hamiltonian Cycles in this graph.

提示

额外的样例:

样例输入样例输出
3 3
1 2              
2 1              
1 3
0


 

 

样例输入

4 7
1 2
2 3
3 4
4 1
1 3
4 2
2 1

样例输出

2

 

题意:给你一个n个结点m条边的有向图,让你求有多少种方法可以从1个结点出发,经过其他点后然后再回到起点。其实就是求哈密顿环的数目。

解题思路:这个题让我对状态压缩有一个新的思考。dp和图的结合。对于每一个假设已经经历过一些点的状态,u和v一定已经被经历过了并且现在我假设从u出发到v,那么只经过u的状态里一定没有v。如果u和v没有边的话,那么我们就不需要这种状态。

这样的话我们就能构建状态转移方程 dp[i][v]+=dp[i^(1<<(v-1))][u]*mp[u][v]; i表示的是状态。在这题里,重边得看成一条边。

 

 

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=20;
#define mem(a,b)memset(a,b,sizeof(a))
#define ll long long
int n,m,dp[1<<12][maxn],mp[maxn][maxn];
int main(){
	int i,j,k;
	scanf("%d%d",&n,&m);
	mem(dp,0);mem(mp,0);
	int u,v,x=1<<n;
	for(i=0;i<m;i++){
		scanf("%d%d",&u,&v);
		mp[u][v]=1;
	}
	dp[1][1]=1;
	for(i=1;i<x;i++){
		for(v=1;v<=n;v++){
			if(!(i&(1<<(v-1)))) continue;
			for(u=1;u<=n;u++){
				if(!(i&(1<<(u-1)))||v==u) continue;
				dp[i][v]+=dp[i^(1<<(v-1))][u]*mp[u][v];
			}
		}
	}
	int ans=0;
	for(i=1;i<=n;i++){
		ans+=dp[(1<<n)-1][i]*mp[i][1];
	}
	printf("%d\n",ans);
	return 0;
}

由于点的范围比较小,那么我们直接搜索也是可以的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值