CodeForces - 868C Codeforces Round #438 (Div. 1 + Div. 2 ) C題

Qualification Rounds

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”).


這道題思維還是比較難想的;

首先可以發現,k<=4,這說明最多有24種01排列組合;再觀察會發現,只要找到2組01排列組合,它們在相同位的值是不一樣的,就一定存在;如果沒有找到,那麼就肯定不存在;

可以把01排列轉化爲10進制數,然後直接&判斷是否爲0就行;

#include<bits/stdc++.h>
#define ll long long
#define pa pair<int,int>
#define lson k<<1
#define rson k<<1|1
#define inf 0x3f3f3f3f
//ios::sync_with_stdio(false);
using namespace std;
const int N=200100;
const int M=1000100;
const ll mod=1e9+7;
bool vis[20];
int main(){
	ios::sync_with_stdio(false);
	int n,k;
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		int a=0;
		for(int j=1;j<=k;j++){
			int x;
			cin>>x;
			a*=2;
			a+=x;		
		}
		vis[a]=true;
	}
	int ok=0;
	for(int i=0;i<=(1<<k);i++){
		if(!vis[i]) continue;
		for(int j=0;j<=(1<<k);j++){
			if(!vis[j]||(i&j)) continue;
			ok=1;
			break;
		}
	}
	if(ok) cout<<"YES"<<endl;
	else cout<<"NO"<<endl;
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章