• BZOJ 1059 矩阵游戏 二分图匹配


    题目链接:

    https://www.lydsy.com/JudgeOnline/problem.php?id=1059

    题目大意:

    小Q是一个非常聪明的孩子,除了国际象棋,他还很喜欢玩一个电脑益智游戏——矩阵游戏。矩阵游戏在一个N
    *N黑白方阵进行(如同国际象棋一般,只是颜色是随意的)。每次可以对该矩阵进行两种操作:行交换操作:选择
    矩阵的任意两行,交换这两行(即交换对应格子的颜色)列交换操作:选择矩阵的任意行列,交换这两列(即交换
    对应格子的颜色)游戏的目标,即通过若干次操作,使得方阵的主对角线(左上角到右下角的连线)上的格子均为黑
    色。对于某些关卡,小Q百思不得其解,以致他开始怀疑这些关卡是不是根本就是无解的!!于是小Q决定写一个程
    序来判断这些关卡是否有解。
    思路:
    每一行必须匹配某一列才是有解的,所以可以二分图匹配来做。
     1 #include<bits/stdc++.h>
     2 #define IOS ios::sync_with_stdio(false);//不可再使用scanf printf
     3 #define Max(a, b) ((a) > (b) ? (a) : (b))//禁用于函数,会超时
     4 #define Min(a, b) ((a) < (b) ? (a) : (b))
     5 #define Mem(a) memset(a, 0, sizeof(a))
     6 #define Dis(x, y, x1, y1) ((x - x1) * (x - x1) + (y - y1) * (y - y1))
     7 #define MID(l, r) ((l) + ((r) - (l)) / 2)
     8 #define lson ((o)<<1)
     9 #define rson ((o)<<1|1)
    10 #pragma comment(linker, "/STACK:102400000,102400000")//栈外挂
    11 using namespace std;
    12 inline int read()
    13 {
    14     int x=0,f=1;char ch=getchar();
    15     while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
    16     while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    17     return x*f;
    18 }
    19 
    20 typedef long long ll;
    21 const int maxn = 500 + 10;
    22 const int MOD = 1000000007;//const引用更快,宏定义也更快
    23 const int INF = 1e9 + 7;
    24 const double eps = 1e-6;
    25 int Map[maxn][maxn];
    26 int cx[maxn], cy[maxn];
    27 bool vis[maxn];
    28 int n;
    29 bool dfs(int u)
    30 {
    31     for(int v = 1; v <= n; v++)
    32         if(Map[u][v] && !vis[v])
    33         {
    34             vis[v] = 1;
    35             if(cy[v] == -1 || dfs(cy[v]))
    36             {
    37                 cx[u] = v;
    38                 cy[v] = u;
    39                 return 1;
    40             }
    41         }
    42     return 0;
    43 }
    44 int maxmatch()
    45 {
    46     int ans = 0;
    47     memset(cx, -1, sizeof(cx));
    48     memset(cy, -1, sizeof(cy));
    49     for(int i = 1; i <= n; i++)
    50     {
    51         if(cx[i] == -1)
    52         {
    53             memset(vis, 0, sizeof(vis));
    54             ans += dfs(i);
    55         }
    56     } 
    57     return ans;
    58 }
    59 
    60 int main()
    61 {
    62     int T;
    63     scanf("%d", &T);
    64     while(T--)
    65     {
    66         memset(Map, 0, sizeof(Map));
    67         scanf("%d", &n);
    68         int x;
    69         for(int i = 1; i <= n; i++)for(int j = 1; j <= n; j++)
    70         {
    71             scanf("%d", &x);
    72             if(x)Map[i][j] = 1;
    73         }
    74         if(maxmatch() == n)puts("Yes");
    75         else puts("No");
    76     } 
    77     return 0;
    78 }
  • 相关阅读:
    SPSS Clementine 数据挖掘入门1
    SPSS19.0实战之多元线性回归
    SPSS19.0实战之聚类分析
    使用PowerDesigner建立数据库模型【转】
    Power Desginer系列03【转载】
    Power Desginer系列02【转载】
    Power Desginer系列01【转摘】
    Power Desginer系列00【转载】
    PowerDesigner概念设计模型(CDM)中的3种实体关系
    PowerDesigner教程系列(一)概念数据模型
  • 原文地址:https://www.cnblogs.com/fzl194/p/9678089.html
Copyright © 2020-2023  润新知