$###Description
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.
Determine if Snark and Philip can make an interesting problemset! Input
The first line contains two integers n, k (1 ≤ n ≤ 105, 1 ≤ k ≤ 4) — the number of problems and the number of experienced teams.
Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise. Output
Print “YES” (quotes for clarity), if it is possible to make an interesting problemset, and “NO” otherwise.
You can print each character either upper- or lowercase (“YeS” and “yes” are valid when the answer is “YES”). Examples
input5 31 0 11 1 01 0 01 0 01 0 0outputNOinput3 21 01 10 1outputYES
題目大意
有n個試題,k個隊,1代表該對已經知道該題內容,0代表不知道。問從n個試題中任意抽取一部分,是否可以滿足每個隊最多隻能知道不超過一半的試題。 解題思路
將每個隊對於每份試題的瞭解情況看成一種狀態轉化為二進位,用整數儲存並標記出現次數。然後在 [0,(1<<k−1)] [0,(1遍曆,若該狀態存在並且與另一種狀態取與等於0,則這兩種狀態便可滿足題意,直接輸出”YES”。 代碼實現
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<vector>using namespace std;#define maxn 16int a[maxn];int main(){ int n,k; int t,mi,z; while(~scanf("%d %d",&n,&k)) { memset(a,0,sizeof(a)); for(int i=0; i<n; i++) { t=0,mi=1<<(k-1); for(int j=0; j<k; j++) { scanf("%d",&z); t+=z*mi; mi/=2; } a[t]++; } bool flag=false; for(int i=0; i<(1<<k); i++) { if(a[i]!=0) { for(int j=0; j<(1<<k); j++) { if(a[j]!=0&&((i&j)==0)) { flag=true; printf("YES\n"); break; } } } if(flag) break; } if(!flag) printf("NO\n"); } return 0;}