zoukankan      html  css  js  c++  java
  • bzoj1059 [ZJOI2007]矩阵游戏

    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

    正解:二分图完美匹配。

    我们可以把这题理解为,是否能找到n个行互不相同,列也互不相同的点。那么我们就可以以行和列为点,对于每个黑点,将行和列连一条边,如果这个二分图有完美匹配,那么就合法,否则就不合法。

     1 //It is made by wfj_2048~
     2 #include <algorithm>
     3 #include <iostream>
     4 #include <complex>
     5 #include <cstring>
     6 #include <cstdlib>
     7 #include <cstdio>
     8 #include <vector>
     9 #include <cmath>
    10 #include <queue>
    11 #include <stack>
    12 #include <map>
    13 #include <set>
    14 #define inf (1<<30)
    15 #define il inline
    16 #define RG register
    17 #define ll long long
    18 
    19 using namespace std;
    20 
    21 struct edge{ int nt,to; }g[40010];
    22 
    23 int head[210],vis[210],lk[210],n,num,cnt;
    24 
    25 il int gi(){
    26     RG int x=0,q=1; RG char ch=getchar(); while ((ch<'0' || ch>'9') && ch!='-') ch=getchar();
    27     if (ch=='-') q=-1,ch=getchar(); while (ch>='0' && ch<='9') x=x*10+ch-48,ch=getchar(); return q*x;
    28 }
    29 
    30 il void insert(RG int from,RG int to){ g[++num]=(edge){head[from],to},head[from]=num; return; }
    31 
    32 il int dfs(RG int x){
    33     for (RG int i=head[x];i;i=g[i].nt){
    34     RG int v=g[i].to; if (vis[v]==cnt) continue;
    35     vis[v]=cnt; if (!lk[v] || dfs(lk[v])){ lk[v]=x; return 1; }
    36     }
    37     return 0;
    38 }
    39 
    40 il int maxmatch(){ for (RG int i=1;i<=n;++i){ cnt++; if (!dfs(i)) return 0; } return 1; }
    41 
    42 il void work(){
    43     n=gi(),num=0; RG int x;
    44     memset(lk,0,sizeof(lk));
    45     memset(head,0,sizeof(head));
    46     for (RG int i=1;i<=n;++i)
    47     for (RG int j=1;j<=n;++j){
    48         x=gi(); if (x) insert(i,j);
    49     }
    50     if (maxmatch()) puts("Yes"); else puts("No"); return;
    51 }
    52 
    53 int main(){
    54     RG int T=gi();
    55     while (T--) work();
    56     return 0;
    57 }
  • 相关阅读:
    routine 程序;日常工作|日常的;例行的
    have great expectation of 寄予厚望
    数据库总结十完整性约束
    Spoken Language One
    Stature 身高,身材;(精神、道德等的)高度
    ultimate与estimate
    dramatically 从戏剧角度;戏剧性地,显著地
    predestined 注定的
    How to lists.
    endanger 危及;使遭到危险
  • 原文地址:https://www.cnblogs.com/wfj2048/p/6433263.html
Copyright © 2011-2022 走看看