记录编号 610624 评测结果 AAAAAAAAAA
题目名称 [ZJOI 2007] 矩阵游戏 最终得分 100
用户昵称 Gravatar终焉折枝 是否通过 通过
代码语言 C++ 运行时间 0.448 s
提交时间 2026-01-16 20:49:04 内存使用 3.77 MiB
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
const int N = 300;
vector<int> g[N];
bool vst[N];
int rmatch[N];
bool dfs(int u) {
    for (int i = 0; i < g[u].size(); i++) {
        int v = g[u][i];
        if (!vst[v]) {
            vst[v] = true;
            if(rmatch[v] == -1 || dfs(rmatch[v])){
                rmatch[v] = u;
                return true;
            }
        }
    }
    return false;
}
int hungary(int n) {
    int cnt = 0;
    memset(rmatch, -1, sizeof(rmatch));
    for (int i = 1; i <= n; ++i) {
        memset(vst, 0, sizeof(vst));
        cnt += dfs(i);
    }
    return cnt;
}
int main() {
    int T, n, x;
    cin >> T;
    while (T--) {
        cin >> n;
        for (int i = 1; i <= n; i++) g[i].clear();
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                cin >> x;
                if(x == 1){
                    g[i].push_back(j);
                }
            }
        }
        if(hungary(n) == n){
            cout << "Yes\n";
        }
        else{
            cout << "No\n";
        }
    }
    return 0;
}