BZOJ 1059. [ZJOI2007]矩阵游戏

 

就是二分图匹配,直接匈牙利即可。

#include <bits/stdc++.h>

const int N = 220;
bool used[N];
int hascow[N], n, link[N][N];

bool find(int x) {
    for (int i = 1; i <= n; i++) {
        if (link[x][i] && !used[i]) {
            used[i] = 1;
            if (!hascow[i] || find(hascow[i])) {
                hascow[i] = x;
                return 1;
            }
        }
    }
    return 0;
}

int main() {
    int T;
    scanf("%d", &T);
    while (T--) {
        scanf("%d", &n);
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++)
                scanf("%d", &link[i][j]);
            hascow[i] = 0;
        }
        int ans = 0;
        for (int i = 1; i <= n; i++) {
            memset(used, 0, sizeof(used));
            ans += find(i);
        }
        if (ans == n) puts("Yes");
        else puts("No");
    }
    return 0;
}
View Code

 

posted @ 2020-01-25 20:45  Mrzdtz220  阅读(83)  评论(0编辑  收藏  举报