• bzoj 1059: [ZJOI2007]矩阵游戏 二分图匹配


    题目链接

    1059: [ZJOI2007]矩阵游戏

    Time Limit: 10 Sec  Memory Limit: 162 MB
    Submit: 3219  Solved: 1560
    [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
     
    如果a[i][j] = 1, 那么i就向j连一条边, 看最后是否所有的行都可以匹配到某一列, 如果不可以就输出no
    #include <iostream>
    #include <vector>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <map>
    #include <set>
    #include <string>
    #include <queue>
    #include <stack>
    #include <bitset>
    using namespace std;
    #define pb(x) push_back(x)
    #define ll long long
    #define mk(x, y) make_pair(x, y)
    #define lson l, m, rt<<1
    #define mem(a) memset(a, 0, sizeof(a))
    #define rson m+1, r, rt<<1|1
    #define mem1(a) memset(a, -1, sizeof(a))
    #define mem2(a) memset(a, 0x3f, sizeof(a))
    #define rep(i, n, a) for(int i = a; i<n; i++)
    #define fi first
    #define se second
    typedef pair<int, int> pll;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int mod = 1e9+7;
    const int inf = 1061109567;
    const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
    int a[205][205];
    const int maxn = 1e5+5;
    int head[maxn*2], num, path[205], vis[205];
    struct node
    {
        int to, nextt;
    }e[maxn*2];
    void add(int u, int v) {
        e[num].to = v, e[num].nextt = head[u], head[u] = num++;
    }
    void init() {
        num = 0;
        mem1(head);
    }
    int dfs(int u) {
        for(int i = head[u]; ~i; i = e[i].nextt) {
            int v = e[i].to;
            if(vis[v])
                continue;
            vis[v] = 1;
            if(path[v] == -1 || dfs(path[v])) {
                path[v] = u;
                return 1;
            }
        }
        return 0;
    }
    int main()
    {
        int t, n;
        cin>>t;
        while(t--) {
            cin>>n;
            init();
            int cnt = 0;
            for(int i = 0; i<n; i++) {
                for(int j = 0; j<n; j++) {
                    scanf("%d", &a[i][j]);
                    if(a[i][j]) {
                        add(i+1, j+1);
                    }
                }
            }
            mem1(path);
            int flag = 0;
            for(int i = 1; i<=n; i++) {
                mem(vis);
                if(!dfs(i))
                    flag = 1;
            }
            if(!flag) {
                puts("Yes");
            } else {
                puts("No");
            }
        }
        return 0;
    }
  • 相关阅读:
    audio之点击图标切换音频播放状态以及替换当前图标
    js之ajax的封装
    JavaScript之ajax的作用、流程以及如何设置请求头
    BFC的理解
    2016年总结
    如何用shared_ptr减少锁的争用
    webrtc初识
    2015总结与2016展望
    记一次程序排错与std::getline
    spawn协程学习
  • 原文地址:https://www.cnblogs.com/yohaha/p/5227201.html
Copyright © 2020-2023  润新知