이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#pragma GCC target("avx2")
#pragma GCC optimize("Ofast,unroll-loops,inline,fast-math")
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
struct Edge {
int to, f, c, rev;
};
vector<vector<Edge>> g;
vector<int> ptr, dst;
void add_edge(int v, int to) {
g[v].push_back({to, 0, 1, (int)g[to].size()});
g[to].push_back({v, 0, 0, (int)g[v].size() - 1});
}
int s, t;
int dfs(int v, int cmin) {
if (v == t)
return cmin;
for (; ptr[v] < (int)g[v].size(); ++ptr[v]) {
auto& [to, f, c, rev] = g[v][ptr[v]];
if (c - f < cmin || dst[to] != dst[v] + 1)
continue;
int delt = dfs(to, cmin);
if (delt > 0) {
f += delt;
g[to][rev].f -= delt;
return delt;
}
}
return 0;
}
bool bfs(int cmin) {
queue<int> q;
q.push(s);
dst.assign(g.size(), INF);
dst[s] = 0;
while (!q.empty()) {
int v = q.front();
q.pop();
for (auto& [to, f, c, rev] : g[v]) {
if (c - f < cmin || dst[to] != INF)
continue;
dst[to] = dst[v] + 1;
q.push(to);
}
}
return dst[t] != INF;
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n, m;
cin >> n >> m;
vector<vector<int>> table(n, vector<int>(m));
for (auto& r : table) {
string s;
cin >> s;
for (int j = 0; j < m; ++j) {
if (s[j] == 'R')
r[j] = 0;
else if (s[j] == 'G')
r[j] = 1;
else
r[j] = 2;
}
}
g.resize(2 * n * m + 2);
auto encode = [&](int x, int y) {
return x * m + y;
};
s = 2 * n * m, t = 2 * n * m + 1;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (table[i][j] == 0)
add_edge(s, encode(i, j));
else if (table[i][j] == 1)
add_edge(encode(i, j), n * m + encode(i, j));
else
add_edge(n * m + encode(i, j), t);
if (table[i][j])
continue;
if (j + 2 < m && table[i][j + 1] == 1 && table[i][j + 2] == 2) {
add_edge(encode(i, j), encode(i, j + 1));
add_edge(n * m + encode(i, j + 1), n * m + encode(i, j + 2));
}
if (i + 2 < n && table[i + 1][j] == 1 && table[i + 2][j] == 2) {
add_edge(encode(i, j), encode(i + 1, j));
add_edge(n * m + encode(i + 1, j), n * m + encode(i + 2, j));
}
}
}
int maxflow = 0;
while (bfs(1)) {
ptr.assign(g.size(), 0);
while (int delt = dfs(s, 1)) {
maxflow += delt;
ptr.assign(g.size(), 0);
}
}
cout << maxflow;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |