lightOj--1261--K-SAT Problem(STL)


Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu

 Status

Description

SAT was the first known NP-complete problem. The problem remains NP-complete even if all expressions are written in conjunctive normal form with 3 variables per clause (3-CNF), yielding the 3-SAT problem. A K-SAT problem can be described as follows:

There are n persons, and m objects. Each person makes K wishes, for each of these wishes either he wants to take an object or he wants to reject an object. You have to take a subset of the objects such that every person is happy. A person is happy if at least one of his K wishes is kept. For example, there are 3 persons, 4 objects, and K = 2, and

Person 1 says, "take object 1 or reject 2."

Person 2 says, "take object 3 or 4."

Person 3 says, "reject object 3 or 1."

So, if we take object 1 2 3, then it is not a valid solution, since person 3 becomes unhappy. But if we take 1 2 4 then everyone becomes happy. If we take only 4, it's also a valid solution. Now you are given the information about the persons' wishes and the solution we are currently thinking. You have to say whether the solution is correct or not.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case starts with a line containing three integers nmK (1 ≤ n, m, K ≤ 30). Each of the next n lines contains K space separated integers where theith line denotes the wishes of the ith person. Each of the integers in a line will be either positive or negative. Positive means the person wants the object in the solution; negative means the person doesn't want that in the solution. You can assume that the absolute value of each of the integers will lie between 1 andm.

The next line contains an integer p (0 ≤ p ≤ m) denoting the number of integers in the solution, followed by p space separated integers each between 1 andm, denoting the solution. That means the objects we have taken as solution set.

Output

For each case, print the case number and 'Yes' if the solution is valid or 'No' otherwise.

Sample Input

2

3 4 2

+1 -2

+3 +4

-3 -1

1 4

1 5 3

+1 -2 +4

2 2 5

Sample Output

Case 1: Yes

Case 2: No

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
using namespace std;
int mp[50][50];
int vis[1010];
int p;
int n, m, k;
set<int>st;
bool js()
{
	for(int i = 1; i <= n; i++)
	{
		int flot  = 0;
		for(int j = 1; j <= k; j++)
		{
			if(st.count(mp[i][j]))
			{
				flot = 1;
				break;
			}
		}
		if(!flot)return false;
	}
	return true;
}
int main()
{
	int T, kase = 0;
	scanf("%d", &T);
	while(T--)
	{
		scanf("%d%d%d", &n, &m, &k);
		for(int i = 1; i <= n; i++)
		{
			for(int j = 1; j <= k; j++)
			{
				scanf("%d", &mp[i][j]);
			}
		}
		scanf("%d", &p);
		st.clear();
		memset(vis, 0, sizeof(vis));
		int x;
		for(int i = 1; i <= p; i++)
		{
			scanf("%d", &x);
			st.insert(x);
			vis[x] = 1;
		}
		for(int i = 1; i<= m; i++)
		{
			if(!vis[i])
				st.insert(-i);
		}
		if(js())
			printf("Case %d: Yes\n", ++kase);
		else
			printf("Case %d: No\n", ++kase);
	}
	return 0;
}

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下 4载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值