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 <bits/stdc++.h>
#define pb push_back
using namespace std;
const int MAXN = 3000;
int n, m;
string mp[MAXN+1], ss;
int p[MAXN+1][MAXN+1];
int cnt, tmp, sze;
int ans;
vector <vector<int>> path;
vector <int> vis;
int dfs(int now, bool flg) {
int s = 1;
vis[now] = 1 + flg;
if (flg) tmp++;
for (int i : path[now]) {
if (vis[i]) {
assert(vis[i] == 1 + (!flg));
continue;
}
s += dfs(i, !flg);
}
return s;
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> ss;
mp[i] = ' ' + ss;
}
path.pb(vector<int>());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (i >= 3 && mp[i][j] == 'W' && mp[i-1][j] == 'G' && mp[i-2][j] == 'R') {
cnt++;
path.pb(vector<int>());
if (p[i][j]) {
path[p[i][j]].pb(cnt);
path[cnt].pb(p[i][j]);
} else p[i][j] = cnt;
if (p[i-1][j]) {
path[p[i-1][j]].pb(cnt);
path[cnt].pb(p[i-1][j]);
} else p[i-1][j] = cnt;
if (p[i-2][j]) {
path[p[i-2][j]].pb(cnt);
path[cnt].pb(p[i-2][j]);
} else p[i-2][j] = cnt;
}
if (j >= 3 && mp[i][j] == 'W' && mp[i][j-1] == 'G' && mp[i][j-2] == 'R') {
cnt++;
path.pb(vector<int>());
if (p[i][j]) {
path[p[i][j]].pb(cnt);
path[cnt].pb(p[i][j]);
} else p[i][j] = cnt;
if (p[i][j-1]) {
path[p[i][j-1]].pb(cnt);
path[cnt].pb(p[i][j-1]);
} else p[i][j-1] = cnt;
if (p[i][j-2]) {
path[p[i][j-2]].pb(cnt);
path[cnt].pb(p[i][j-2]);
} else p[i][j-2] = cnt;
}
}
}
vis.resize(cnt+1);
for (int i = 1; i <= cnt; i++) {
if (vis[i]) continue;
tmp = 0;
sze = dfs(i, 1);
ans += max(tmp, sze - tmp);
}
cout << ans << "\n";
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |