CF11 D. A Simple Task


题目传送门:https://codeforces.com/problemset/problem/11/D

题目大意:
给定一个简单无向图,求图中简单环的个数(\(n\leqslant 19\)


\(n\)?很小的话,考虑状压

\(F[S][i]\)表示点集\(S\)中遍历到\(i\)的方案数

转移的话,我们枚举下一个点\(j\),如果\(j\notin S\),则有\(F[S|2^j][j]+=F[S][i]\);如果\(j\in S\),则成环,有\(Ans+=F[S][i]\)

但这样存在一个问题,由于起点不固定,每个大小为\(Size\)的环都会被算\(Size\)

所以我们可以固定点集\(S\)中,编号最小的点为起点,这样每个环仅会被算两次(顺时针逆时针)

此外,由于没有记录边是否被使用过,因此两点(有边相连)也会被算入答案。又因为两点不存在顺逆时针差异,故仅会被算一次

综上所述,答案为\(\frac{Ans-m}{2}\)

/*program from Wolfycz*/
#include
#include
#include
#include
#include
#include
#include
#define Fi first
#define Se second
#define ll_inf 1e18
#define MK make_pair
#define sqr(x) ((x)*(x))
#define pii pair
#define int_inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline char gc(){
	static char buf[1000000],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
templateinline T frd(T x){
	int f=1; char ch=gc();
	for (;ch<'0'||ch>'9';ch=gc())	if (ch=='-')    f=-1;
	for (;ch>='0'&&ch<='9';ch=gc())	x=(x<<1)+(x<<3)+ch-'0';
	return x*f;
}
templateinline T read(T x){
	int f=1; char ch=getchar();
	for (;ch<'0'||ch>'9';ch=getchar())	if (ch=='-')	f=-1;
	for (;ch>='0'&&ch<='9';ch=getchar())	x=(x<<1)+(x<<3)+ch-'0';
	return x*f;
}
inline void print(int x){
	if (x<0)	putchar('-'),x=-x;
	if (x>9)	print(x/10);
	putchar(x%10+'0');
}
const int N=20;
bool Map[N+10][N+10];
ll F[(1<>1);
	return 0;
}

CF

相关