QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#293410 | #5071. Check Pattern is Good | _LAP_ | WA | 178ms | 4260kb | C++14 | 4.5kb | 2023-12-29 07:42:00 | 2023-12-29 07:42:00 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
struct Dinic {
static const int N = 2e4 + 10;
struct edge {
int from, to, cap, flow;
edge(int u, int v, int c, int f): from(u), to(v), cap(c), flow(f) {}
};
vector<edge> edges; vector<int> G[N];
inline void AddEdge(int u, int v, int c) {
static int M;
edges.emplace_back(edge(u, v, c, 0)), edges.emplace_back(edge(v, u, 0, 0));
M = edges.size(); G[u].emplace_back(M - 2), G[v].emplace_back(M - 1);
}
inline void clear() {
edges.clear();
for(int i = 0; i < N; i ++) G[i].clear();
}
int s, t, cur[N], dist[N];
inline bool BFS() {
memset(dist, 0, sizeof dist);
dist[s] = 1; queue<int> Q; Q.push(s);
while(!Q.empty()) {
int u = Q.front(); Q.pop();
for(auto i : G[u]) {
edge &e = edges[i];
if(!dist[e.to] && e.flow < e.cap)
dist[e.to] = dist[e.from] + 1, Q.push(e.to);
}
}
return dist[t];
}
int dfs(int u, int F) {
if(u == t || F <= 0) return F;
int flow = 0;
for(int &i = cur[u]; i < G[u].size(); i ++) {
edge &e = edges[G[u][i]];
if(e.cap == e.flow || dist[e.to] != dist[e.from] + 1) continue;
int f = dfs(e.to, min(F, e.cap - e.flow));
flow += f, F -= f, e.flow += f, edges[G[u][i] ^ 1].flow -= f;
if(!F) break;
}
return flow;
}
int MaxFlow(int s, int t) {
this->s = s, this->t = t;
int ans = 0;
while(BFS()) {
memset(cur, 0, sizeof cur);
ans += dfs(s, INF);
}
return ans;
}
inline void make_array(int n, int m, int A[105][105]) {
static bool vis[N];
memset(vis, false, sizeof vis);
queue<int> Q; Q.push(s), vis[s] = true;
while(!Q.empty()) {
int u = Q.front(); Q.pop();
for(auto i : G[u]) {
edge &e = edges[i];
if(e.cap == e.flow || vis[e.to]) continue;
int x = e.to / 2 / m, y = e.to / 2 % m;
A[x][y] = A[x + 1][y] = A[x][y + 1] = A[x + 1][y + 1] = 1;
vis[e.to] = true, Q.push(e.to);
}
}
for(int i = 0; i < n; i ++)
for(int j = 0; j < m; j ++)
if(A[i][j] == 0) A[i][j] = -1;
}
} solver;
const int N = 105;
int n, m, A[N][N];
char S[N][N];
inline int get(int x, int y) {return x * m + y; }
inline void Add(int a, int b) {
solver.AddEdge(a * 2, b * 2 + 1, INF);
solver.AddEdge(b * 2, a * 2 + 1, INF);
}
inline void solve() {
cin >> n >> m;
for(int i = 0; i < n; i ++) {
cin >> S[i];
for(int j = 0; j < m; j ++)
A[i][j] = (S[i][j] == '?' ? 0 : (S[i][j] == 'W' ? 1 : -1));
}
for(int i = 0; i < n; i += 2)
for(int j = 0; j < m; j ++)
A[i][j] = -A[i][j];
for(int i = 0; i < n; i ++)
for(int j = 0; j < m; j += 2)
A[i][j] = -A[i][j];
int S = get(n - 1, m - 1) * 2 + 2, T = S + 1, ans = 0;
for(int i = 0; i < n - 1; i ++) for(int j = 0; j < m - 1; j ++) {
bool P = true, Q = true;
P = (A[i][j] != -1 && A[i + 1][j] != -1 && A[i][j + 1] != -1 && A[i + 1][j + 1] != -1);
Q = (A[i][j] != 1 && A[i + 1][j] != 1 && A[i][j + 1] != 1 && A[i + 1][j + 1] != 1);
if(P) solver.AddEdge(S, get(i, j) * 2, 1), ans ++;
if(Q) solver.AddEdge(get(i, j) * 2 + 1, T, 1), ans ++;
solver.AddEdge(get(i, j) * 2, get(i, j) * 2 + 1, INF);
if(j < m - 2) Add(get(i, j), get(i, j + 1));
if(j > 0 && i < n - 2) Add(get(i, j), get(i + 1, j - 1));
if(i < n - 2) Add(get(i, j), get(i + 1, j));
if(j < m - 2 && i < n - 2) Add(get(i, j), get(i + 1, j + 1));
}
ans -= solver.MaxFlow(S, T);
cout << ans << '\n';
solver.make_array(n, m, A), solver.clear();
for(int i = 0; i < n; i += 2)
for(int j = 0; j < m; j ++)
A[i][j] = -A[i][j];
for(int i = 0; i < n; i ++)
for(int j = 0; j < m; j += 2)
A[i][j] = -A[i][j];
for(int i = 0; i < n; i ++) {
for(int j = 0; j < m; j ++)
cout << (A[i][j] == 1 ? 'W' : 'B');
cout << '\n';
}
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
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: 4260kb
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: 178ms
memory: 4256kb
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 WBBWBWW BWWBWBB BWBWBWW WBWBWBB BWBWWWW WBWBBWB WWWWBBW BBWBBWW BBWBWBB 6 BBWBWBW WWBWBWB BBWBWBW BWBWBWB 0 B W B B B W W W B W 15 BWWW WWBW WBWB WWBW WBWB BWBW WBWB BWBW WBWB BWBW 8 WBW BWB WBW BWB WBW BWB BWB WWW 0 W B B W 1 WBWB BWBW 3 BW...
result:
wrong answer (1, 3) should be B, you output W (test case 3)