QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#411263 | #5071. Check Pattern is Good | N_z_ | TL | 32ms | 89968kb | C++14 | 7.0kb | 2024-05-15 10:50:09 | 2024-05-15 10:50:10 |
Judging History
answer
#define LUOGU
#if defined(LOCAL) or not defined(LUOGU)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast,unroll-loops")
#endif
#include<bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include"dbg.h"
#else
#define dbg(...) (__VA_ARGS__)
#endif
namespace Fread{const int SIZE=1<<16;char buf[SIZE],*S,*T;inline char getchar(){if(S==T){T=(S=buf)+fread(buf,1,SIZE,stdin);if(S==T)return'\n';}return *S++;}}namespace Fwrite{const int SIZE=1<<16;char buf[SIZE],*S=buf,*T=buf+SIZE;inline void flush(){fwrite(buf,1,S-buf,stdout);S=buf;}inline void putchar(char c){*S++=c;if(S==T)flush();}struct NTR{~NTR(){flush();}}ztr;}
#define getchar Fread::getchar
#define putchar Fwrite::putchar
#define Setprecision 10
#define between '\n'
#define __int128 long long
template<typename T>struct is_char{static constexpr bool value=(std::is_same<T,char>::value||std::is_same<T,signed char>::value||std::is_same<T,unsigned char>::value);};template<typename T>struct is_integral_ex{static constexpr bool value=(std::is_integral<T>::value||std::is_same<T,__int128>::value)&&!is_char<T>::value;};template<typename T>struct is_floating_point_ex{static constexpr bool value=std::is_floating_point<T>::value||std::is_same<T,__float128>::value;};namespace Fastio{struct Reader{template<typename T>typename std::enable_if_t<std::is_class<T>::value,Reader&>operator>>(T&x){for(auto &y:x)*this>>y;return *this;}template<typename T>typename std::enable_if_t<is_integral_ex<T>::value,Reader&>operator>>(T&x){char c=getchar();short f=1;while(c<'0'||c>'9'){if(c=='-')f*=-1;c=getchar();}x=0;while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+(c^48);c=getchar();}x*=f;return *this;}template<typename T>typename std::enable_if_t<is_floating_point_ex<T>::value,Reader&>operator>>(T&x){char c=getchar();short f=1,s=0;x=0;T t=0;while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.')c=getchar();else return x*=f,*this;while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;x=(x+t)*f;return*this;}template<typename T>typename std::enable_if_t<is_char<T>::value,Reader&>operator>>(T&c){c=getchar();while(c=='\n'||c==' '||c=='\r')c=getchar();return *this;}Reader&operator>>(char*str){int len=0;char c=getchar();while(c=='\n'||c==' '||c=='\r')c=getchar();while(c!='\n'&&c!=' '&&c!='\r')str[len++]=c,c=getchar();str[len]='\0';return*this;}Reader&operator>>(std::string&str){str.clear();char c=getchar();while(c=='\n'||c==' '||c=='\r')c=getchar();while(c!='\n'&&c!=' '&&c!='\r')str.push_back(c),c=getchar();return*this;}Reader(){}}cin;const char endl='\n';struct Writer{typedef __int128 mxdouble;template<typename T>typename std::enable_if_t<std::is_class<T>::value,Writer&>operator<<(T x){for(auto &y:x)*this<<y<<between;*this<<'\n';return *this;}template<typename T>typename std::enable_if_t<is_integral_ex<T>::value,Writer&>operator<<(T x){if(x==0)return putchar('0'),*this;if(x<0)putchar('-'),x=-x;static int sta[45];int top=0;while(x)sta[++top]=x%10,x/=10;while(top)putchar(sta[top]+'0'),--top;return*this;}template<typename T>typename std::enable_if_t<is_floating_point_ex<T>::value,Writer&>operator<<(T x){if(x<0)putchar('-'),x=-x;x+=pow(10,-Setprecision)/2;mxdouble _=x;x-=(T)_;static int sta[45];int top=0;while(_)sta[++top]=_%10,_/=10;if(!top)putchar('0');while(top)putchar(sta[top]+'0'),--top;putchar('.');for(int i=0;i<Setprecision;i++)x*=10;_=x;while(_)sta[++top]=_%10,_/=10;for(int i=0;i<Setprecision-top;i++)putchar('0');while(top)putchar(sta[top]+'0'),--top;return*this;}template<typename T>typename std::enable_if_t<is_char<T>::value,Writer&>operator<<(T c){putchar(c);return*this;}Writer&operator<<(char*str){int cur=0;while(str[cur])putchar(str[cur++]);return *this;}Writer&operator<<(const char*str){int cur=0;while(str[cur])putchar(str[cur++]);return*this;}Writer&operator<<(std::string str){int st=0,ed=str.size();while(st<ed)putchar(str[st++]);return*this;}Writer(){}}cout;}
#define cin Fastio::cin
#define cout Fastio::cout
#define endl Fastio::endl
void solve();
main()
{
int t=1;
cin>>t;
while(t--)solve();
}
long long inf=1e9;
struct sber
{
int to[1000001],dis[1000001],arc[1000001];
vector<int>e[1000001];
long long flow[1000001];
sber(){memset(flow,0,sizeof(flow));}
bool bfs(int s,int t)
{
memset(dis,0x3f,sizeof(dis));
dis[s]=1;
queue<int>q;
q.push(s);
while(!q.empty())
{
int u=q.front();
if(u==t)return 1;
q.pop();
for(auto id:e[u])
if(flow[id]&&dis[to[id]]>dis[u]+1)
{
dis[to[id]]=dis[u]+1;
q.push(to[id]);
}
}
return 0;
}
long long dfs(int s,int t,long long up)
{
if(s==t)return up;
long long res=0;
for(int &_=arc[s];_<e[s].size();_++)
{
int i=e[s][_];
if(flow[i]&&dis[to[i]]==dis[s]+1)
{
long long nw=dfs(to[i],t,min(up,flow[i]));
flow[i]-=nw,flow[i^1]+=nw;
up-=nw;
res+=nw;
if(up==0)return res;
}
}
return res;
}
long long dinic(int s,int t)
{
long long ans=0;
while(bfs(s,t))
{
memset(arc,0,sizeof(arc));
ans+=dfs(s,t,inf);
}
return ans;
}
void add_edge(int u,int v,int w,int x)
{
e[u].emplace_back(2*x);
e[v].emplace_back(2*x+1);
to[2*x]=v,to[2*x+1]=u;
flow[2*x]=w,flow[2*x+1]=0;
}
};
int a[101][101];
bool vis[100001];
sber u;
void dfs(int nw)
{
if(!vis[nw])vis[nw]=1;
else return;
for(auto id:u.e[nw])
if(u.flow[id])dfs(u.to[id]);
}
void solve()
{
u=sber();
int n,m;
cin>>n>>m;
int s=0,t=2*n*m+1;
for(int x=1;x<=n;x++)
for(int y=1;y<=m;y++)
{
char ch;
cin>>ch;
a[x][y]=ch=='B'?-1:ch=='W'?1:0;
a[x][y]*=((x^y)&1)?-1:1;
}
int id=0,ans=0;
for(int x=1;x<n;x++)
for(int y=1;y<m;y++)
{
if(a[x][y]!= 1&&a[x][y+1]!= 1&&a[x+1][y]!= 1&&a[x+1][y+1]!= 1)u.add_edge(s,(x-1)*m+y,1,id++),ans++;
if(a[x][y]!=-1&&a[x][y+1]!=-1&&a[x+1][y]!=-1&&a[x+1][y+1]!=-1)u.add_edge((x-1)*m+y+n*m,t,1,id++),ans++;
for(int dx=x-1;dx<=x+1;dx++)
for(int dy=y-1;dy<=y+1;dy++)
if(1<=dx&&dx<n&&1<=dy&&dy<m)u.add_edge((x-1)*m+y,(dx-1)*m+dy+n*m,inf,id++);
}
cout<<ans-u.dinic(s,t)<<endl;
memset(vis,0,sizeof(vis));
dfs(s);
for(int x=1;x<n;x++)
for(int y=1;y<m;y++)
{
if(a[x][y]!= 1&&a[x][y+1]!= 1&&a[x+1][y]!= 1&&a[x+1][y+1]!= 1&& vis[(x-1)*m+y ])a[x][y]=a[x+1][y]=a[x][y+1]=a[x+1][y+1]=-1;
if(a[x][y]!=-1&&a[x][y+1]!=-1&&a[x+1][y]!=-1&&a[x+1][y+1]!=-1&&!vis[(x-1)*m+y+n*m])a[x][y]=a[x+1][y]=a[x][y+1]=a[x+1][y+1]= 1;
}
for(int x=1;x<=n;x++)
{
for(int y=1;y<=m;y++)
a[x][y]*=((x^y)&1)?-1:1,cout<<(a[x][y]==-1?'B':'W');
cout<<endl;
}
}
/*
1
3 3
BW?
W?B
?BW
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 32ms
memory: 89968kb
input:
3 2 2 ?? ?? 3 3 BW? W?B ?BW 3 3 BW? W?W ?W?
output:
1 WB BW 1 BWW WWB WBW 4 BWB WBW BWB
result:
ok ok (3 test cases)
Test #2:
score: -100
Time Limit Exceeded
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 ...