hdu 4997 状压dp

本文详细介绍了求解HDU 4997问题的解决方案,使用了状态压缩动态规划(DP)的方法。通过状态计算、邻接矩阵表示图并进行遍历,最终得出答案。代码中包含了解决该问题的关键算法实现,如`calc`函数,以及相关的辅助函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*************************************************************************
 > File Name: hdu4997.cpp
 > Author: TechMonster
 > Mail: 928221136@qq.com
 > Created Time: 六  7/ 9 12:15:10 2016
 ************************************************************************/

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<string>
#include<math.h>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
#define ls (o<<1)
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x, y) memcpy(x, y, sizeof(x))
typedef long long LL;
const int N = 1030;
const int M = 1e9+7;

int mp[10][10];
int e[N],con[N],meth[N][N],bico[N];
int n,m;
int getlow(int &sta)
{
	for(int i = 0; i < n; ++i)
		if(sta & 1<<i) return i;
	return -1;
}
void calc(int &sta)
{
	int low = getlow(sta);
	e[sta] = e[sta ^ 1<<low];
	for(int i = 0; i < n; ++i)
		if(!mp[i][low] && sta & 1<<i) e[sta]++;
	
	con[sta] = (1ll<<e[sta])%M;
	int rs = sta ^ 1<<low,ns;
	if(rs) for(int i = rs-1&rs;; i = i-1&rs)
	{
		ns = i ^ 1<<low;
		con[sta] -= (LL)con[ns]*((1ll<<e[sta^ns])%M)%M;
		con[sta] %= M;
		if(!i) break;
	}
	
	meth[0][sta] = 1;
	for(int s = sta-1&sta; s; s = s-1&sta)
	{
		int &t = meth[s][sta^s] = 0;
		rs = s-(s&-s);
		for(int i = rs;; i = i-1&rs)
		{
			ns = i^(s&-s);
			t += (LL)meth[s^ns][sta^s]*con[ns]%M*(e[sta^ns^s]-e[ns]-e[sta^s])%M;
			t %= M;
			if(!i) break;
		}
	}
	
	bico[sta] = con[sta];
	rs = sta ^ 1<<low;
	if(rs) for(int i = rs-1&rs;; i = i-1&rs)
	{
		ns = i ^ 1<<low;
		bico[sta] -= (LL)bico[ns]*meth[sta^ns][ns]%M;
		bico[sta] %= M;
		if(!i) break;
	}
}
void solve()
{
	int u,v;
	scanf("%d%d",&n,&m);
	MS(mp,0);
	for(int i = 0; i < n; ++i) mp[i][i] = 1;
	for(int i = 0; i < m; ++i)
	{
		scanf("%d%d",&u,&v);
		u--,v--;
		mp[u][v] = mp[v][u] = 1;
	}
	
	int top = 1<<n;
	for(int i = 1; i < top; ++i)
		calc(i);//printf("%d:%d %d %d\n",i,e[i],con[i],bico[i]);
	printf("%d\n",(bico[top-1]%M+M)%M);
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
		solve();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值