QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#640463 | #5071. Check Pattern is Good | Flamire | RE | 0ms | 0kb | C++17 | 3.4kb | 2024-10-14 13:00:45 | 2024-10-14 13:00:45 |
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);
// }
int A;
printf("%d\n",A=(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);
int cc=0;
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)
{
++cc;
// 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));
}
}
}
}
assert(cc==A);
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;
}
詳細信息
Test #1:
score: 0
Runtime Error
input:
3 2 2 ?? ?? 3 3 BW? W?B ?BW 3 3 BW? W?W ?W?