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 ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18
#define INF (ll)1e9
#define MOD (ll)(1e9 + 7)
using namespace std;
template<class T1, class T2>
bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}
template<class T1, class T2>
bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}
template<class T1, class T2>
void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}
template<class T1, class T2>
void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}
template<class T>
void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}
const int MAX = 3100;
int N, M;
string a[MAX];
int pos[MAX][MAX][2], timer;
vector<int> adj[MAX * MAX];
bool vis[MAX * MAX];
void dfs(int u, int &sum, int &num1, int c){
sum++;
num1 += c;
for(int v: adj[u]) if(!vis[v]){
vis[v] = true;
dfs(v, sum, num1, !c);
}
}
void solve(){
cin >> N >> M;
for(int i = 1; i <= N; i++){
cin >> a[i];
a[i] = "*" + a[i];
}
for(int i = 1; i <= N; i++){
for(int j = 1; j <= M; j++) if(a[i][j] == 'G'){
bool t1 = i > 1 && i < N && a[i - 1][j] == 'R' && a[i + 1][j] == 'W';
bool t2 = j > 1 && j < M && a[i][j - 1] == 'R' && a[i][j + 1] == 'W';
if(t2){
pos[i][j][0] = ++timer;
if(pos[i - 1][j + 1][1]){
adj[pos[i][j][0]].push_back(pos[i - 1][j + 1][1]);
adj[pos[i - 1][j + 1][1]].push_back(pos[i][j][0]);
}
}
if(t1){
pos[i][j][1] = ++timer;
if(pos[i - 1][j + 1][0]){
adj[pos[i][j][1]].push_back(pos[i - 1][j + 1][0]);
adj[pos[i - 1][j + 1][0]].push_back(pos[i][j][1]);
}
}
if(t1 && t2){
adj[timer].push_back(timer - 1);
adj[timer - 1].push_back(timer);
}
}
}
int ans = 0;
for(int i = 1; i <= timer; i++) if(!vis[i]){
vis[i] = true;
int sum = 0, num1 = 0;
dfs(i, sum, num1, 0);
ans += max(num1, sum - num1);
}
cout << ans << '\n';
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
//i freopen("order.inp","r",stdin);
// freopen("order.out","w",stdout);
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... |