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 + 5;
const int maxs = 9e6 + 5;
string s[maxn];
int cntv = 0;
int arr[maxs];
int adj[maxs][3]; int ind[maxs];
bool mark[maxs];
int h[maxs]; int cnt[2];
int GI(int i, int j) {
return i * m + j;
}
void dfs(int v) {
mark[v] = 1;
cnt[h[v] % 2]++;
for (int i = 0; i < ind[v]; i++) {
int u = adj[v][i];
if (!mark[u]) {
h[u] = h[v] + 1;
dfs(u);
}
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> s[i];
fill(arr, arr + n * m, -1);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m - 2; j++) {
if (s[i][j] == 'R' && s[i][j + 1] == 'G' && s[i][j + 2] == 'W') {
for (int j1 : {j, j + 1, j + 2}) {
if (arr[GI(i, j1)] != -1) {
int v1 = arr[GI(i, j1)], v2 = cntv;
adj[v1][ind[v1]++] = v2; adj[v2][ind[v2]++] = v1;
}
else arr[GI(i, j1)] = cntv;
}
cntv++;
}
}
}
for (int i = 0; i < n - 2; i++) {
for (int j = 0; j < m; j++) {
if (s[i][j] == 'R' && s[i + 1][j] == 'G' && s[i + 2][j] == 'W') {
for (int i1 : {i, i + 1, i + 2}) {
if (arr[GI(i1, j)] != -1) {
int v1 = arr[GI(i1, j)], v2 = cntv;
adj[v1][ind[v1]++] = v2; adj[v2][ind[v2]++] = v1;
}
else arr[GI(i1, j)] = cntv;
}
cntv++;
}
}
}
int output = 0;
for (int i = 0; i < cntv; i++) {
if (!mark[i]) {
dfs(i);
output += max(cnt[0], cnt[1]);
cnt[0] = 0; cnt[1] = 0;
}
}
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... |