比赛 4043级2023省选模拟赛3 评测结果 WWWWWWWWWW
题目名称 矩阵游戏 最终得分 0
用户昵称 yrtiop 运行时间 2.376 s
代码语言 C++ 内存使用 5.76 MiB
提交时间 2023-03-24 19:57:47
显示代码纯文本
#include <bits/stdc++.h>
#define pb emplace_back

const int maxn = 205;

int n,cov[maxn],cnt = 0;
std::vector<int> lns1[maxn],lns2[maxn];
std::vector<int> a[maxn];
std::set<int> G;
std::mt19937 rd((unsigned)time(0));
bool used[maxn],usd[maxn];

void SWAP(int j1,int j2) {
	for(int i = 1;i <= n;++ i)
		std::swap(a[i][j1] , a[i][j2]);
	return ;
}

int calc() {
	int ans = 0;
	for(int i = 1;i <= n;++ i)
		ans += a[i][i];
	return ans;
}

void work() {
	scanf("%d",&n);
	for(int i = 1;i <= n;++ i) {
		a[i].resize(n + 1 , 0);
		for(int j = 1;j <= n;++ j)
			scanf("%d",&a[i][j]),cnt += a[i][j];
	}
	if(cnt < n)
		return puts("No"),void();
	for(int i = 1;i <= n;++ i) {
		int cur = 0;
		for(int j = 1;j <= n;++ j)
			cur += !a[i][j];
		if(cur == n)
			return puts("No"),void();
	}
	if(cnt >= n * 30)
		return puts("Yes"),void();
	for(int i = 1;i <= n;++ i)
		cov[i] = 0,lns1[i].clear(),lns2[i].clear(),used[i] = usd[i] = false;
	cnt = 0;
	for(int i = 1;i <= n;++ i) {
		bool flag = false;
		for(int j = 1;j <= n;++ j) {
			if(a[i][j]&&!cov[j]&&!used[j]) {
				std::swap(a[i] , a[j]);
				++ cnt;
				cov[j] = true;
				used[j] = true;
				break ;
			}
		}
	}
	for(int i = 1;i <= n;++ i)
		if(!used[i])
			G.insert(i);
	for(int i = 1;i <= n;++ i)
		for(int j = 1;j <= n;++ j)
			if(a[i][j])
				lns1[i].pb(j),lns2[j].pb(i);
	if(cnt >= n)
		return puts("Yes"),void();
	double st = clock();
	while(cnt < n&&(clock() - st) / CLOCKS_PER_SEC < 0.043) {
		int u = rd() % n + 1,v = rd() % n + 1;
		if(rd() & 1) {
			SWAP(u , v);
			int now = calc();
			if(now > cnt)
				cnt = now;
			else
				SWAP(u , v);
		}
		else {
			std::swap(a[u] , a[v]);
			int now = calc();
			if(now > cnt)
				cnt = now;
			else
				std::swap(a[u] , a[v]); 
		}
	}
	if(cnt >= n)
		puts("Yes");
	else {
		int ans = 0,p = 0;
		for(int i = 1;i <= n;++ i) {
			int cnt = 0;
			for(int j = 1;j <= n;++ j)
				cnt += a[i][j];
			ans += cnt >= n / 3;
		}
		if(ans >= n / 3 * 2)
			puts("Yes");
		else
			puts("No");
	}
	return ;
}

int main() {
	freopen("qmatrix.in","r",stdin);
	freopen("qmatrix.out","w",stdout);
	int T;
	scanf("%d",&T);
	while(T --)
		work();
	return 0;
}