QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#370859 | #5071. Check Pattern is Good | Graphcity | TL | 1ms | 3808kb | C++20 | 3.3kb | 2024-03-29 17:48:57 | 2024-03-29 17:48:58 |
Judging History
answer
#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rof(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
const int Maxn=1e6,inf=1e9;
const int dx[9]={1,0,-1,0,1,-1,1,-1,0},dy[9]={0,1,0,-1,1,1,-1,-1,0};
int n,m,s,t,vis[Maxn+5],dis[Maxn+5],maxf;
char ch[105][105]; int i1[105][105],i2[105][105];
int v1[105][105],v2[105][105];
struct Node{int frm,to,nxt,w;} Edge[Maxn*2+5];
int tot=1,Head[Maxn+5],cur[Maxn+5];
inline void Addedge(int x,int y,int k)
{
Edge[++tot]=Node{x,y,Head[x],k},Head[x]=tot;
Edge[++tot]=Node{y,x,Head[y],0},Head[y]=tot;
}
inline int bfs()
{
queue<int> q; For(i,1,t) dis[i]=vis[i]=0,cur[i]=Head[i];
dis[s]=0,vis[s]=1,q.push(s);
while(!q.empty())
{
int x=q.front(); q.pop();
for(int i=Head[x];i;i=Edge[i].nxt)
{
int y=Edge[i].to;
if(Edge[i].w && !vis[y])
dis[y]=dis[x]+1,vis[y]=1,q.push(y);
}
} return vis[t];
}
inline int dfs(int x,int flow)
{
if(!flow || x==t) {maxf+=flow; return flow;}
int used=0,res=0;
for(int i=cur[x];i && used<flow;i=Edge[i].nxt)
{
int y=Edge[i].to; cur[x]=i;
if(dis[y]==dis[x]+1 && Edge[i].w)
if(res=dfs(y,min(flow-used,Edge[i].w)))
used+=res,Edge[i].w-=res,Edge[i^1].w+=res;
} return used;
}
inline void dfs(int x)
{
vis[x]=1; for(int i=Head[x];i;i=Edge[i].nxt) if(Edge[i].w)
{int y=Edge[i].to; if(!vis[y]) dfs(y);}
}
inline void Clear() {For(i,1,t) Head[i]=cur[i]=0; tot=1,maxf=0;}
inline void Solve()
{
cin>>n>>m; s=t=0;
For(i,1,n) scanf("%s",ch[i]+1);
For(i,1,n) For(j,1,m) if(i+j&1)
{
if(ch[i][j]=='W') ch[i][j]='B';
else if(ch[i][j]=='B') ch[i][j]='W';
}
For(i,1,n-1) For(j,1,m-1)
{
if(ch[i][j]!='W' && ch[i][j+1]!='W' && ch[i+1][j]!='W' && ch[i+1][j+1]!='W')
v1[i][j]=1; else v1[i][j]=0;
if(ch[i][j]!='B' && ch[i][j+1]!='B' && ch[i+1][j]!='B' && ch[i+1][j+1]!='B')
v2[i][j]=1; else v2[i][j]=0;
}
For(i,1,n-1) For(j,1,m-1) {if(v1[i][j]) i1[i][j]=++s; if(v2[i][j]) i2[i][j]=++s;}
++s,t=s+1;
// For(i,1,n) printf("%s\n",ch[i]+1); printf("\n");
For(i,1,n-1) For(j,1,m-1)
{
if(v1[i][j]) Addedge(i1[i][j],t,1),maxf--;
if(v2[i][j]) Addedge(s,i2[i][j],1),maxf--;
if(v2[i][j]) For(_,0,8)
{
int x=i+dx[_],y=j+dy[_]; if(x<1 || x>n || y<1 || y>m) continue;
if(i1[x][y]) Addedge(i2[i][j],i1[x][y],inf);
}
}
while(bfs()) dfs(s,inf); printf("%d\n",-maxf);
For(i,1,t) vis[i]=0; dfs(s);
For(i,1,n-1) For(j,1,m-1)
{
if(v2[i][j] && vis[i2[i][j]]) ch[i][j]=ch[i][j+1]=ch[i+1][j]=ch[i+1][j+1]='W';
if(v1[i][j] && !vis[i1[i][j]]) ch[i][j]=ch[i][j+1]=ch[i+1][j]=ch[i+1][j+1]='B';
}
For(i,1,n) For(j,1,m) if(ch[i][j]=='?') ch[i][j]='W';
// For(i,1,n) printf("%s\n",ch[i]+1); printf("\n");
For(i,1,n) For(j,1,m) if(i+j&1)
{
if(ch[i][j]=='W') ch[i][j]='B';
else if(ch[i][j]=='B') ch[i][j]='W';
}
For(i,1,n) printf("%s\n",ch[i]+1); Clear();
}
int main()
{
// freopen("1.in","r",stdin);
int T; cin>>T;
while(T--) Solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3808kb
input:
3 2 2 ?? ?? 3 3 BW? W?B ?BW 3 3 BW? W?W ?W?
output:
1 BW WB 1 BWW WBB 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 ...
output:
3 BB BW WW WW BW WB BW WB BB 2 BW WB BW BW WW BW 9 WBBBWBW BWBBWWW WBWBWWB BWWWBWB BBWBBWB WWBBWWB BWBWBWB WWWWBBW BBWBBWW BWWWWBB 3 BWWBWWB WBBWWWB BWWBBBW BBBWBBB 0 B W W B B W W W B B 13 WBWW WWWW WWWB BWBW WWWB BWBW WBWB BWBW WBWW BWBW 8 WBW WWB WBW BWB WBW WBW BWB BWB 0 W B W B 0 WBWB WWBB 3 BW...