QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#402789 | #6746. Merge the Rectangles | sqqqqqqqqy# | WA | 3ms | 43324kb | C++14 | 3.3kb | 2024-05-01 14:42:50 | 2024-05-01 14:42:50 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
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 cnt=0;
queue<int>q;
for(auto i:v)
{
if(!d[i])
{
q.push(i);
cnt++;
}
}
while(q.size())
{
int t=q.front();
q.pop();
for(int i=h[t];~i;i=ne[i])
{
int j=e[i];
if(--d[j]==0)
{
q.push(j);
cnt++;
}
}
}
return cnt==v.size();
}
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: 3ms
memory: 43324kb
input:
3 4 0000 0111 101 101 110
output:
NO
result:
wrong answer expected YES, found NO