D. A Simple Task

Problem - D - Codeforces

思路:这个题就是求环的数量,通过数据范围的大小,我们可以想到用状压dp来做,因为只有19个点,我们可以将环的路径进行状态压缩,用一个二进制数表示环,当某一位为1时表示这个点在环上,那么我们可以用f[i][j]表示当前的路径状态为i,当前所在的点为j的情况,为了让环不重复统计,那么我们可以将路径中最小的点作为起点,可以保证不重复,那么我们只需要将状态从小到大枚举,这样一定会枚举到所有的情况,因为i一定是变大的不会变小,然后再枚举现在再哪个位置了,再枚举接下来要走到哪个点,那么如果当前的位置或者要到的位置比起点的编号小了,那么就跳过,并且如果从当前位置不能够走到下一个位置也跳过,并且我们再更新i的过程中要保证路径i没有环,因为如果存在环会重复统计。       

// Problem: D. A Simple Task
// Contest: Codeforces - Codeforces Beta Round 11
// URL: https://codeforces.com/contest/11/problem/D
// Memory Limit: 256 MB
// Time Limit: 2000 ms

#include<bits/stdc++.h>
#include<sstream>
#include<cassert>
#define fi first
#define se second
#define i128 __int128
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int,int> PII;
const double eps=1e-7;
const int N=5e5+7 ,M=5e5+7, INF=0x3f3f3f3f,mod=1e9+7,mod1=998244353;
const long long int llINF=0x3f3f3f3f3f3f3f3f;
inline ll read() {ll x=0,f=1;char c=getchar();while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();}
while(c>='0'&&c<='9') {x=(ll)x*10+c-'0';c=getchar();} return x*f;}
inline void write(ll x) {if(x < 0) {putchar('-'); x = -x;}if(x >= 10) write(x / 10);putchar(x % 10 + '0');}
inline void write(ll x,char ch) {write(x);putchar(ch);}
void stin() {freopen("in_put.txt","r",stdin);freopen("my_out_put.txt","w",stdout);}
bool cmp0(int a,int b) {return a>b;}
template<typename T> T gcd(T a,T b) {return b==0?a:gcd(b,a%b);}
template<typename T> T lcm(T a,T b) {return a*b/gcd(a,b);}
void hack() {printf("\n----------------------------------\n");}

int T,hackT;
int n,m,k;
bool st[30][30];
ll f[1<<20][20];

int lowbit(int x) {
	return x&-x;
}

void solve() {
	n=read(),m=read();
	
	for(int i=1;i<=m;i++) {
		int a=read(),b=read();
		st[a-1][b-1]=st[b-1][a-1]=true;
	}	

	for(int i=0;i<n;i++) f[1<<i][i]=1;
	
	ll res=0;
	for(int i=0;i<(1<<n);i++) {
		int start=lowbit(i);
		for(int j=0;j<n;j++) {
			if((1<<j)<start) continue;
			for(int k=0;k<n;k++) {
				if((1<<k)<start) continue;
				if(!st[j][k]) continue;
				
				if(start==(1<<k)) {
					res+=f[i][j];
				}else if(!((i>>k)&1)){
					f[i|(1<<k)][k]+=f[i][j];
				}
			}
		}
	}
	
	res=(res-m)/2;
	
	printf("%lld\n",res);
}

int main() {
    // init();
    // stin();
	// ios::sync_with_stdio(false); 

    // scanf("%d",&T);
    T=1; 
    while(T--) hackT++,solve();
    
    return 0;       
}          

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值