二分图匹配:Matrix Transformer

Matrix Transformer Time Limit: 2 Seconds      Memory Limit: 65536 KB

Alice and Bob meet again. This time they play a game named MATRIX TRANSFORMER.

They got an n * n board. Every grid has two positions, UP and DOWN. In this game you can push some amazing buttons to exchange any two rows or any two columns. Alice will win if she got the grids in the main diagonal line all UP.

But Alice finds that for some board, no matter how many times she tries, she cannot get the grids in the main diagonal line all UP. Now she asks you for help, tell her if she can win this board or not.

Input

There are several test cases.
For each test case:
The 1st line contains 1 integer n, indicating the size of the board. (1 ≤ n ≤ 200)
The next n lines, each contains n characters. 'U' indicates the position UP, and 'D' indicates the position DOWN.
There is no separation line between any two test cases.

Output

For each test case, you should print one line.You should print 'YES' if Alice can win, print 'NO' if not.

Sample Input

3
DUD
UDD
DDU
3
DUD
DUD
UDD

Sample Output

YES
NO


题目大意:能否通过矩阵的行交换列交换让一条对角线全是 U

#include<iostream>
#include<cstdio>
#include<map>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<set>
#define inf 110000000
#define M 10005
#define N 100005
#define Min(a,b) ((a)<(b)?(a):(b))
#define Max(a,b) ((a)>(b)?(a):(b))
#define LL long long

using namespace std;

const int MAXN = 440;
int uN,vN;  //u,vÊýÄ¿
int g[MAXN][MAXN];//±àºÅÊÇ0~n-1µÄ
int linker[MAXN];
bool used[MAXN];
bool dfs(int u){
    int v;
    for(v=0;v<=vN;v++)
        if(g[u][v]&&!used[v]){
            used[v]=true;
            if(linker[v]==-1||dfs(linker[v])){
                linker[v]=u;
                return true;
            }
        }
    return false;
}

int hungary(){
    int res=0;
    int u;
    memset(linker,-1,sizeof(linker));
    for(u=0;u<=uN;u++){
        memset(used,0,sizeof(used));
        if(dfs(u))  res++;
    }
    return res;
}

int main(){
    int n;
    while(~scanf("%d", &n)){
        int i, j, k;
        char ch[210];
        uN = n - 1;
        vN = n - 1;
        memset(g, 0, sizeof(g));
        for(i = 0; i < n; i ++){
            scanf("%s", ch);
            for(j = 0; j < n; j ++){
                if(ch[j] == 'U')
                    g[i][j] = 1;
            }
        }
        //printf("ans = %d\n", hungary());
        if(hungary() == n){
            printf("YES\n");
        }
        else
            printf("NO\n");
    }
    return 0;
}



你可能感兴趣的:(Integer,input,each,Exchange,Matrix,linker)