QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#407443 | #6746. Merge the Rectangles | SanCai# | WA | 1ms | 9696kb | C++14 | 3.4kb | 2024-05-08 18:19:41 | 2024-05-08 18:19:42 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define lowbit(x) ((x)&(-x))
#define endl '\n'
#define sc scanf
#define pt printf
#define YES cout<<"YES"<<endl;
#define NO cout<<"NO"<<endl;
#define ls k+k
#define rs k+k+1
using pll = pair<ll, ll>;
using pii = pair<int, int>;
const int N = 3e6 + 10;
const ll p=998244353;
int n,m,now;
struct node
{
int U,D,L,R,col;
}nd[1510][1510];
int sz[N],pre[N];
int find(int x)
{
return pre[x]==x?x:pre[x]=find(pre[x]);
}
void merge(int x,int y)
{
int fx=find(x),fy=find(y);
if(fx==fy)return;
pre[fx]=fy;
}
bool vis[1510][1510],viscol[N];
void dfs1(int x,int y,int col)
{
if(vis[x][y])return;
vis[x][y]=1;
nd[x][y].col=col;
sz[col]++;
if(x+1<=n&&!nd[x][y].D)dfs1(x+1,y,col);
if(x-1>=1&&!nd[x][y].U)dfs1(x-1,y,col);
if(y+1<=m&&!nd[x][y].R)dfs1(x,y+1,col);
if(y-1>=1&&!nd[x][y].L)dfs1(x,y-1,col);
}
struct Cnode
{
pii n1,n2;
}CN[N];
void clearvis()
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)vis[i][j]=0;
}
}
void Merge(int x,int y)
{
int fx=find(x),fy=find(y);
Cnode &N1=CN[fx],&N2=CN[fy];
if(fx==fy)return;
//-----------------------------------------
int x1=N1.n1.first,x2=N1.n2.first;
int x3=N2.n1.first,x4=N2.n2.first;
int y1=N1.n1.second,y2=N1.n2.second;
int y3=N2.n1.second,y4=N2.n2.second;
if(x1==x3&&x2==x4)
{
CN[fy].n1={x1,min(y1,y3)};
CN[fy].n2={x2,max(y1,y3)};
pre[fx]=fy;
}
if(y1==y3&&y2==y4)
{
CN[fy].n1={min(x1,x3),y1};
CN[fy].n2={max(x2,x4),y2};
pre[fx]=fy;
}
//-----------------------------------------
}
void UraykevoliQwQ()
{
cin>>n>>m;
for(int i=1;i<n;i++)
{
string s;cin>>s;
s=" "+s;
for(int j=1;j<=m;j++)
{
if(s[j]=='1')
{
nd[i][j].D=nd[i+1][j].U=1;
}
}
}
for(int i=1;i<=n;i++)
{
string s;cin>>s;
s=" "+s;
for(int j=1;j<m;j++)
{
if(s[j]=='1')
{
nd[i][j].R=nd[i][j+1].L=1;
}
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
if(!vis[i][j])dfs1(i,j,++now);
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
//cout<<find(nd[i][j].col)<<' ';
}
//cout<<endl;
}
clearvis();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(!viscol[nd[i][j].col])
{
int Col=nd[i][j].col;
viscol[Col]=1;
int x=i,y=j;
while(x<=n&&nd[x+1][j].col==Col)x++;
while(y<=m&&nd[i][y+1].col==Col)y++;
if((x-i+1)*(y-j+1)!=sz[Col])
{
cout<<"NO";
return;
}
CN[Col].n1={i,j},CN[Col].n2={x,y};
}
}
}
for(int i=1;i<=now;i++)pre[i]=i;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(i+1<=n)
{
int x=nd[i][j].col,y=nd[i+1][j].col;
Merge(x,y);
}
if(i-1>=1)
{
int x=nd[i][j].col,y=nd[i-1][j].col;
Merge(x,y);
}
if(j+1<=m)
{
int x=nd[i][j].col,y=nd[i][j+1].col;
Merge(x,y);
}
if(j-1>=1)
{
int x=nd[i][j].col,y=nd[i][j-1].col;
Merge(x,y);
}
}
}
int cnt=0;
//cout<<"--------------"<<endl;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
//cout<<find(nd[i][j].col)<<' ';
}
//cout<<endl;
}
for(int i=1;i<=now;i++)if(pre[i]==i)cnt++;
if(cnt==1)cout<<"YES";
else cout<<"NO";
}
int main()
{
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
//int _;cin>>_;while(_--)
UraykevoliQwQ();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 9696kb
input:
3 4 0000 0111 101 101 110
output:
NO
result:
wrong answer expected YES, found NO