CF906C Party (状压DP)(未完成//鬼知道啥情况)

CF上下了数据,本地能过,一交就错
这状压不用解释,看代码就懂。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int  a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int  a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long
 
#define ON_DEBUG
 
#ifdef ON_DEBUG
 
#define D_e_Line printf("\n\n----------\n\n")
#define D_e(x)  cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin);
 
#else
 
#define D_e_Line ;
#define D_e(x)  ;
#define Pause() ;
#define FileOpen() ;
 
#endif
 
struct ios{
    template<typename ATP>ios& operator >> (ATP &x){
        x = 0; int f = 1; char c;
        for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-')  f = -1;
        while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
        x*= f;
        return *this;
    }
}io;
using namespace std;
 
const int N = 23;
 
int f[1 << N];
int a[1 << N];
int q[N + 7];
int fa[1 << N];
 
int main(){
//FileOpen();
 
	int n, m;
	io >> n >> m;
	
	if(n == 1 || (n * (n - 1) >> 1) <= m){
		printf("0");
		return 0;
	}
	
	int M = (1 << n) - 1;
	R(i,0,M) f[i] = 0x3f3f3f3f; // can't memset f[] 0x7fffffff directly
	
	R(i,1,m){
		int u, v;
		io >> u >> v;
		a[u] |= (1 << (v - 1));
		a[v] |= (1 << (u - 1));
	}
	R(i,1,n){
		a[i] |= (1 << (i - 1));
		f[a[i]] = 1;
		q[a[i]] = i;
	}
	R(s,0,M){
		if(f[s] == 0x3f3f3f3f) continue;
		R(i,1,n){
			if(((s >> (i - 1)) & 1) && (f[s | a[i]] > (f[s] + 1))){
				f[s | a[i]] = f[s] + 1;
				q[s | a[i]] = i;
				fa[s | a[i]] = s;
				
			}
		}
	}
	
	printf("%d\n", f[M]);
	for(register int i = M; i; i = fa[i]){
		printf("%d ", q[i]);
	}
	return 0;
}

posted @ 2019-07-25 21:22  邱涵的秘密基地  阅读(233)  评论(0编辑  收藏  举报