poj 2009

19 篇文章 0 订阅

留给将来成为大佬的我

#include <iostream>
#include <cstdio>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
#include <stack>

#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define ll long long
#define ull unsigned long long
#define uint unsigned int

using namespace std;

#define MAXN 111117
int dfs(int u);
int hhash[MAXN];
int c, t, k;
int cy[MAXN], mk[MAXN];
int cnts[1100], cw[1100][40], piz[1100];

int query(int k){
	int p;
	p = k % MAXN;
	while (hhash[p] != -1 && hhash[p] != k) 
		p = (p + 10) % MAXN;
	return p;
}

bool hashit(int k){
	int p;
	p = k % MAXN;
	while (hhash[p] != -1 && hhash[p] != k) 
		p = (p + 10) % MAXN;
	if (hhash[p] == -1)
		hhash[p] = k;
	else
		return false;

	return true;
}

int fail[MAXN], tp;

int coordinate(int u, int cnt, int id, int pz){
	if (cnts[u] - id + cnt < k)
		return 0;
	if (cnt == k){
		int p = query(pz);
		if (mk[p] != -1)
			return 0;
		int x = cy[p];
		mk[p] = 1;
		if (dfs(x)){
			cy[p] = u;
			return 1;
		}
		return 0;
	}
	else{
		if (coordinate(u, cnt, id + 1, pz))
			return 1;
		else {
			pz |= (1 << (cw[u][id] - 1));
			if (coordinate(u, cnt + 1, id + 1, pz))
				return 1;
			return 0;
		}
	}
	return 0;
}

int newPiz(int u, int cnt, int id, int pz){
	if (cnts[u] - id + cnt < k) return 0;
	if (cnt == k){
		if (!hashit(pz)) 
			return 0;
		int p = query(pz);
		cy[p] = u;
		return 1;
	}
	else{
		if (newPiz(u, cnt, id + 1, pz))
			return 1;
		else{
			pz |= (1 << (cw[u][id] - 1));
			if (newPiz(u, cnt + 1, id + 1, pz)) 
				return 1;
			return 0;
		}
	}
	return 0;
}

int dfs(int u){
	if (cnts[u] < k) 
		return 0;
	for (int i = 0; i < tp; i++)
		if ((piz[u] & (~fail[i])) == 0) 
			return 0;

	if (newPiz(u, 0, 0, 0))
		return 1;
	else if (coordinate(u, 0, 0, 0)) 
		return 1;
	else 
		fail[tp++] = piz[u];
	return 0;
}

int main(){
	while (~scanf("%d%d%d", &c, &t, &k)){
		for (int i = 1; i <= c; i++){
			piz[i] = 0;
			scanf("%d", &cnts[i]);
			for (int j = 0; j < cnts[i]; j++) {
				scanf("%d", &cw[i][j]);
				piz[i] |= (1 << (cw[i][j] - 1));
			}
		}
		int ans = 0;
		tp = 0;
		memset(hhash, -1, sizeof(hhash));
		memset(cy, -1, sizeof(cy));
		for (int i = 1; i <= c; i++) {
			memset(mk, -1, sizeof(mk));
			if (dfs(i))
				ans++;
		}
		printf("%d\n", ans);
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值