博客
关于我
[bzoj1059][二分图匹配]矩阵游戏
阅读量:121 次
发布时间:2019-02-26

本文共 1644 字,大约阅读时间需要 5 分钟。

Description

  小Q是一个非常聪明的孩子,除了国际象棋,他还很喜欢玩一个电脑益智游戏——矩阵游戏。矩阵游戏在一个N

*N黑白方阵进行(如同国际象棋一般,只是颜色是随意的)。每次可以对该矩阵进行两种操作:行交换操作:选择 矩阵的任意两行,交换这两行(即交换对应格子的颜色)列交换操作:选择矩阵的任意行列,交换这两列(即交换
对应格子的颜色)游戏的目标,即通过若干次操作,使得方阵的主对角线(左上角到右下角的连线)上的格子均为黑
色。对于某些关卡,小Q百思不得其解,以致他开始怀疑这些关卡是不是根本就是无解的!!于是小Q决定写一个程 序来判断这些关卡是否有解。

Input

  第一行包含一个整数T,表示数据的组数。接下来包含T组数据,每组数据第一行为一个整数N,表示方阵的大

小;接下来N行为一个N*N的01矩阵(0表示白色,1表示黑色)。

Output

  输出文件应包含T行。对于每一组数据,如果该关卡有解,输出一行Yes;否则输出一行No。

Sample Input

2

2

0 0

0 1

3

0 0 1

0 1 0

1 0 0

Sample Output

No

Yes

【数据规模】

对于100%的数据,N ≤ 200

题解

好久之前模拟赛就见过类似的题了

结果那时候自以为是不理这种题。。然后bzoj上遇见了
那就好好学
可以发现 对于同一行,或者同一列的黑格子,无论怎样变换,他们都是在同一行或者同一列的
那么其实就是,对于全部行,能不能匹配完这张图的全部列
假如map[i][j]==1 那么表示第i行可以匹配第j列
跑二分图匹配

#include
#include
#include
#include
#include
using namespace std;bool chw[210],map[210][210];int match[210],n;bool find_muniu(int x){ for(int j=1;j<=n;j++) if(map[x][j] && chw[j]) { chw[j]=false; if(match[j]==0 || find_muniu(match[j])){match[j]=x;return true;} } return false;}int main(){ int T; scanf("%d",&T); while(T--) { scanf("%d",&n); memset(map,false,sizeof(map)); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { int x; scanf("%d",&x); if(x==1)map[i][j]=true; } int ans=0; memset(match,0,sizeof(match)); for(int i=1;i<=n;i++) { memset(chw,true,sizeof(chw)); if(find_muniu(i))ans++; else break; } if(ans==n)printf("Yes\n"); else printf("No\n"); } return 0;;}

转载地址:http://xdmu.baihongyu.com/

你可能感兴趣的文章
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named 'pandads'
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No static resource favicon.ico.
查看>>
no such file or directory AndroidManifest.xml
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>