QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#359731#5071. Check Pattern is Goodeggegg185WA 2ms8316kbC++143.4kb2024-03-20 20:21:012024-03-20 20:21:02

Judging History

你现在查看的是最新测评结果

  • [2024-03-20 20:21:02]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:8316kb
  • [2024-03-20 20:21:01]
  • 提交

answer

#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
#define int long long
int head[100001],cur[100001],nxt[100001],wei[100001],frm[100010],to[100001],cnt = 2,level[100001],s,t;
int mappa[105][105];
const int inf = 0x3f3f3f3f;
void add(int u,int v,int w) {
	to[cnt] = v,frm[cnt] = u;
	wei[cnt] = w;
	nxt[cnt] = head[u];
	head[u] = cnt;
	cnt++;
	to[cnt] = u,frm[cnt] = v;
	wei[cnt] = 0;
	nxt[cnt] = head[v];
	head[v] = cnt;
	cnt++;
}
int bfs() {
	memset(level,-1,sizeof(level));
	memcpy(cur,head,sizeof(head));
	level[s] = 0;
	queue<int> q;
	q.push(s);
	while(!q.empty()) {
		int top = q.front();
		q.pop();
		for(int i = head[top]; i ; i = nxt[i]) {
			int tp = to[i];
			if(level[tp] == -1 && wei[i] > 0) {
				level[tp] = level[top] + 1;
				q.push(tp);
			}
		}
	}
	if(level[t] == -1) return 0;
	return 1;
}
int dfs(int now,int flow) {
	if(now == t) return flow;
	int remain = flow;
	for(int i = cur[now]; i ; i = nxt[i]) {
		cur[now] = i;
		int tp = to[i],w = wei[i];
		if(w > 0 && level[now]+1 == level[tp]) {
			int c = dfs(tp,min(w,remain));
			remain -= c;
			wei[i] -= c;
			wei[i^1] += c;
		}
	}
	return flow - remain;
}
int dinic() {
	int ans = 0;
	while(bfs()) {
		ans += dfs(s,inf);
	}
	return ans;
}
bool plc[100010];
void dfs2(int now) {
	plc[now] = true;
	for(int i = head[now]; i; i = nxt[i]) {
		if(!plc[to[i]] && wei[i]) dfs2(to[i]);
	}
}
//S is B is 0, T is W is 1
char ss[100010];
signed muin() {
	memset(plc,0,sizeof(plc));
	memset(head,0,sizeof(head));
	cnt = 2; return 0;
}
signed moin() {
	int n,m; cin >> n >> m; n--,m--;
	s = 2*n*m+1,t = 2*n*m+2; int tot = 0;
	for(int i = 1; i <= n+1; i++) {
		cin >> (ss+1); for(int j = 1; j <= m+1; j++) {
			mappa[i][j] = (ss[j] == 'W')^((i+j)&1); mappa[i][j] ^= 1;
			if(ss[j] == '?') mappa[i][j] = -1;
		}
	}
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= m; j++) {
			if(mappa[i][j] != 1 && mappa[i+1][j] != 1 && mappa[i][j+1] != 1 && mappa[i+1][j+1] != 1) add(s,(i-1)*m+j,1),tot++;
			if(mappa[i][j] != 0 && mappa[i+1][j] != 0 && mappa[i][j+1] != 0 && mappa[i+1][j+1] != 0) add(n*m+(i-1)*m+j,t,1),tot++;
			add((i-1)*m+j,n*m+(i-1)*m+j,inf);
			if(i < n) add((i-1)*m+j,n*m+i*m+j,inf),add(i*m+j,n*m+(i-1)*m+j,inf);
			if(j < m) add((i-1)*m+j,n*m+(i-1)*m+(j+1),inf),add((i-1)*m+(j+1),n*m+(i-1)*m+j,inf);
			if(i < n && j < m) add((i-1)*m+j,n*m+i*m+(j+1),inf),add(i*m+(j+1),n*m+(i-1)*m+j,inf);
			if(i > 1 && j < m) add((i-1)*m+j,n*m+(i-2)*m+(j+1),inf),add((i-2)*m+(j+1),n*m+(i-1)*m+j,inf);
		}
	}
	printf("%lld\n",tot-dinic()); dfs2(s);
	if(m)
	for(int i = 2; i <= cnt; i+=2) {
		int x = frm[i],y = to[i];
		int x1 = x/m+1,y1 = x%m; if(y1 == 0) x1--,y1 = m; x1 -= n;
		int x2 = y/m+1,y2 = y%m; if(y2 == 0) x2--,y2 = m;
		if(plc[x] == plc[y]) {
			if(x == s) mappa[x2][y2] = mappa[x2][y2+1] = mappa[x2+1][y2] = mappa[x2+1][y2+1] = 0;
			if(y == t) mappa[x1][y1] = mappa[x1][y1+1] = mappa[x1+1][y1] = mappa[x1+1][y1+1] = 1;
		}
		//cout << x << ' ' << y << ' ' << plc[x] << ' ' << plc[y] << ' ' << s << ' ' << t << endl;
	}
	for(int i = 1; i <= n+1; i++) {
		for(int j = 1; j <= m+1; j++) {
			if(mappa[i][j] == -1) mappa[i][j] = 0;
			mappa[i][j] ^= ((i+j)&1);
			putchar(mappa[i][j]?'W':'B');
		} putchar('\n');
	}
	return 0;
}
signed main() {
	cin.tie(0)->sync_with_stdio(false);
	int T; cin >> T;
	while(T--) {muin();moin();}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 2ms
memory: 8316kb

input:

3
2 2
??
??
3 3
BW?
W?B
?BW
3 3
BW?
W?W
?W?

output:

1
WB
BW
1
WBB
BWW
BWB
4
WBW
BWB
WBW

result:

wrong answer (0, 0) should be B, you output W (test case 2)