QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#644043 | #5071. Check Pattern is Good | Kevin5307 | TL | 0ms | 0kb | C++23 | 3.8kb | 2024-10-16 10:20:47 | 2024-10-16 10:20:51 |
answer
//Author: Kevin
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
#define ll long long
#define ull unsigned ll
#define pb emplace_back
#define mp make_pair
#define ALL(x) (x).begin(),(x).end()
#define rALL(x) (x).rbegin(),(x).rend()
#define srt(x) sort(ALL(x))
#define rev(x) reverse(ALL(x))
#define rsrt(x) sort(rALL(x))
#define sz(x) (int)(x.size())
#define inf 0x3f3f3f3f
#define pii pair<int,int>
#define lb(v,x) (int)(lower_bound(ALL(v),x)-v.begin())
#define ub(v,x) (int)(upper_bound(ALL(v),x)-v.begin())
#define uni(v) v.resize(unique(ALL(v))-v.begin())
#define longer __int128_t
void die(string S){puts(S.c_str());exit(0);}
string grid[105];
int n,m;
class MaximumFlow
{
public:
struct edge
{
int u,v,capa;
}E[2001000];
edge make_edge(int u,int v,int capa)
{
edge e;
e.u=u;
e.v=v;
e.capa=capa;
return e;
}
vector<int> G[30010];
int p;
bool inque[30010];
int dep[30010];
int head[30010];
bool bfs(int s,int t)
{
for(int i=0;i<30010;i++)
dep[i]=inf;
memset(inque,0,sizeof(inque));
dep[s]=0;
inque[s]=1;
queue<int> q;
q.push(s);
while(!q.empty())
{
int x=q.front();
q.pop();
inque[x]=0;
for(int e:G[x])
{
int v=E[e].v;
if(dep[v]>dep[x]+1&&E[e].capa)
{
dep[v]=dep[x]+1;
if(!inque[v])
{
q.push(v);
inque[v]=1;
}
}
}
}
return (dep[t]!=inf);
}
int dfs(int u,int t,int aug)
{
int naug=0;
if(u==t)
return aug;
int tot=0;
for(;head[u]<sz(G[u]);head[u]++)
{
int e=G[u][head[u]];
int v=E[e].v;
if(E[e].capa&&dep[v]==dep[u]+1)
{
if((naug=dfs(v,t,min(aug,E[e].capa))))
{
E[e].capa-=naug;
E[e^1].capa+=naug;
aug-=naug;
tot+=naug;
}
if(!aug) return tot;
}
}
return tot;
}
public:
void clear(int n)
{
p=0;
for(int i=0;i<=n;i++)
G[i].clear();
}
MaximumFlow()
{
clear(0);
}
void addEdge(int u,int v,int capa)
{
G[u].pb(p);
E[p++]=make_edge(u,v,capa);
G[v].pb(p);
E[p++]=make_edge(v,u,0);
}
int dinic(int s,int t)
{
int flow;
int maxflow=0;
memset(head,0,sizeof(head));
while(bfs(s,t))
{
while((flow=dfs(s,t,inf)))
maxflow+=flow;
}
return maxflow;
}
};
MaximumFlow mf;
int vis[30010];
void dfs(int x)
{
vis[x]=1;
if(x<=n*m&&x)
grid[(x-1)/m][(x-1)%m]='B';
for(auto e:mf.G[x])
if(mf.E[e].capa)
if(!vis[mf.E[e].v])
dfs(mf.E[e].v);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--)
{
cin>>n>>m;
for(int i=0;i<n;i++)
cin>>grid[i];
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if((i+j)&1) if(grid[i][j]!='?')
grid[i][j]='B'+'W'-grid[i][j];
int s=0,t=n*m+(n-1)*(m-1)*2+1;
for(int i=0;i<n-1;i++)
for(int j=0;j<m-1;j++)
for(int a=0;a<2;a++)
for(int b=0;b<2;b++)
{
mf.addEdge(n*m+i*(m-1)+j+1,(i+a)*m+(j+b)+1,inf);
mf.addEdge((i+a)*m+(j+b)+1,n*m+i*(m-1)+j+1+(n-1)*(m-1),inf);
}
for(int i=0;i<n-1;i++)
for(int j=0;j<m-1;j++)
mf.addEdge(n*m+i*(m-1)+j+1+(n-1)*(m-1),n*m+i*(m-1)+j+1,1);
for(int i=s;i<=t;i++)
vis[i]=0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(grid[i][j]=='B')
mf.addEdge(s,i*m+j+1,inf);
else if(grid[i][j]=='W')
mf.addEdge(i*m+j+1,t,inf);
int ans=(n-1)*(m-1)-mf.dinic(s,t);
dfs(s);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(grid[i][j]=='?')
grid[i][j]='W';
cout<<ans<<'\n';
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
if((i+j)&1)
cout<<(char)('B'+'W'-grid[i][j]);
else
cout<<grid[i][j];
cout<<'\n';
}
mf.clear(t);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
3 2 2 ?? ?? 3 3 BW? W?B ?BW 3 3 BW? W?W ?W?