[BZOJ1059&ZJOI2007] 矩阵游戏 (二分图匹配)

一行内如果没有被染色的块,那么就不可能通过行列变换使得满足结果

一列也是如此

那么只需要在每一行和每一列都有被染色的就可以了,这样就转换成了二分图完美匹配的问题

#include <bits/stdc++.h>
using namespace std;
const int maxn = 200+10;
int T, n;
int g[maxn][maxn];
int used[maxn];
int line[maxn][maxn];
int other_side[maxn];
bool Find(int x)
{
    for(int i = 1 ; i <= n ; i++)
    {
        if(line[x][i] && !used[i])
        {
            used[i] = 1;
            if(other_side[i] == 0 || Find(other_side[i]))
            {
                other_side[i] = x;
                return 1;
            }
        }
    }
    return 0;
}

void solve()
{
    int sum = 0;
    for(int i = 1 ; i <= n ; i++)
    {
        memset(used,0,sizeof(used));
        if(Find(i))
        {
            sum++;
        }
    }
    if(sum >= n) cout << "Yes" << endl;
    else cout << "No" << endl;
}

void init()
{
    memset(line,0,sizeof(line));
    memset(other_side,0,sizeof(other_side));
    memset(used,0,sizeof(used));
}
int main()
{
    ios::sync_with_stdio(false);
    cin >> T;
    while(T--)
    {
        init();
        cin >> n;
        for(int i = 1 ; i <= n ; i++)
        {
            for(int j = 1 ; j <= n ; j++)
            {
                cin >> line[i][j];
            }
        }
        solve();
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/zhaiqiming2010/article/details/80274828