QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#377741 | #5071. Check Pattern is Good | InfinityNS | WA | 45ms | 4176kb | C++20 | 6.2kb | 2024-04-05 17:11:23 | 2024-04-05 17:11:23 |
Judging History
answer
#include<bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
typedef long long ll;
using namespace std;
typedef pair<int,int> pii;
const int mod=998244353;
inline int add(int x,int y){int ret=x+y;if(ret>=mod)ret-=mod;return ret;}
inline int sub(int x,int y){int ret=x-y;if(ret<0)ret+=mod;return ret;}
inline int mul(int x,int y){return ((ll)x*y)%mod;}
inline int step(int base,int pw){int ret=1;while(pw){if(pw&1)ret=mul(ret,base);base=mul(base,base);pw>>=1;}return ret;}
inline int invv(int x){return step(x,mod-2);}
const int maxn=110;
const int maxv=maxn*maxn;
struct edge{
int a,b,f,c;
};
int inf=1e9;
int rez,level[maxv],start[maxv],sink,source;
vector<edge>e;
vector<int>vect[maxv];
void add_edge(int u,int v,int c){
if(c==1)rez++;
vect[u].pb(e.size());
e.pb({u,v,0,c});
vect[v].pb(e.size());
e.pb({v,u,c,c});
///printf("%d %d %d | %d \n",u,v,c,rez);
}
void init_mrezu(){
e.clear();
for(int i=source;i<=sink;i++){
vect[i].clear();
}
}
int bfs(){
for(int i=source;i<=sink;i++)level[i]=0;
level[source]=1;
queue<int>q;
q.push(source);
while(q.size()){
int x=q.front();
q.pop();
for(int i=0;i<vect[x].size();i++){
int id=vect[x][i];
if(e[id].f==e[id].c || level[e[id].b])continue;
level[e[id].b]=level[x]+1;
q.push(e[id].b);
}
}
return level[sink];
}
int sflow(int x,int flow){
if(x==sink)return flow;
for(;start[x]<vect[x].size();start[x]++){
int id=vect[x][start[x]];
if(e[id].f<e[id].c && level[x]+1==level[e[id].b]){
int tmp=sflow(e[id].b,min(flow,e[id].c-e[id].f));
if(tmp){
e[id].f+=tmp;
e[id^1].f-=tmp;
return tmp;
}
}
}
return 0;
}
int get_flow(){
int ret=0;
while(bfs()){
for(int i=source;i<=sink;i++)start[i]=0;
int pom;
while(pom=sflow(source,inf))ret+=pom;
}
return ret;
}
int a[maxn][maxn],n,m,ind[maxn][maxn],b[maxn][maxn];
pii revind[maxv];
void menjaj(int &x){
if(x==0)x=2;
else if(x==2)x=0;
}
void labeluj(){
for(int i=1;i<n;i++){
for(int j=1;j<m;j++){
int cnt[3]={0,0,0};
cnt[a[i][j]]++;cnt[a[i][j+1]]++;
cnt[a[i+1][j]]++;cnt[a[i+1][j+1]]++;
if(cnt[0] && cnt[2])b[i][j]=-1;
else if(cnt[0]){
if(cnt[0]<4)b[i][j]=0;
else{
rez++;
b[i][j]=-1;
}
}
else if(cnt[2]){
if(cnt[2]<4)b[i][j]=2;
else{
rez++;
b[i][j]=-1;
}
}
else b[i][j]=1;
///printf("%d %d %d %d |",b[i][j],cnt[0],cnt[1],cnt[2]);
}
///printf("\n");
}
///printf("BNIZ\n");
}
int get_color(int x){
pii pos=revind[x];
return b[pos.ff][pos.ss];
}
void solve(){
rez=0;
labeluj();
int dx[8]={-1,-1,-1,0,0,1,1,1};
int dy[8]={-1,0,1,-1,1,-1,0,1};
int indcnt=0;
for(int i=1;i<n;i++){
for(int j=1;j<m;j++){
ind[i][j]=++indcnt;
revind[indcnt]={i,j};
if(b[i][j]==1){
++indcnt;
revind[indcnt]={i,j};
}
}
}
source=0;
sink=indcnt+1;
init_mrezu();
for(int i=1;i<n;i++){
for(int j=1;j<m;j++){
if(b[i][j]==0){
add_edge(source,ind[i][j],1);
}
if(b[i][j]==1){
add_edge(source,ind[i][j],1);
add_edge(ind[i][j]+1,sink,1);
add_edge(ind[i][j],ind[i][j]+1,inf);
}
if(b[i][j]==2){
add_edge(ind[i][j],sink,1);
}
if(b[i][j]==-1 || b[i][j]==2)continue;
for(int k=0;k<8;k++){
int idx=i+dx[k];
int idy=j+dy[k];
if(idx<1 || idx>=n || idy<1 || idy>=m || b[idx][idy]==-1 || b[idx][idy]==0)continue;
add_edge(ind[i][j],ind[idx][idy]+(b[idx][idy]==1),inf);
}
}
}
rez-=get_flow();
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)if(a[i][j]==1)a[i][j]=2;
vector<int>cand;
for(int i=source+1;i<sink;i++){
if(level[i] && get_color(i)!=2)cand.pb(i);
if(level[i]){
///printf("%d OPA\n",i);
}
}
for(int i=0;i<cand.size();i++){
pii pos=revind[cand[i]];
///printf("%d %d %d POS\n",pos.ff,pos.ss,cand[i]);
a[pos.ff][pos.ss]=0;
a[pos.ff][pos.ss+1]=0;
a[pos.ff+1][pos.ss]=0;
a[pos.ff+1][pos.ss+1]=0;
}
rez=0;
for(int i=1;i<n;i++){
for(int j=1;j<m;j++){
int cnt[3]={0,0,0};
cnt[a[i][j]]++;cnt[a[i][j+1]]++;
cnt[a[i+1][j]]++;cnt[a[i+1][j+1]]++;
if(cnt[0] && cnt[2])continue;
rez++;
}
}
}
int main(){
///freopen("test.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--){
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++){
string s;
cin>>s;
for(int j=1;j<=m;j++)
if(s[j-1]=='?')a[i][j]=1;
else if(s[j-1]=='W')a[i][j]=0;
else if(s[j-1]=='B')a[i][j]=2;
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if((i+j)%2){
menjaj(a[i][j]);
}
}
}
solve();
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if((i+j)%2){
menjaj(a[i][j]);
}
}
}
printf("%d\n",rez);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(a[i][j]==0)printf("W");
else if(a[i][j]==2)printf("B");
}
printf("\n");
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 4044kb
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: 45ms
memory: 4176kb
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:
wrong answer ans finds a larger answer (test case 29)