QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#399267 | #5071. Check Pattern is Good | xlwang | WA | 260ms | 3776kb | C++14 | 5.2kb | 2024-04-26 09:04:26 | 2024-04-26 09:04:27 |
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];
inline void clear(){
fr(i,1,nod) tx[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);
}
int vis[Maxn*Maxn*5];
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;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3744kb
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: 260ms
memory: 3776kb
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 BWBBBBB BBBWBBB 0 B W B B B W W W B W 15 BWWW WBWB WWWW WBWW BWBW WWWW WWWW WWWW BWBW WBWW 8 WBW WBW BWB WBW WWW WBW BWB WWW 0 W B B W 1 BBBB WBWB 3 BW...
result:
wrong answer There are 3 check pattern in you output, but you answered 6 (test case 4)