QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#359773#5071. Check Pattern is Goodeggegg185WA 2015ms9472kbC++143.4kb2024-03-20 20:44:482024-03-20 20:44:48

Judging History

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

  • [2024-03-20 20:44:48]
  • 评测
  • 测评结果:WA
  • 用时:2015ms
  • 内存:9472kb
  • [2024-03-20 20:44:48]
  • 提交

answer

#include <iostream>
#include <queue>
#include <cstring>
#include <cassert>
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);
			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: 100
Accepted
time: 2ms
memory: 9308kb

input:

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

output:

1
WB
BW
1
BWB
WWB
BBW
4
BWB
WBW
BWB

result:

ok ok (3 test cases)

Test #2:

score: -100
Wrong Answer
time: 2015ms
memory: 9472kb

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
BWBBWWW
WBWBWWB
BWWWBWW
BBWBBWB
WWBWWWB
BWBWBBW
WWWWBBW
BBWBBBW
BBWBWWB
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
WBW
BWB
0
W
B
B
W
1
BBWB
WWBB
3
BW...

result:

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