QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#399269 | #5071. Check Pattern is Good | xlwang | TL | 2031ms | 4376kb | C++14 | 5.2kb | 2024-04-26 09:07:43 | 2024-04-26 09:07:43 |
Judging History
answer
#include<bits/stdc++.h>
#define fr(i,j,k) for(register int i=j;i<=k;++i)
#define rf(i,j,k) for(register int i=j;i>=k;--i)
#define foredge(i,j) for(register int i=head[j];i;i=e[i].nxt)
#define pb push_back
#define Times printf("Time:%.3lf\n",clock()/CLOCKS_PER_SEC)
using namespace std;
inline int read(){
int x=0;
bool f=0;
char c=getchar();
while(!isdigit(c)) f|=(c=='-'),c=getchar();
while(isdigit(c)) x=(x<<3)+(x<<1)+(c^48),c=getchar();
return f?-x:x;
}
inline void write(int x){
if(x<0){putchar('-');x=-x;}
if(x>9)write(x/10);
putchar(x%10+'0');
}
inline void writeln(int x){write(x); puts("");}
inline void writepl(int x){write(x); putchar(' ');}
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
inline int randfind(int l,int r){return rnd()%(r-l+1)+l;}
const int Maxn=1e2+10,inf=1e7;
namespace FLOW{
static const int Maxn=3e6+10;
struct node{
int nxt,to,d;
}e[Maxn<<1];
int head[Maxn],cnt=1;
inline void adde(int u,int v,int w){++cnt;e[cnt].nxt=head[u];head[u]=cnt;e[cnt].to=v;e[cnt].d=w;}
inline void add(int u,int v,int w){adde(u,v,w);adde(v,u,0);}
int vis[Maxn],dis[Maxn],now[Maxn],nod,S,T;
inline bool bfs(){
// cerr<<S<<' '<<T<<' '<<nod<<endl;
fr(i,1,nod) dis[i]=inf,vis[i]=0,now[i]=head[i];
queue<int> q;dis[S]=0;q.push(S);
while(!q.empty()){
int x=q.front();q.pop();vis[x]=0;
for(register int i=head[x];i;i=e[i].nxt){
int y;y=e[i].to;if(!e[i].d) continue;
if(dis[y]>dis[x]+1){
dis[y]=dis[x]+1;
if(!vis[y]) vis[y]=1,q.push(y);
}
}
}
return dis[T]!=inf;
}
inline int check(int x,int flow){
if(!flow || x==T) return flow;
int ans=0;
for(register int &i=now[x];i;i=e[i].nxt){
int y;y=e[i].to;if(!flow) break;
if(!(dis[y]==dis[x]+1) || !e[i].d) continue;
int k=check(y,min(flow,e[i].d));
ans+=k;flow-=k;e[i].d-=k,e[i^1].d+=k;
}
return ans;
}
inline int getans(){
int ans=0;
while(bfs()) {
// cerr<<ans<<endl;
ans+=check(S,inf);
}return ans;
}
inline void clear(){fr(i,1,nod) head[i]=0;cnt=1;}
}
int n,m;
int nod,S,T;
char c[Maxn][Maxn];
int in[Maxn][Maxn],out[Maxn][Maxn],id1[Maxn][Maxn],id2[Maxn][Maxn];
int id[Maxn][Maxn];
int tx[Maxn*Maxn],ty[Maxn*Maxn];
int vis[Maxn*Maxn*5];
inline void clear(){
fr(i,1,nod) tx[i]=0,vis[i]=0;
nod=S=T=0;FLOW::clear();
}
inline void add(int u,int v,int w){
// cout<<u<<' '<<v<<' '<<w<<endl;
FLOW::add(u,v,w);
}
inline void dfs(int x){
if(vis[x]) return ;
vis[x]=1;
if(tx[x]) c[tx[x]][ty[x]]='B';
for(register int i=FLOW::head[x];i;i=FLOW::e[i].nxt) if(FLOW::e[i].d) dfs(FLOW::e[i].to);
}
inline void work(){
n=read();m=read();clear();
fr(i,1,n) scanf("%s",c[i]+1);
S=1;T=nod=2;
fr(i,1,n) fr(j,1,m) in[i][j]=++nod,tx[nod]=i,ty[nod]=j,out[i][j]=++nod;
fr(i,1,n-1) fr(j,1,m-1) id1[i][j]=++nod,id2[i][j]=++nod;
fr(i,1,n) fr(j,1,m){
if((i+j)&1) continue;
if(c[i][j]=='B') c[i][j]='W';
else if(c[i][j]=='W') c[i][j]='B';
}
// fr(i,1,n){
// fr(j,1,m) putchar(c[i][j]);
// puts("");
// }
int ans=0;
int N=1e5;
fr(i,1,n) fr(j,1,m) id[i][j]=0;
fr(i,1,n) fr(j,1,m) {
add(in[i][j],out[i][j],inf);
if(c[i][j]=='B'){
add(S,in[i][j],inf);
continue;
}
if(c[i][j]=='W'){
add(out[i][j],T,inf);
continue;
}
add(S,in[i][j],N);add(out[i][j],T,N);ans+=N;
}
fr(i,1,n-1) fr(j,1,m-1){
add(S,id1[i][j],1);add(id2[i][j],T,1);
fr(p1,i,i+1) fr(p2,j,j+1) add(id1[i][j],in[p1][p2],inf),add(out[p1][p2],id2[i][j],inf);
}
// cerr<<S<<' '<<T<<' '<<nod<<endl;
FLOW::S=S,FLOW::T=T,FLOW::nod=nod;
ans+=2*(n-1)*(m-1);
// cout<<ans<<endl;
writeln(ans-FLOW::getans());
dfs(S);
// for(register int i=FLOW::head[S];i;i=FLOW::e[i].nxt){
// int y;y=FLOW::e[i].to;
// if(!FLOW::e[i].d) continue;
// if(tx[y]) c[tx[y]][ty[y]]='B';
// }
fr(i,1,n) fr(j,1,m){
if(c[i][j]!='?') continue;
// cout<<FLOW::e[id[i][j]].d<<' '<<FLOW::e[id[i][j]+2].d<<endl;
c[i][j]='W';
}
// fr(i,1,n) fr(j,1,m){
// if(c[i][j]!='?') continue;
// // cout<<FLOW::e[id[i][j]].d<<' '<<FLOW::e[id[i][j]+2].d<<endl;
// if(FLOW::e[id[i][j]].d==0) c[i][j]='B';
// else c[i][j]='W';
// }
// fr(i,1,n){
// fr(j,1,m) putchar(c[i][j]);
// puts("");
// }
fr(i,1,n){
fr(j,1,m){
if((i+j)&1) putchar(c[i][j]);
else {
if(c[i][j]=='B') putchar('W');
else putchar('B');
}
}
puts("");
}
}
inline void init(){
int t=read();
while(t--) work();
}
signed main(){
// freopen("input.in","r",stdin);
// freopen("output.out","w",stdout);
init();
// printf("\nTIME:%.3lf",(double)clock()/CLOCKS_PER_SEC);
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3828kb
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: 0
Accepted
time: 276ms
memory: 4104kb
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:
ok ok (10000 test cases)
Test #3:
score: 0
Accepted
time: 271ms
memory: 3868kb
input:
10000 9 6 ?B?W?W WWBBWB ?WB?BW B?W?W? WW??W? B???BW ?W?WW? W?B?B? ?W?BB? 10 1 W ? ? ? ? ? ? ? B W 9 4 ???? ???? W??? ?W?B ??WW ?BW? WW?W ??W? ??W? 3 2 ?W ?B BB 2 7 ?W?BWWB ??W???W 9 9 ?BW?WWW?W BW?WBBWWW W?W????WW W??WW??WW W?BWB?B?W ??BB?WWWW W???WBW?W WWW???WWW B?WWWWWW? 8 10 W??BWWW??B ?BWBWBW?BW...
output:
21 BBWWBW WWBBWB BWBWBW BBWBWB WWBWWB BBWBBW BWBWWB WBBWBW BWWBBW 0 W W B W B W B W B W 15 WBWB BWBW WBWB BWBB WBWW WBWB WWBW WBWB BWWW 1 BW WB BB 4 BWBBWWB WBWWBBW 20 WBWBWWWWW BWBWBBWWW WBWBBWBWW WWBWWBWWW WBBWBWBWW BWBBWWWWW WBWBWBWWW WWWWBWWWW BWWWWWWWB 28 WWBBWWWWWB WBWBWBWBBW BWBWBWBWBW WBWWBW...
result:
ok ok (10000 test cases)
Test #4:
score: 0
Accepted
time: 267ms
memory: 4068kb
input:
10000 7 7 ?B??BBW ????BB? WBBB??B WW?B??? ?B??BBB BBWB??B B???BB? 10 6 W?WW?? W??W?? ?WWWW? ?WW?WW WW??W? W????? W?WW?? WW???W WWW??W ?W??W? 2 6 ?B??W? B???BB 1 8 ??BWB?W? 5 2 WB W? B? BB ?W 7 5 W???? ?WW?? ???W? WWWW? W?W?W ?W?B? W?WWB 8 5 B?WBW B??WW WWW?B WBBWB BW?WW B?W?B ??WWB BBW?B 10 4 WWWW ?...
output:
15 WBWBBBW BWBWBBW WBBBBWB WWWBWBW BBBWBBB BBWBWWB BWBWBBW 13 WWWWWB WBWWBW BWWWWB WWWBWW WWBWWW WBWBWB WBWWBW WWBBWW WWWWBW WWWBWB 4 WBWBWW BWBWBB 0 BWBWBWWW 1 WB WB BW BB BW 12 WBBWB BWWBW WBBWB WWWWB WBWBW BWBBW WBWWB 7 BBWBW BWBWW WWWBB WBBWB BWBWW BBWBB BWWWB BBWBB 9 WWWW WBWB WWBW WBWB BWWB BW...
result:
ok ok (10000 test cases)
Test #5:
score: 0
Accepted
time: 274ms
memory: 3812kb
input:
10000 1 1 ? 7 9 W?WB????B ?WB??B??W BBB?W?WB? WWW??WWW? WW?B??W?W ?BWW??WWW B?WW?W?WB 3 7 ??BBBB? BW?WW?? B??B?BW 1 6 ?B?WWB 7 1 W W W B ? W ? 8 8 WW??W?B? WWW????? BB??WWWW ?W???WBW BBW???WB BWBWBWW? ?W?WW??B BB?????W 10 8 WWW?W?BW WB?W?WBW WW?W?WBW WWWW?WW? WBWB?B?W BW?BW??B ??WWBWWB W?BW?BWW W?W?...
output:
0 B 18 WBWBWWBWB BWBWBBWBW BBBBWBWBW WWWWBWWWB WWBBWBWBW WBWWWBWWW BWWWBWBWB 5 WBBBBBW BWBWWWB BBWBBBW 0 BBBWWB 0 W W W B B W B 23 WWBBWWBW WWWWBBWB BBWBWWWW WWBWBWBW BBWBWBWB BWBWBWWB BWBWWBWB BBWBBWBW 19 WWWBWBBW WBWWBWBW WWBWBWBW WWWWBWWB WBWBWBBW BWBBWBWB WBWWBWWB WWBWWBWW WBWBWWWW WWWWBBWB 0 WB...
result:
ok ok (10000 test cases)
Test #6:
score: 0
Accepted
time: 273ms
memory: 3876kb
input:
10000 9 1 W B ? B W W ? W B 1 10 W??????BWB 5 8 ??W??WB? ?B?WWB?W ??????B? BB??BBBB WB??BBB? 6 2 ?B ?? WB ?B WW W? 1 10 WW??BW?BW? 4 3 BW? ??? B?B ??W 10 10 WW?BBW?BW? WW?BW????? ?WWBW?WB?W ???B?BBBBB ??BBBB?BBW ?WW??W?WBB W??BB?WBBB BBWBW?WBBW ?W????BWB? ??BW??WBWB 1 6 ??B??? 6 5 WBB?W ?WWWW WWWW? ...
output:
0 W B B B W W B W B 0 WWBWBWBBWB 10 BWWWBWBW WBWWWBWW BWBWBWBW BBWBBBBB WBBWBBBW 2 WB BW WB WB WW WB 0 WWBWBWBBWW 6 BWB WBW BWB WBW 26 WWBBBWWBWB WWWBWBBWBW BWWBWWWBWW WBWBWBBBBB BWBBBBWBBW WWWBWWBWBB WWBBBWWBBB BBWBWBWBBW BWBWBWBWBW WBBWWBWBWB 0 BWBWBW 4 WBBWW BWWWW WWWWB WWWBW WWBBW WBWWB 0 B B B ...
result:
ok ok (10000 test cases)
Test #7:
score: 0
Accepted
time: 1049ms
memory: 4072kb
input:
10000 10 10 ?W?WW?W??W ?BWBW?BBWW ?BB?WWW?W? W?B?WWWWWW ?BWW?WWW?W BWWWWWWW?W WBBWW??B?? W??WW?W??W WWWW?WW?W? ?W?BWW?WW? 10 10 WB?WBBWWWB ?WWWW?WB?? ?B?BWW?BW? WBWBW??W?W B?WB?WBWWB WWBWBBWW?? ??WBBWBWBW WWB??WWBWB B?BWWWWBWW WW?WWWBWWB 10 10 ??W????WW? ?WW?W???W? ??W??WW?W? WW??WW?WW? ?W??WW?WW? ?...
output:
20 BWBWWBWWBW WBWBWWBBWW BBBWWWWWWW WWBBWWWWWW WBWWBWWWBW BWWWWWWWBW WBBWWWBBWB WBWWWBWWBW WWWWBWWBWB WWWBWWBWWB 24 WBBWBBWWWB BWWWWBWBBW WBBBWWBBWB WBWBWBWWBW BWWBBWBWWB WWBWBBWWWB BBWBBWBWBW WWBWWWWBWB BWBWWWWBWW WWWWWWBWWB 33 WBWWBWBWWW BWWBWBWBWB WBWWBWWBWW WWBBWWBWWB BWBBWWBWWB WWWWBWWWBW BWBBW...
result:
ok ok (10000 test cases)
Test #8:
score: 0
Accepted
time: 1065ms
memory: 4108kb
input:
10000 10 10 ?BBBBWBBB? ??W???WB?? BB?W???BB? ?B???BBB?? W??BB?WBBB ?B?B???W?W ?????BB??? ?BW???B??? ???BBB??BB BWBBBBBBB? 10 10 BWW?WWB?BW ??B?WBBBWB B??BB??BWB BW?BWB???W ?WB?WWW?W? B??B??W?BB ?WBB?WBB?B BB??BBWBW? WB??WBB?BW ?B???B?W?? 10 10 ??WWWB??BB ?WW???WBWW ???W??W?WW ?W?B?W?W?? WWB?WBB??W B...
output:
34 WBBBBWBBBW BWWBWBWBWB BBBWBWBBBW WBWBWBBBWB WWBBBWWBBB WBWBWBBWBW BWBWBBBBWB WBWBWWBWBW BWBBBBWBBB BWBBBBBBBB 31 BWWBWWBWBW WBBWWBBBWB BWWBBWWBWB BWWBWBBWBW BWBWWWWBWB BBWBWBWWBB BWBBBWBBWB BBWWBBWBWB WBWBWBBWBW WBBWBBWWWB 33 WBWWWBBWBB BWWBBWWBWW WBBWWBWBWW BWWBBWBWBB WWBWWBBBWW BBWBWBWWWW BWBWB...
result:
ok ok (10000 test cases)
Test #9:
score: 0
Accepted
time: 61ms
memory: 3760kb
input:
10000 1 100 WWW?BWB?BB?BBW?BWBB?W??B?B?BWWBWB?WWB??BBBBB??BBBBB?BBBWBWWW?B?BBBWW??BBBW???B???W??W??BW?B?B?W??WB? 1 100 ?WBW?WB?BBBB?BWBWB???WWB?BBB?BBW?B?B??W?B??BBW??WBBW???WW?BBBB?WWB?WBB???WBBB?BBW?W??BW?B??BBBBBBBWB 1 100 W?????BBB?BB?BB?????BWWWB?B???BB??????B??BWW???B??B?B???????BBB??B?BBB???B...
output:
0 WWWWBWBWBBBBBWBBWBBWWWBBBBBBWWBWBWWWBWBBBBBBBWBBBBBWBBBWBWWWBBBBBBWWBWBBBWBWBBBWBWBWWWBBWWBWBWWWBWBW 0 BWBWBWBWBBBBBBWBWBBWBWWBBBBBBBBWBBBBBWWWBWBBBWBWWBBWBWBWWWBBBBBWWBBWBBBWBWBBBWBBWWWWBBWWBWBBBBBBBBWB 0 WWBWBWBBBWBBBBBWBWBWBWWWBWBWBWBBBWBWBWBWBBWWBWBBBWBWBWBWBWBWBBBWBBBBBBBWBBBWBWBWWWBWBWWWBBBW...
result:
ok ok (10000 test cases)
Test #10:
score: 0
Accepted
time: 105ms
memory: 3960kb
input:
10000 100 1 W B B ? B B B ? B B B B W B B B ? ? B ? B B ? W B W ? B ? B W W ? W ? B ? B B ? W W B ? B B ? ? W W B B ? B B ? B ? ? ? W B W B ? B W ? ? B B B B ? B ? W B B W B ? W B B ? B B ? B ? W ? B ? B B ? B W 100 1 ? W ? W ? W W W W W B W ? ? B B ? W ? B W W W W ? ? ? ? W W B W W W W W ? W W W ? ...
output:
0 W B B W B B B W B B B B W B B B B W B W B B B W B W B B B B W W B W B B B B B W W W B W B B B W W W B B B B B W B W B W W B W B B B W W B B B B B W B W W B B W B W W B B W B B B B B W B B B B B W B W 0 B W B W B W W W W W B W B W B B B W B B W W W W B W B W W W B W W W W W B W W W B W B W B B W B ...
result:
ok ok (10000 test cases)
Test #11:
score: 0
Accepted
time: 2031ms
memory: 4376kb
input:
1000 100 10 WWWB?WWW?W W????????W WB?W??WW?W WBB?WWW??B ?WWWW?WW?W ?WWWW?W?WB ?B??W?W??? WW?W?BWWW? WW?B?W?W?W ????WW??W? BWB??WWWW? W??W??WW?? W?WBB??WWW ?WWBBWW?WW ?WBWW?B??? ???WWW???W ??WW?WWW?? ????W?BW?W ???W?W?W?W ?WW?WW?WB? BW??WW?WW? WB?WWWWW?W ??BWW??W?W W??B?WWWW? WWW?W??WWW BBBW??W?W? ??...
output:
335 WWWBBWWWBW WBWBWBWBWW WBBWBWWWBW WBBBWWWBWB BWWWWWWWBW BWWWWBWBWB WBWBWWWWBW WWBWBBWWWB WWBBWWBWBW WBWBWWWBWB BWBWBWWWWB WBWWWBWWBW WBWBBWBWWW BWWBBWWBWW BWBWWBBWBW WBWWWWWBWW BWWWBWWWBW WBWBWBBWWW BWBWBWBWBW WWWBWWWWBW BWBWWWBWWB WBWWWWWWBW BWBWWBBWBW WBWBBWWWWB WWWBWWBWWW BBBWBBWBWB WBWBWWBWBW...
result:
ok ok (1000 test cases)
Test #12:
score: 0
Accepted
time: 2023ms
memory: 4140kb
input:
1000 10 100 BBWB??W??B?BWB?BBB??WWWW?B???WBB??WW???WWBW?B??W??BW?BWBBBW?BWBW?WBW?B?WWB??B?B?BBWWWBBBBW?BB???B?WB ??????WWWBWBBB??W??WW??BWBW??W??????WWWB?B??B?????W?B?????W??BBBBWBW??BWWWB???WBWB?BB?WW?B????W?WWB? WB?BBBW?B??BB?WWW?B??WBB??W?BBW??BB??BB???BB??B??WB??W?B?B?WWWWW?BB??W?W?WBB??B?WWBBB?...
output:
330 BBWBWBWWBBBBWBWBBBWBWWWWBBBWBWBBWBWWBWBWWBWWBWBWBWBWWBWBBBWWBWBWBWBWWBWWWBWWBWBWBBWWWBBBBWWBBWBWBBWB BWBWBWWWWBWBBBBWWWBWWBWBWBWBWWBWBWBBWWWBWBWWBBWBWBWBBWBWBWWBWBBBBWBWBWBWWWBBWBWBWBWBBBWWWBBWWBWBWWBW WBWBBBWWBWBBBWWWWBBWBWBBBWWWBBWBWBBBWBBWBWBBWWBWBWBWBWBBWBBWWWWWBBBBWWBWBWBBBWBWWWBBBWBWWBWBWW...
result:
ok ok (1000 test cases)
Test #13:
score: -100
Time Limit Exceeded
input:
100 100 100 ?WW?WW??WWW??BBW??WW??W???W?W?W?????W?W?BWBW??WBW????W??BB??BW?W??W????WBW?????WWB??BWW????W??W??WW? B?????WW???B?BWWWB?WWW?WB?BB??????W??W?BWWW?BB??WWBWB?WWW????WW?W??BB?BBWB?W????W???BWWW??BBWWW???BW W?BW??????WBB?B????W?BBW????BBW????W?W?????B?B??WW??????WWWWW?W??WW?WBW??W??W????BWWB?...
output:
4352 BWWWWWBWWWWWBBBWBWWWBWWWBWWWWWWWBWBWWWWWBWBWBWWBWWBWBWBWBBBWBWBWBWWWBWBWBWBWBWBWWBBWBWWWBWBWBWWWBWWW BBWBWBWWWBWBWBWWWBWWWWWWBBBBWBWBWBWBWWWBWWWBBBWBWWBWBBWWWBWBWWWBWBWBBBBBWBWWWBWBWBWBBWWWWBBBWWWBWBBW WWBWBWBWBWWBBWBWBWBWBBBWBWBWBBWWBWBWBWBWBWBBBBBWWWWWBWBWWWWWWWWWBWWWWBWWBWBWWWBWBBWWBWBWBWBWW...