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

    题目意思还是比较直观的,而且这个建模的套路也很明显。

    我们首先考虑从主对角线可以转移到哪些状态。

    由于每一次操作都不会把同一行(列)的黑色方块分开。因此我们发现:

    只要找出(n)个黑色棋子,让它们恰好占据所有的行和列即为有解。

    所以我们对于所有黑色棋子的位置建边,就是将行和列链接起来。

    然后二分图匹配/网络流水一波最大匹配即可,若等于(n)(Yes),否则就是(No)

    CODE

    #include<cstdio>
    #include<cstring>
    using namespace std;
    const int N=205;
    struct edge
    {
        int to,next;
    }e[N*N];
    int head[N],from[N],cnt,t,n,x,tot;
    bool vis[N];
    inline char tc(void)
    {
        static char fl[100000],*A=fl,*B=fl;
        return A==B&&(B=(A=fl)+fread(fl,1,100000,stdin),A==B)?EOF:*A++;
    }
    inline void read(int &x)
    {
        x=0; char ch=tc();
        while (ch<'0'||ch>'9') ch=tc();
        while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=tc();
    }
    inline void add(int x,int y)
    {
        e[++cnt].to=y; e[cnt].next=head[x]; head[x]=cnt;
    }
    inline bool find(int now)
    {
        for (register int i=head[now];i!=-1;i=e[i].next)
        if (!vis[e[i].to-n])
        {
            vis[e[i].to-n]=1;
            if (!from[e[i].to-n]||find(from[e[i].to-n]))
            {
                from[e[i].to-n]=now; return 1;
            }
        }
        return 0;
    }
    int main()
    {
        //freopen("CODE.in","r",stdin); freopen("CODE.out","w",stdout);
        register int i,j; read(t);
        while (t--)
        {
            memset(head,-1,sizeof(head));
            memset(e,-1,sizeof(e));
            memset(from,0,sizeof(from));
            read(n); tot=cnt=0;
            for (i=1;i<=n;++i)
            for (j=1;j<=n;++j)
            {
                read(x); if (x) add(i,j+n);
            }
            for (i=1;i<=n;++i)
            memset(vis,0,sizeof(vis)),tot+=find(i);
            puts(tot^n?"No":"Yes");
        }
        return 0;
    }
    
  • 相关阅读:
    DDD 领域驱动设计
    IOC 控制反转
    WCF
    Lucene 全文检索引擎
    Redis
    Cache 缓存
    return
    PHP中empty();和isset();的区别.
    sql 简单用语
    关系型数据库
  • 原文地址:https://www.cnblogs.com/cjjsb/p/9240576.html
Copyright © 2011-2022 走看看