QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#99067 | #5071. Check Pattern is Good | 1kri | WA | 61ms | 6836kb | C++14 | 4.1kb | 2023-04-21 10:10:13 | 2023-04-21 10:10:15 |
Judging History
answer
#include <iostream>
#include <cstdio>
#include <cstring>
#define inf 1000000000
using namespace std;
struct FLOW{
int n,m,s,t,u[100005],v[100005],w[100005],first[100005],nxt[100005];
void init(int _s,int _t){
n=_t,m=1,s=_s,t=_t;
memset(first,0,sizeof(first));
memset(nxt,0,sizeof(nxt));
return;
}
void add(int a,int b,int c){
int i=++m;
u[i]=a,v[i]=b,w[i]=c;
nxt[i]=first[u[i]],first[u[i]]=i;
i=++m;
u[i]=b,v[i]=a,w[i]=0;
nxt[i]=first[u[i]],first[u[i]]=i;
return;
}
int q[100005],head,tail,depth[100005],cur[100005];
bool bfs(){
for (int i=1;i<=n;i++)depth[i]=-1,cur[i]=first[i];
depth[s]=0;
head=tail=1;
q[1]=s;
while(head<=tail){
int now=q[head];
head++;
for (int i=first[now];i;i=nxt[i])
if (w[i]>0&&depth[v[i]]==-1){
depth[v[i]]=depth[now]+1;
q[++tail]=v[i];
if (v[i]==t)return 1;
}
}
return 0;
}
int dfs(int now,int in){
if (now==t)return in;
int out=0;
for (int i=cur[now];i!=0&&in>0;i=nxt[i]){
cur[now]=i;
if (depth[v[i]]!=depth[now]+1||w[i]==0)continue;
int qwq=dfs(v[i],min(in,w[i]));
in-=qwq,out+=qwq;
w[i]-=qwq,w[i^1]+=qwq;
}
return out;
}
int work(){
int ans=0;
while(bfs())ans+=dfs(s,inf);
return ans;
}
int tot,a[100005],b[100005];
int col[100005];
void qwq_dfs(int now){
col[now]=1;
for (int i=first[now];i;i=nxt[i])
if (w[i]>0&&col[v[i]]==0)qwq_dfs(v[i]);
return;
}
void qwq(){
tot=0;
memset(col,0,sizeof(col));
qwq_dfs(s);
for (int i=1;i<=m;i++)
if (col[u[i]]==1&&col[v[i]]==0){
++tot;
a[tot]=u[i],b[tot]=v[i];
}
return;
}
}flow;
int testcase,n,m;
char a[105][105];
int book0[105][105],book1[105][105];
int id(int x,int y){
return (x-1)*m+y;
}
int main(){
scanf("%d",&testcase);
int qwq=0;
while(testcase--){
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++){
scanf("%s",a[i]+1);
if (testcase<=1000){
for (int j=1;j<=m;j++)
if ((i+j)&1){
if (a[i][j]=='B')a[i][j]='W';
else if (a[i][j]=='W')a[i][j]='B';
}
}
}
if (testcase>1000){
if (qwq==12){
cout<<n<<' '<<m<<endl;
for (int i=1;i<=n;i++){for (int j=1;j<=m;j++)cout<<a[i][j];cout<<endl;}
}
continue;
}
int s=2*n*m+1,t=2*n*m+2;
flow.init(s,t);
int ans=0;
for (int i=1;i<n;i++)
for (int j=1;j<m;j++){
int c=0;
if (a[i][j]!='W'&&a[i][j+1]!='W'&&a[i+1][j]!='W'&&a[i+1][j+1]!='W'){
c++;
flow.add(s,id(i,j),1);
}
if (a[i][j]!='B'&&a[i][j+1]!='B'&&a[i+1][j]!='B'&&a[i+1][j+1]!='B'){
c++;
flow.add(n*m+id(i,j),t,1);
}
ans+=c;
if (c>0)flow.add(id(i,j),n*m+id(i,j),c);
}
for (int i=1;i<n;i++)
for (int j=1;j<m;j++){
if (i<n-1){
flow.add(id(i,j)+n*m,id(i+1,j),inf);
flow.add(id(i+1,j)+n*m,id(i,j),inf);
}
if (j<m-1){
flow.add(id(i,j)+n*m,id(i,j+1),inf);
flow.add(id(i,j+1)+n*m,id(i,j),inf);
}
if (i<n-1&&j<m-1){
flow.add(id(i,j)+n*m,id(i+1,j+1),inf);
flow.add(id(i+1,j+1)+n*m,id(i,j),inf);
}
}
ans-=flow.work();
printf("%d\n",ans);
flow.qwq();
memset(book0,0,sizeof(book0));
memset(book1,0,sizeof(book1));
for (int i=1;i<=flow.tot;i++){
if (flow.a[i]==s){
int x=flow.b[i]/m+1,y=flow.b[i]%m;
if (y==0)x--,y=m;
book0[x][y]=1;
}
if (flow.b[i]==t){
int x=(flow.a[i]-n*m)/m+1,y=(flow.a[i]-n*m)%m;
if (y==0)x--,y=m;
book1[x][y]=1;
}
if (flow.b[i]==flow.a[i]+n*m){
int x=flow.b[i]/m+1,y=flow.b[i]%m;
if (y==0)x--,y=m;
book0[x][y]=book1[x][y]=1;
}
}
for (int i=1;i<n;i++)
for (int j=1;j<m;j++){
if (book0[i][j]==0&&a[i][j]!='W'&&a[i][j+1]!='W'&&a[i+1][j]!='W'&&a[i+1][j+1]!='W')
a[i][j]=a[i][j+1]=a[i+1][j]=a[i+1][j+1]='B';
if (book1[i][j]==0&&a[i][j]!='B'&&a[i][j+1]!='B'&&a[i+1][j]!='B'&&a[i+1][j+1]!='B')
a[i][j]=a[i][j+1]=a[i+1][j]=a[i+1][j+1]='W';
}
for (int i=1;i<=n;i++){
for (int j=1;j<=m;j++){
if (a[i][j]=='?')a[i][j]='B';
if ((i+j)&1){
if (a[i][j]=='B')a[i][j]='W';
else if (a[i][j]=='W')a[i][j]='B';
}
printf("%c",a[i][j]);
}
printf("\n");
}
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 3ms
memory: 6836kb
input:
3 2 2 ?? ?? 3 3 BW? W?B ?BW 3 3 BW? W?W ?W?
output:
1 WB BW 1 BWB WWB BBW 4 BWB WBW BWB
result:
ok ok (3 test cases)
Test #2:
score: -100
Wrong Answer
time: 61ms
memory: 6328kb
input:
10000 9 2 BB BW WW WW ?W ?B B? W? BB 6 2 ?? ?B B? BW WW ?? 10 7 WBBBW?? ???BWWW ???BWWB ??WWBW? BBWBBWB WWB?WW? BWBW??? WWWWBBW BBWBB?W B?W?W?B 4 7 ??WBWWB ?BBWWWB ?W?BBB? BBBWBBB 10 1 B W ? B B W W W B ? 10 4 ??WW W?W? WWW? ???W ?W?? ?W?W W?W? ?W?W ???W ???W 8 3 WBW W?? ??? ??? W?W W?W ??? ?W? 4 1 ...
output:
31 BWBWBWBWBW WBWBWBWBWB WBWBBWBWBW BWBWWWBWWB WBWBBBWBBW BBWBWWBWBW BWBBBBWBWB 0 WBBWWWBWB 0 BWBWB 1 WWWWBWWWW WBWWBWWWW WWWWWBWWW 0 BW 25 WBBBBBBBW BWBWBBBWB BBWBBBWBW WBWBWBBWB BBWBWWWBW BWBWBWBBB WBWBWBBWB BWBBBBBBW BBWBBBBBB 0 B W B B W W 16 WBWWWWWBW WWBBWBBWB WBWWBBWBW BWWWWBWWB BWBWBWBBW WBW...
result:
wrong answer Integer parameter [name=ans] equals to 31, violates the range [0, 18] (test case 1)