Educational Codeforces Round 115 (Rated for Div. 2) B. Groups

题目链接:Problem - 1598B - Codeforces

n students attended the first meeting of the Berland SU programming course (nn is even). All students will be divided into two groups. Each group will be attending exactly one lesson each week during one of the five working days (Monday, Tuesday, Wednesday, Thursday and Friday), and the days chosen for the groups must be different. Furthermore, both groups should contain the same number of students.

Each student has filled a survey in which they told which days of the week are convenient for them to attend a lesson, and which are not.

Your task is to determine if it is possible to choose two different week days to schedule the lessons for the group (the first group will attend the lesson on the first chosen day, the second group will attend the lesson on the second chosen day), and divide the students into two groups, so the groups have equal sizes, and for each student, the chosen lesson day for their group is convenient.

Input

The first line contains a single integer tt (1≤t≤10^4) — the number of testcases.

Then the descriptions of tt testcases follow.

The first line of each testcase contains one integer nn (2≤n≤1000) — the number of students.

The i-th of the next nn lines contains 5 integers, each of them is 0 or 1. If the j-th integer is 1, then the i-th student can attend the lessons on the j-th day of the week. If the j-th integer is 0, then the i-th student cannot attend the lessons on the j-th day of the week.

Additional constraints on the input: for each student, at least one of the days of the week is convenient, the total number of students over all testcases doesn't exceed 10^5.

Output

For each testcase print an answer. If it's possible to divide the students into two groups of equal sizes and choose different days for the groups so each student can attend the lesson in the chosen day of their group, print "YES" (without quotes). Otherwise, print "NO" (without quotes).

Example

input

Copy

2
4
1 0 0 1 0
0 1 0 0 1
0 0 0 1 0
0 1 0 1 0
2
0 0 0 1 0
0 0 0 1 0

output

Copy

YES
NO

Note

In the first testcase, there is a way to meet all the constraints. For example, the first group can consist of the first and the third students, they will attend the lessons on Thursday (the fourth day); the second group can consist of the second and the fourth students, and they will attend the lessons on Tuesday (the second day).

In the second testcase, it is impossible to divide the students into groups so they attend the lessons on different days.

题意:给定n个人(n一定是偶数),然后每行5个数0,表示没空,1表示有空,问能否分成相同数量的两组在不同的一天都有空

思路:遍历5天找到有空人数大于等于总人数一半的天数,在求出两天都有空的人;如果这两天都有空的人数和减去都有空的人数等于n就输出YES,否则NO

#include<bits/stdc++.h>
using namespace std;

int arr[1005][5]; 
vector<int> ve[5];

int main(){
	int t;
	int n;
	cin >> t;
	while(t--){
		cin >> n;
		for(int i = 0; i < 5; i++){
			ve[i].clear();
		}
		for(int i = 0; i < n; i++){
			for(int l = 0; l < 5; l++){
				cin >> arr[i][l];
				if(arr[i][l] == 1){
					ve[l].push_back(i);
				} 
			}
		}
		bool flag = 0;
		for(int i = 0; i < 5; i++){
			for(int l = i + 1; l < 5; l++){
				if(ve[i].size() >= n / 2 && ve[l].size() >= n / 2){
					int ans = 0;
					for(int j = 0; j < n; j++){
						if(arr[j][i] == 1 && arr[j][l] == 1){
							ans++;
						}
					}
					if(ve[i].size() + ve[l].size() - ans >= n){
						flag = 1;
						break;
					}
				}
			}
			if(flag){
				break;
			}
		}
		if(flag){
			cout << "YES" << endl;
		}else{
			cout << "NO" << endl;
		}
	}
	return 0;
} 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值