QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#359703 | #5071. Check Pattern is Good | eggegg185 | WA | 2ms | 8904kb | C++14 | 3.4kb | 2024-03-20 20:06:34 | 2024-03-20 20:06:35 |
Judging History
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);
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);
}
}
cout << tot-dinic() << endl; dfs2(s);
if(m)
for(int i = 2; i <= cnt; i++) {
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;
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 2ms
memory: 8904kb
input:
3 2 2 ?? ?? 3 3 BW? W?B ?BW 3 3 BW? W?W ?W?
output:
1 1 4 WB BW BWB WWB BBW BWB WBW BWB
result:
wrong answer Token parameter [name=g[i]] equals to "1", doesn't correspond to pattern "[BW]{1,100}" (test case 1)