Luo Gu P1129 [ZJOI2007] matrix game

Portal

Again, the biggest problem the difficulty of matching in a bipartite graph you can see whether this is a bipartite graph matching problem and how to build on the map.

This question is difficult to see a bipartite graph maximum matching problem.

Think about it, you can swap any two lines, you can also swap any two.

For any two black boxes, if they have been on the same line, no matter how exchange, they will be in the same row (column empathy).

And we only need one line of that particular column of black gets the job done, there is no other I do not care (column empathy)

Is not that the matching of bipartite graph of the thing.

Therefore, direct or running on the network flow algorithm Hungary to complete a thing.

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define N 210
using namespace std;
int T,n,g[N][N],nxt[N],vis[N];

bool match(int u){
    for(int i=1;i<=n;i++)
        if(g[u][i]&&!vis[i]){
            vis[i]=1;
            if(!nxt[i]||match(nxt[i])){
                nxt[i]=u;
                return true;
            }
        }
    return false;
}

int main () {
    scanf("%d",&T);
    while(T--){
        memset(g,0,sizeof(g));
        memset(nxt,0,sizeof(nxt));
        int tot=0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) scanf("%d",&g[i][j]);
        for(int i=1;i<=n;i++) {
            memset(vis,0,sizeof(vis));
            all + = match (i);
        }
        if(tot==n) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/BakaCirno/p/11734064.html