QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#640461 | #5071. Check Pattern is Good | Flamire | WA | 77ms | 16092kb | C++17 | 3.4kb | 2024-10-14 12:59:30 | 2024-10-14 12:59:30 |
Judging History
answer
#include <bits/stdc++.h>
#define N 1000011
#define ID(i,j) (((i)-1)*(m-1)+(j))
using namespace std;
int t,n,m,S,T;char s[111][111];
int toi(char c){return (c=='W')+2*(c=='?');}
char toc[11]="BW?";
struct edge{int v,w,next;edge(){}edge(int _v,int _w,int _next){v=_v;w=_w;next=_next;}}e[N*2];int head[N],cur[N],sz;
void insert(int u,int v,int w,int w2=0){e[++sz]=edge(v,w,head[u]);head[u]=sz;e[++sz]=edge(u,w2,head[v]);head[v]=sz;}
bool vis[N];int dis[N];
bool bfs()
{
static queue<int> q;
for(int i=S;i<=T;++i)vis[i]=dis[i]=0;
q.push(S);vis[S]=1;dis[S]=0;
while(!q.empty())
{
int p=q.front();q.pop();//printf("bfs p:%d dis:%d\n",p,dis[p]);
for(int i=head[p];~i;i=e[i].next)if(e[i].w&&!vis[e[i].v])
{
dis[e[i].v]=dis[p]+1;vis[e[i].v]=1;
q.push(e[i].v);
}
}
return vis[T];
}
int dfs(int u,int fl)
{//printf("dfs(%d,fl:%d)\n",u,fl);
if(u==T||!fl)return fl;int ans=0;
for(int &i=cur[u];~i;i=e[i].next)if(e[i].w&&dis[e[i].v]==dis[u]+1)
{
int res=dfs(e[i].v,min(e[i].w,fl));
e[i].w-=res;e[i^1].w+=res;fl-=res;ans+=res;
if(!fl)break;
}
return ans;
}
int dinic(){int ans=0;while(bfs()){for(int i=S;i<=T;++i)cur[i]=head[i];ans+=dfs(S,1e9);}return ans;}
#define id(i,j) (((i)-1)*m+(j))
int fa[N];char col[N];
int get(int a){return fa[a]==a?a:fa[a]=get(fa[a]);}
int main()
{
memset(head,-1,sizeof(head));sz=-1;
scanf("%d",&t);while(t--)
{
for(int i=0;i<=sz;++i)head[i]=-1;sz=-1;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i)scanf("%s",s[i]+1);
S=1;T=2*(n-1)*(m-1)+2;
for(int i=1;i<=n;++i)for(int j=1;j<=m;++j)if(s[i][j]!='?'&&(i+j&1))s[i][j]^='B'^'W';
for(int i=1;i<n;++i)
{
for(int j=1;j<m;++j)
{
bool can[4]={1,1,0,0};
can[toi(s[i][j])^1]=0;
can[toi(s[i+1][j])^1]=0;
can[toi(s[i][j+1])^1]=0;
can[toi(s[i+1][j+1])^1]=0;
insert(S,2*ID(i,j),can[0]?0:1e9,1e9);
insert(2*ID(i,j),2*ID(i,j)+1,1,1e9);
insert(2*ID(i,j)+1,T,can[1]?0:1e9,1e9);
if(i+1<n)insert(2*ID(i,j)+1,2*ID(i+1,j),1e9),insert(2*ID(i+1,j)+1,2*ID(i,j),1e9);
if(j+1<m)insert(2*ID(i,j)+1,2*ID(i,j+1),1e9),insert(2*ID(i,j+1)+1,2*ID(i,j),1e9);
if(i+1<n&&j+1<m)insert(2*ID(i,j)+1,2*ID(i+1,j+1),1e9),insert(2*ID(i+1,j+1)+1,2*ID(i,j),1e9);
if(i>1&&j+1<m)insert(2*ID(i,j)+1,2*ID(i-1,j+1),1e9),insert(2*ID(i-1,j+1)+1,2*ID(i,j),1e9);
}
}
// for(int i=S;i<=T;++i)
// {
// printf("edge[%d]:",i);
// for(int _=head[i];~_;_=e[_].next)printf("(%d->%d %d) ",i,e[_].v,e[_].w);putchar(10);
// }
printf("%d\n",(n-1)*(m-1)-dinic());
for(int i=1;i<=n;++i)for(int j=1;j<=m;++j)fa[id(i,j)]=id(i,j),col[id(i,j)]='?';
// printf("col:%s\n",col+1);
for(int i=1;i<n;++i)
{
for(int j=1;j<m;++j)
{
for(int _=head[2*ID(i,j)];~_;_=e[_].next)if(e[_].v==2*ID(i,j)+1)
{
if(e[_].w)
{
// printf("chose center (%d,%d)\n",i,j);
fa[get(id(i,j+1))]=get(id(i,j));
fa[get(id(i+1,j))]=get(id(i,j));
fa[get(id(i+1,j+1))]=get(id(i,j));
}
}
}
}
for(int i=1;i<=n;++i)for(int j=1;j<=m;++j)if(s[i][j]!='?')col[get(id(i,j))]=s[i][j];
for(int i=1;i<=n*m;++i)if(get(i)==i&&col[i]=='?')col[i]='B';
// printf("col:%s\n",col+1);
// printf("s:\n");
// for(int i=1;i<=n;++i)printf("%s\n",s[i]+1);
for(int i=1;i<=n;++i)
{
for(int j=1;j<=m;++j)
{
putchar(col[get(id(i,j))]^(i+j&1)*('B'^'W'));
}
putchar(10);
}
}
fclose(stdin);fclose(stdout);return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 16092kb
input:
3 2 2 ?? ?? 3 3 BW? W?B ?BW 3 3 BW? W?W ?W?
output:
1 BW WB 1 BWB WBB BBW 4 BWB WBW BWB
result:
ok ok (3 test cases)
Test #2:
score: -100
Wrong Answer
time: 77ms
memory: 14048kb
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:
3 BB BW WW WW BW WB BW WB BB 2 BW WB BW BW WW WB 9 WBBBWWB WBWBWWW BWBBWWB WBWWBWW BBWBBWB WWBBWWW BWBWBWB WWWWBBW BBWBBWW BBWBWBB 6 BWWBWWB WBBWWWB BWWBBBW BBBWBBB 0 B W B B B W W W B W 15 BWWW WBWB WWWB WBBW BWWB BWBW WBWB BWBW WBWW BWBW 8 WBW WWB WBW BWB WBW WBW BWB WWW 0 W B B W 1 BBWB WWBB 3 BW...
result:
wrong answer There are 17 check pattern in you output, but you answered 18 (test case 15)