This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <functional>
using namespace std;
int main(){
cin.tie(0)->sync_with_stdio(0);
int n, m; cin >> n >> m;
vector<pair<int, int>> ed;
int N = 0;
{
string g[n];
for (int i = 0; i < n; i++)
cin >> g[i];
vector<vector<int>> vis(n, vector<int>(m, -1));
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
if (g[i][j] != 'W') continue;
if (i - 1 >= 0 && g[i - 1][j] == 'G' && \
i - 2 >= 0 && g[i - 2][j] == 'R'){
vis[i][j] = vis[i - 1][j] = vis[i - 2][j] = N;
N++;
}
}
}
for (int j = 0; j < m; j++){
for (int i = 0; i < n; i++){
if (g[i][j] != 'W') continue;
if (j - 1 >= 0 && g[i][j - 1] == 'G' && \
j - 2 >= 0 && g[i][j - 2] == 'R'){
if (vis[i][j] != -1)
ed.emplace_back(vis[i][j], N);
if (vis[i][j - 1] != -1)
ed.emplace_back(vis[i][j - 1], N);
if (vis[i][j - 2] != -1)
ed.emplace_back(vis[i][j - 2], N);
N++;
}
}
}
}
vector<vector<int>> g(N);
for (auto &[u, v]: ed){
g[u].push_back(v);
g[v].push_back(u);
}
vector<vector<int>> dp(N, vector<int>(2));
vector<bool> vis(N);
function<void(int)> dfs = [&](int u){
vis[u] = 1;
dp[u][1] = 1;
for (auto &v: g[u]){
if (vis[v]) continue;
dfs(v);
dp[u][0] += max(dp[v][0], dp[v][1]);
dp[u][1] += dp[v][0];
}
};
int ans = 0;
for (int i = 0; i < N; i++){
if (!vis[i]){
dfs(i);
ans += max(dp[i][0], dp[i][1]);
}
}
cout << ans << '\n';
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... |