BZOJ#1059 [ZJOI2007]矩阵游戏

[ZJOI2007]矩阵游戏

image

思路:

假设只考虑变换列,不会影响每一行得相对位置,那么要使得每一行都有一个1,就相当于每一个x有一个匹配的y,可以使用匈牙利算法

代码:

#include <bits/stdc++.h>
#define int long long
int _ = 0, Case = 1;
using namespace std;
#define all(v) begin(v),end(v)
#define nline '\n'

const int N = 220;
vector<int> h[N];
int a[N][N];
int match[N], st[N];
bool find(int u) {
    for (auto i : h[u]) {
        if (!st[i]) {
            st[i] = true;
            if (!match[i] or find(match[i])) {
                match[i] = u;
                return true;
            }
        }
    }
    return false;
}
void solve(int Case) {
    int n;
    cin >> n;
    for(int i=1;i<=n;i++) match[i]=0,h[i].clear();
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cin >> a[i][j];
            if (a[i][j]) {
                h[i].push_back(j);
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) st[j] = false;
        if (!find(i)) {
            cout << "No" << nline;
            return;
        }
    }
    cout << "Yes" << nline;
}

signed main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    cin >> _; for (Case = 1; Case <= _; Case++)
        solve(Case);

    return 0;
}
posted @ 2022-03-30 16:46  指引盗寇入太行  阅读(36)  评论(0编辑  收藏  举报