QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#402785#6746. Merge the Rectanglessqqqqqqqqy#WA 0ms27860kbC++143.2kb2024-05-01 14:36:152024-05-01 14:36:15

Judging History

你现在查看的是最新测评结果

  • [2024-05-01 14:36:15]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:27860kb
  • [2024-05-01 14:36:15]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;

const int N=2000;
const int M=4e6+5;
int g[N][N];
int tot[N][N];
int n,m;
int p[M];
int e[M],ne[M],idx;
int d[M];
int q[M];
int h[M];
vector<int>v;

bool topsort()
{
    int hh = 0, tt = -1;

    for (auto i:v)
        if (!d[i])
            q[ ++ tt] = i;

    while (hh <= tt)
    {
        int t = q[hh ++ ];

        for (int i = h[t]; i != -1; i = ne[i])
        {
            int j = e[i];
            if (-- d[j] == 0)
                q[ ++ tt] = j;
        }
    }
    return tt == v.size() - 1;
}

void add(int a,int b)
{
    d[b]++;
    e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}

int find(int x)
{
    if(p[x]!=x)p[x]=find(p[x]);
    return p[x];
}

int get(int x,int y)
{
    return (x-1)*m+y;
}

void uni(int x1,int  y1,int x2,int y2)
{
    int id1=get(x1,y1);
    int id2=get(x2,y2);
    if(find(id1)!=find(id2))
    {
        p[find(id1)]=find(id2);
    }
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    memset(h,-1,sizeof h);
    cin>>n>>m;
    for(int i=1;i<=n*m;i++)
    {
        p[i]=i;
    }
    for(int i=1;i<=n-1;i++)
    {
        int x=i,y=i+1;
        string s;
        cin>>s;
        s=" "+s;
        for(int j=1;j<=m;j++)
        {
            if(s[j]=='1')
            {
                int row=i;
                int col1=j-1,col2=j;
                g[row][col1]++;
                g[row][col2]++;
                tot[row][col1]+=1;
                tot[row][col2]+=4;
            }else
            {
                uni(x,j,y,j);
            }
        }
    }
    for(int i=1;i<=m-1;i++)
    {
        int x=i,y=i+1;
        string s;
        cin>>s;
        s=" "+s;
        for(int j=1;j<=n;j++)
        {
            if(s[j]=='1')
            {
                
                int row=i;
                int col1=j-1,col2=j;
                g[col1][row]++;
                g[col2][row]++;
                tot[col1][row]+=2;
                tot[col2][row]+=8;
            }else
            {
                uni(j,x,j,y);
            }
        }
    }
    for(int i=1;i<=n*m;i++)
    {
        if(find(i)==i)v.push_back(i);
    }
    for(int i=1;i<=n-1;i++)
    {
        for(int j=1;j<=m-1;j++)
        {
            int zs,zx,ys,yx;
            zs=get(i,j);
            zx=get(i+1,j);
            ys=get(i,j+1);
            yx=get(i+1,j+1);
            zs=find(zs);
            zx=find(zx);
            ys=find(ys);
            yx=find(yx);
            if(g[i][j]==3)
            {
                int k=1+2+4+8-tot[i][j];
                if(k==1)
                {
                    add(ys,zs);
                    add(ys,zx);
                }else if(k=4)
                {
                    add(zs,ys);
                    add(zs,yx);
                }else if(k==2)
                {
                    add(zs,zx);
                    add(ys,yx);
                }else if(k==8)
                {
                    add(zx,zs);
                    add(yx,ys);
                }
            }
        }
    }
    if(topsort())cout<<"YES"<<'\n';
    else cout<<"NO"<<'\n';
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 27860kb

input:

3 4
0000
0111
101
101
110

output:

NO

result:

wrong answer expected YES, found NO