QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#359022 | #5071. Check Pattern is Good | Slongod | TL | 0ms | 3564kb | C++14 | 3.5kb | 2024-03-20 10:57:06 | 2024-03-20 10:57:06 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
namespace Slongod{
const int N = 1e4+7 , inf = 0x3f3f3f3f;
namespace wll
{
int econt , s , t , head[N] , hu[N] , dep[N];
struct EDGE{int to , nxt , w;}edge[N*10];
inline void _add(int x , int y , int w){edge[++econt].to = y; edge[econt].nxt = head[x]; head[x] = econt; edge[econt].w = w;}
inline void add(int x , int y , int w){_add(x , y , w); _add(y , x , 0);}
inline bool bfs()
{
for (int i = s; i <= t; i++){dep[i] = -1; hu[i] = head[i];}
queue <int> q; q.push(s); dep[s] = 0;
while(!q.empty()) {
int u = q.front(); q.pop();
for (int i = head[u]; i; i = edge[i].nxt) {
int v = edge[i].to , w = edge[i].w;
if (w and dep[v] == -1) {
dep[v] = dep[u] + 1; q.push(v);
}
}
} return dep[t] != -1;
}
int dfs(int u , int f)
{
if (u == t){return f;} int used = 0;
for (int &i = hu[u]; i; i = edge[i].nxt) {
int v = edge[i].to , w = edge[i].w;
if (dep[v] == dep[u] + 1 and w) {
int tmp = dfs(v , min(f-used , w));
edge[i].w -= tmp; edge[i^1].w += tmp; used += tmp;
}
} if (!used){dep[u] = -1;} return used;
}
inline int dinic(){int re = 0; while(bfs()){re += dfs(s , inf);} return re;}
inline void init(int S , int T){s = S; t = T; for (int i = S; i <= T; i++){head[i] = 0;} econt = 1;}
}
int n , m , s , t , ans; bool vis[N]; char out[105][105];
inline int id(int x , int y){return (x - 1) * m + y;}
inline int rid(int x , int y , int op){return id(n , m) + id(x , y) + op * id(n - 1 , m - 1);}
inline void bfs(int st)
{
for (int i = s; i <= t; i++){vis[i] = 0;}
queue <int> q; q.push(st);
while(!q.empty()) {
int u = q.front(); q.pop();
for (int i = wll::head[i]; i; i = wll::edge[i].nxt) {
int v = wll::edge[i].to , w = wll::edge[i].w;
if (w and !vis[v]){vis[v] = 1; q.push(v);}
}
}
}
void main()
{
int T; cin >> T;
while(T--) {
cin >> n >> m; s = 0; t = rid(n - 1 , m - 1 , 1) + 1; ans = 0; wll::init(s , t);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
char a; cin >> a;
if ((i+j)&1) {
if (a == 'W'){wll::add(s , id(i,j) , inf); wll::add(id(i,j) , t , 1);}
if (a == 'B'){wll::add(id(i,j) , t , inf); wll::add(s , id(i,j) , 1);}
if (a == '?'){wll::add(s , id(i,j) , 1); wll::add(id(i,j) , t , 1);}
} else {
if (a == 'W'){wll::add(id(i,j) , t , inf); wll::add(s , id(i,j) , 1);}
if (a == 'B'){wll::add(s , id(i,j) , inf); wll::add(id(i,j) , t , 1);}
if (a == '?'){wll::add(s , id(i,j) , 1); wll::add(id(i,j) , t , 1);}
}
if (i + 1 <= n and j + 1 <= m) { ans++;
wll::add(rid(i,j,0) , rid(i,j,1) , 1);
wll::add(id(i,j) , rid(i,j,0) , inf);
wll::add(rid(i,j,1) , id(i,j) , inf);
wll::add(id(i,j+1) , rid(i,j,0) , inf);
wll::add(rid(i,j,1) , id(i,j+1) , inf);
wll::add(id(i+1,j) , rid(i,j,0) , inf);
wll::add(rid(i,j,1) , id(i+1,j) , inf);
wll::add(id(i+1,j+1) , rid(i,j,0) , inf);
wll::add(rid(i,j,1) , id(i+1,j+1) , inf);
}
}
} cout << ans - wll::dinic() + n * m << '\n';
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if ((i+j)&1) {
cout << (wll::dep[id(i,j)] != -1 ? 'W' : 'B');
} else {
cout << (wll::dep[id(i,j)] != -1 ? 'B' : 'W');
}
} cout << '\n';
}
}
}
}int main()
{
#ifndef ONLINE_JUDGE
freopen("in.in" , "r" , stdin);
#endif
ios :: sync_with_stdio(0);
cin.tie(0) , cout.tie(0);
return Slongod :: main(),0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3564kb
input:
3 2 2 ?? ?? 3 3 BW? W?B ?BW 3 3 BW? W?W ?W?
output:
1 WB BW 1 BWW WWB WBW 4 BWB WBW BWB
result:
ok ok (3 test cases)
Test #2:
score: -100
Time Limit Exceeded
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 ...