zoukankan      html  css  js  c++  java
  • BZOJ 1059 [ZJOI2007]矩阵游戏 (二分图最大匹配)

    1059: [ZJOI2007]矩阵游戏

    Time Limit: 10 Sec  Memory Limit: 162 MB
    Submit: 5281  Solved: 2530
    [Submit][Status][Discuss]

    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

    HINT

     

    Source

    析:对于同行或者是同列的数,那么无论经过多少次变化,那么肯定也是同行同列,那么也就是求能不能找到 n 个不同不同列的1。也就是二分图的最大匹配。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <sstream>
    #include <list>
    #include <assert.h>
    #include <bitset>
    #include <numeric>
    #define debug() puts("++++")
    #define gcd(a, b) __gcd(a, b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define fi first
    #define se second
    #define pb push_back
    #define sqr(x) ((x)*(x))
    #define ms(a,b) memset(a, b, sizeof a)
    #define sz size()
    #define pu push_up
    #define pd push_down
    #define cl clear()
    //#define all 1,n,1
    #define FOR(i,x,n)  for(int i = (x); i < (n); ++i)
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const LL LNF = 1e17;
    const double inf = 1e20;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 400 + 10;
    const int maxm = 3e5 + 10;
    const ULL mod = 3;
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, -1, 0, 1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline bool is_in(int r, int c) {
      return r >= 0 && r < n && c >= 0 && c < m;
    }
    
    struct Edge{
      int to, next;
    };
    Edge edges[maxn*maxn<<1];
    int head[maxn], cnt;
    
    void addEdge(int u, int v){
      edges[cnt].to = v;
      edges[cnt].next = head[u];
      head[u] = cnt++;
    }
    
    int match[maxn];
    bool vis[maxn];
    
    bool dfs(int u){
      vis[u] = 1;
      for(int i = head[u]; ~i; i = edges[i].next){
        int v = edges[i].to, w = match[v];
        if(w < 0 || !vis[w] && dfs(w)){
          match[u] = v;
          match[v] = u;
          return true;
        }
      }
      return false;
    }
    
    int main(){
      int T;  cin >> T;
      while(T--){
        scanf("%d", &n);
        ms(head, -1);  cnt = 0;
        for(int i = 0; i < n; ++i)
          for(int j = 0; j < n; ++j){
            int x;  scanf("%d", &x);
            if(x)  addEdge(i, j+n), addEdge(j+n, i);
          }
        int ans = 0;  ms(match, -1);
        for(int i = 0; i < n; ++i)  if(match[i] < 0){
          ms(vis, 0);  if(dfs(i)) ++ans;
        }
        puts(ans == n ? "Yes" : "No");
      }
      return 0;
    }
    

      

  • 相关阅读:
    590. N 叉树的后序遍历
    CF605E
    网络流水题题单
    wqs二分的边界
    luoguP6326 Shopping
    【THUWC2020】工资分配
    CF1336简要题解
    「PKUWC2020」最小割
    洛谷P4895 独钓寒江雪
    省选联考2020简要题解
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/7819522.html
Copyright © 2011-2022 走看看