LightOJ - 1261 K-SAT Problem
Time Limit: 2000MS | Memory Limit: 32768KB | 64bit IO Format: %lld & %llu |
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 n, m, K (1 ≤ n, m, K ≤ 30). Each of the next n lines contains K space separated integers where the ith 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 and m.
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 and m, 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
Source
Problem Setter: Jane Alam Jan感觉别人的代码真是顺畅!!
#include <cstdio> #include <cstring> using namespace std; int vis[110]; int a[33][33]; int main() { int t, cas=1; scanf("%d", &t); while(t--) { int n, m, k; scanf("%d%d%d", &n, &m, &k); for(int i=1; i<=n; i++) for(int j=1; j<= k; j++) { scanf("%d", &a[i][j]); } int p; scanf("%d", &p); memset(vis, 0, sizeof(vis)); for(int i=0; i< p; i++) { int x; scanf("%d", &x); vis[x]=1; } int j; int flag =1; for(int i=1; i<= n; i++) { for(j=1; j<=k; j++) { if(a[i][j]> 0) { if(vis[a[i][j]]==1) break; } else if(a[i][j]<0) { if(vis[-a[i][j]]== 0) break; } } if(j >k) { flag=0; break; } } printf("Case %d: ", cas++); if(flag==0) printf("No\n"); else printf("Yes\n"); } return 0; }
转载于:https://www.cnblogs.com/soTired/p/5451272.html
- Lightoj1261——1261 - K-SAT Problem(模拟)
- lightOj--1261--K-SAT Problem(STL)
- LightOJ - 1261 K-SAT Problem (模拟)
- Light oj 1261 - K-SAT Problem【模拟】
- light oj 1261 - K-SAT Problem (模拟)
- Codeforces Round #400 (Div. 1 + Div. 2, combined)D. The Door Problem【2-sat Tarjan+思维建图】
- 每日刷题:lightoj-1004 - Monkey Banana Problem
- hihoCoder 1261 String Problem II
- lightoj 1179 - Josephus Problem 约瑟夫环
- 【2-SAT】【并查集】ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem
- lightoj 1102 - Problem Makes Problem (组合+逆元)
- LightOJ 1004 - Monkey Banana Problem (dp)
- LightOJ - 1407 Explosion(2-SAT)
- LightOJ 1004 Monkey Banana Problem 动态规划
- LightOJ 1004 - Monkey Banana Problem(dp)
- 数学-F Problem Makes Problem (LightOJ - 1102 )
- Codeforces Round #400 (Div. 1 + Div. 2, combined)D - The Door Problem(2-sat)
- LightOj 1179 Josephus Problem
- LightOJ 1004 Monkey Banana Problem
- hihoCoder挑战赛17 B String Problem II hihoCoder 1261