比赛 2025.2.25 评测结果 WWWWWWWWWWWWWWWWWWWWWWWWW
题目名称 填数游戏 最终得分 0
用户昵称 flyfreem 运行时间 2.704 s
代码语言 C++ 内存使用 13.87 MiB
提交时间 2025-02-25 09:26:13
显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MAXN 1000010
// By flyfreemrn
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=x*10+c-'0';
		c=getchar();
	}
	return x*f;
}
inline void write(ll x){
	if(x>9)write(x/10);
	putchar(x%10+'0');
}
ll dp[15][1<<11],rch[1<<11];
ll sizA[MAXN],A[MAXN][3];
ll sizB[MAXN],B[MAXN][3];
ll n,m;
ll solve(ll pos,ll res){
//	cout<<pos<<" "<<res<<endl;
	if(rch[res])return 1e9;
	else if(dp[pos][res])return dp[pos][res];
	else if(pos>n)return 0;
	for(int i=1;i<=sizA[pos];i++){
		ll Anow=A[pos][i];
		ll ans=1e9;
		for(int j=1;j<=sizB[pos];j++){
			ll Bnow=B[pos][j];
			if(((1<<Bnow-1)&res)==0)continue;
			ans=min(ans,solve(pos+1,res^(1<<Bnow-1))+(Anow==Bnow));
		}
		if(ans==1e9){
			rch[res]=1;
			return 1e9;
		}
		dp[pos][res]=max(dp[pos][res],ans);
	}
//	cout<<pos<<" "<<res<<" "<<dp[pos][res]<<endl;
	return dp[pos][res];
}
ll T;
queue<ll> q;
map <ll,bool> mp;
void clear(){
	while(!q.empty()){
		ll now=q.front();
		q.pop();
		mp[now]=false;
	}
	return;
}
void work1(){
	ll ans=0;
	for(int i=1;i<=n;i++){
		if(mp[B[i][1]]){
			cout<<"-1\n";
			clear();
			return;
		}
		mp[B[i][1]]=true;
		q.push(B[i][1]);
		if(A[i][1]==B[i][1]||A[i][2]==B[i][1])ans++;
	}
	clear();
	cout<<ans<<endl;
	return;
}
void work(){
	ll type=1;
	n=read(),m=read();
	for(int i=1;i<=n;i++){
		sizA[i]=read();
		for(int j=1;j<=sizA[i];j++){
			A[i][j]=read();
		}
	}
	for(int i=1;i<=n;i++){
		sizB[i]=read();
		for(int j=1;j<=sizB[i];j++){
			B[i][j]=read();
		}
		if(sizB[i]==2)type=0;
	}
	if(type==1){
		work1();
		return;
	}
	if(n>10){
		cout<<"0\n";
		return;
	}
	solve(1,(1<<m)-1);
//	for(int i=1;i<(1<<m);i++)cout<<i<<" "<<rch[i]<<"\n";
//	cout<<endl;
	if(rch[(1<<m)-1])cout<<"-1\n";
	else cout<<dp[1][(1<<m)-1]<<endl;
	memset(dp,0,sizeof(dp));
	memset(rch,0,sizeof(rch));
}
int main(){
	freopen("game.in","r",stdin);
	freopen("game.out","w",stdout);
	T=read();
	while(T--)work();
	return 0;
}
/*
9 10
2 8 2
1 2
2 6 7
2 6 5
1 3
2 6 3
1 4
2 8 7
1 2

2 8 9
2 2 1
2 7 6
2 5 6
2 4 3
2 2 3
2 5 4
2 8 7
2 10 8
*/