Bzoj 1059: [zjoi2007] matrix game Bipartite Graph Matching

Source: Internet
Author: User
1059: [zjoi2007] matrix game time limit: 10 sec memory limit: 162 MB
Submit: 1891 solved: 919
[Submit] [Status] Description

Q is a very smart child. In addition to chess, he also enjoys playing a computer puzzle game, matrix game. Matrix games are played in an N * n black/white matrix (just like playing chess, the color is random ). You can perform two operations on the matrix each time: Row exchange operation: select any two rows of the matrix and exchange the two rows (that is, swap the color of the corresponding grid) column exchange operation: select any row and column of the Matrix, and exchange the two columns (that is, swap the color of the corresponding grid) to the game's target, that is, through several operations, the main diagonal line of the matrix (the line from the upper left to the lower right corner) the grid on is black. Q cannot solve certain levels, so that he began to doubt whether these levels are completely unsolvable !! Therefore, Xiao Q decided to write a program to determine whether these levels were resolved.

Input

The first line contains an integer T, indicating the number of data groups. Next, the data contains t groups. The first behavior of each group is an integer N, indicating the size of the square matrix. The next n behavior is an 01 matrix of N * n (0 indicates white, and 1 indicates black ).

Output

The output file should contain t rows. For each group of data, if the level is resolved, a row of yes is output; otherwise, a row of no is output.

Sample input2
2
0 0
0 1
3
0 0 1
0 1 0
1 0 0
Sample outputno
Yes
[Data scale]
For 20% of the data, n ≤ 7
For 50% of the data, n ≤ 50
For 100% of data, n ≤ 200
The hint Hungary algorithm is a bit forgotten. This kind of board conversion problem does not have to go to the target board. You just need to find the rule.
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<vector>using namespace std;#define MAXN 300vector<int> V[MAXN];int vis[MAXN];int pat[MAXN];int find(int x){    int i;    for (i=0;i<V[x].size();i++)    {        if (vis[V[x][i]])continue;        vis[V[x][i]]=true;        if (pat[V[x][i]]==-1||find(pat[V[x][i]]))        {            pat[V[x][i]]=x;            return 1;        }    }    return 0;}int main(){    freopen("input.txt","r",stdin);    //freopen("output.txt","w",stdout);    int nn,n,m;    int i,j,k;    int x;    int tot;    scanf("%d",&nn);    while (nn--)    {        scanf("%d",&n);        for (i=0;i<n;i++)        {            for (j=0;j<n;j++)            {                scanf("%d",&x);                if (x)V[i].push_back(j);            }        }        tot=0;        memset(pat,-1,sizeof(pat));        for (i=0;i<n;i++)        {            memset(vis,0,sizeof(vis));            tot+=find(i);        }        if (tot==n)        {            printf("Yes\n");        }else        {            printf("No\n");        }        for(i=0;i<n;i++)V[i].clear();    }}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.