This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC optimize("fast-math")
//#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<vector<int>> L;
vector<int> matched;
vector<int> used;
vector<bool> da;
int cnt = 1;
int sz = 0;
int szR = 0;
bool dfs(int v) {
    if (used[v] == cnt) return false;
    used[v] = cnt;
    for (int u : L[v]) {
        auto &m = matched[u];
        if (matched[u] == -1 || dfs(m)) {
            matched[u] = v;
            da[v] = true;
            return true;
        }
    }
    return false;
}
void solve() {
    int n, m;
    cin >> n >> m;
    vector<string> d(n);
    vector<vector<int>> comp(n, vector<int>(m, -1));
    for (int i = 0; i < n; i++)
        cin >> d[i];
    for (int i = 0; i < n - 2; i++)
        for (int j = 0; j < m; j++) {
            if (d[i][j] == 'R' && d[i + 1][j] == 'G' && d[i + 2][j] == 'W') {
                comp[i][j] = comp[i + 1][j] = comp[i + 2][j] = sz++;
            }
        }
    L.resize(sz);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m - 2; j++) {
            if (d[i][j] == 'R' && d[i][j + 1] == 'G' && d[i][j + 2] == 'W') {
                int cur = szR++;
                for (int dj = 0; dj < 3; dj++) {
                    if (comp[i][j + dj] != -1) {
                        L[comp[i][j + dj]].push_back(cur);
                    }
                }
            }
        }
    }
    matched.resize(szR, -1);
    used.resize(sz);
    da.resize(sz);
    for (int v = 0; v < sz; v++) {
        if (L[v].empty())
            continue;
        int u = L[v][0];
        if (matched[u] == -1) {
            matched[u] = v;
            da[v] = true;
        }
    }
    for (int v = 0; v < sz; v++) {
        if (!da[v] && dfs(v))
            cnt++;
    }
    int ans = 0;
    for (int v = 0; v < sz; v++)
        ans += da[v];
    cout << sz + szR - ans;
}
signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif
    int t = 1;
//    cin >> t;
    while (t--)
        solve();
    return 0;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |