QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#402793 | #6746. Merge the Rectangles | sqqqqqqqqy | WA | 92ms | 137052kb | C++14 | 3.4kb | 2024-05-01 14:52:33 | 2024-05-01 14:52:35 |
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)
{
if(a==b)return;
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]+=8;
tot[col2][row]+=2;
}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)
{
//cout<<i<<" "<<j<<" "<<1+2+4+8-tot[i][j]<<endl;
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: 100
Accepted
time: 0ms
memory: 34852kb
input:
3 4 0000 0111 101 101 110
output:
YES
result:
ok answer is YES
Test #2:
score: 0
Accepted
time: 3ms
memory: 34848kb
input:
3 3 110 011 01 11 10
output:
NO
result:
ok answer is NO
Test #3:
score: 0
Accepted
time: 57ms
memory: 52676kb
input:
1500 1500 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
YES
result:
ok answer is YES
Test #4:
score: 0
Accepted
time: 65ms
memory: 137052kb
input:
1500 1500 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...
output:
YES
result:
ok answer is YES
Test #5:
score: 0
Accepted
time: 44ms
memory: 99304kb
input:
1500 1500 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...
output:
YES
result:
ok answer is YES
Test #6:
score: 0
Accepted
time: 92ms
memory: 99284kb
input:
1500 1500 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
YES
result:
ok answer is YES
Test #7:
score: -100
Wrong Answer
time: 72ms
memory: 136224kb
input:
1500 1500 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...
output:
YES
result:
wrong answer expected NO, found YES