This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// In the name of Allah
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define all(x) (x).begin(),(x).end()
#define len(x) ((ll) (x).size())
#define F first
#define S second
#define pb push_back
#define sep ' '
#define endl '\n'
#define Mp make_pair
#define debug(x) cerr << #x << ": " << x << endl;
#define kill(x) cout << x << '\n', exit(0);
#define set_dec(x) cout << fixed << setprecision(x);
#define file_io(x,y) freopen(x, "r", stdin); freopen(y, "w", stdout);
int n, m;
const int maxn = 3e3 + 1;
const int maxs = 9e6 + 1;
const int maxc = 5e6 + 1;
string ca;
bitset<2> s[maxn][maxn];
int arr[maxs][2];
pair<pii, bool> V[maxc]; int cntv = 0;
bitset<maxc> mark, c4;
int dp[maxc][5];
vector<int> val; int ind[8];
int GI(int i, int j) {
return i * m + j;
}
void upd(int v, int u) {
for (int i : val) {
int b1 = (bool) (i & 1), b2 = (bool) (i & 2), b3 = (bool) (i & 4);
int val0 = b1 * 2 + b2 * 4, val1 = 1 + b1 * 2 + b2 * 4;
if (b1 || (c4[u] && b3)) {
if (ind[val0] != -1) {
dp[v][ind[i]] += dp[u][ind[val0]];
}
}
else {
if (ind[val0] != -1 && ind[val1] != -1) {
dp[v][ind[i]] += max(dp[u][ind[val0]], dp[u][ind[val1]]);
}
else if (ind[val0] != -1) {
dp[v][ind[i]] += dp[u][ind[val0]];
}
else if (ind[val1] != -1) {
dp[v][ind[i]] += dp[u][ind[val1]];
}
}
}
}
void dfs(int v, int p = -1) {
mark[v] = 1;
for (int i : val) {
int b1 = (bool) (i & 1); dp[v][ind[i]] += b1;
}
int i = V[v].F.F, j = V[v].F.S; bool f = V[v].S;
if (f == 0) {
for (int j1 : {j, j + 1, j + 2}) {
if (arr[GI(i, j1)][1] != -1) {
int u = arr[GI(i, j1)][1];
if (!mark[u]) {
dfs(u, v);
upd(v, u);
}
else if (u != p) {
c4[v] = 1;
}
}
}
}
else {
for (int i1 : {i, i + 1, i + 2}) {
if (arr[GI(i1, j)][0] != -1) {
int u = arr[GI(i1, j)][0];
if (!mark[u]) {
dfs(u, v);
upd(v, u);
}
else if (u != p) {
c4[v] = 1;
}
}
}
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
fill(ind, ind + 8, -1);
for (int i = 0; i < 8; i++) {
int b1 = (bool) (i & 1), b2 = (bool) (i & 2), b3 = (bool) (i & 4);
if ((b1 && b2) || (b2 && b3)) continue;
ind[i] = len(val);
val.pb(i);
}
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> ca;
for (int j = 0; j < m; j++) {
if (ca[j] == 'R') s[i][j] = 0;
else if (ca[j] == 'G') s[i][j] = 1;
else s[i][j] = 2;
}
}
for (int i = 0; i < n * m; i++) {
arr[i][0] = -1; arr[i][1] = -1;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m - 2; j++) {
if (s[i][j] == 0 && s[i][j + 1] == 1 && s[i][j + 2] == 2) {
for (int j1 : {j, j + 1, j + 2}) {
arr[GI(i, j1)][0] = cntv;
}
V[cntv++] = Mp(Mp(i, j), 0);
}
}
}
for (int i = 0; i < n - 2; i++) {
for (int j = 0; j < m; j++) {
if (s[i][j] == 0 && s[i + 1][j] == 1 && s[i + 2][j] == 2) {
for (int i1 : {i, i + 1, i + 2}) {
arr[GI(i1, j)][1] = cntv;
}
V[cntv++] = Mp(Mp(i, j), 1);
}
}
}
int output = 0;
for (int i = 0; i < cntv; i++) {
if (!mark[i]) {
dfs(i);
output += max(dp[i][0], dp[i][1]);
}
}
cout << output << endl;
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... |