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>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<pi> vpi;
typedef vector<vi> vvi;
const int inf = 0x3f3f3f3f;
const ll linf = 123456789012345678;
const ll mod = 998244353;
#pragma GCC optimize("Ofast")
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " = " << x << endl
int n, m;
vvi a;
vi par;
vi siz;
int find(int a){
if(par[a] == -1) return a;
return (par[a] = find(par[a]));
}
void join(int a, int b){
a = find(a);
b = find(b);
if(a == b) return;
par[a] = b;
siz[b] += siz[a];
siz[a] = 0;
}
int main(){
cin >> n >> m;
a = vvi(n, vi(m));
for(int i = 0; i < n; i++){
string s;
cin >> s;
for(int j = 0; j < m; j++){
if(s[j] == 'G') a[i][j] = 1;
else if(s[j] == 'W') a[i][j] = 2;
}
}
par = vi(n*m*2, -1);
siz = vi(n*m*2, 0);
int ans = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(a[i][j] == 0){
if(i+2<n && a[i+1][j] == 1 && a[i+2][j] == 2) {
if(j-1 >= 0) join(i*m+j+n*m, (i+1)*m+j-1);
if(j-2 >= 0) join(i*m+j+n*m, (i+2)*m+j-2);
ans++;
siz[find(i*m+j)]++;
}
if(j+2<m && a[i][j+1] == 1 && a[i][j+2] == 2){
if(i-1 >= 0) join(i*m+j, (i-1)*m+j+1+n*m);
if(i-2 >= 0) join(i*m+j, (i-2)*m+j+2+n*m);
ans++;
siz[find(i*m+j)]++;
}
}
}
}
int extra = 0;
for(int i = 0; i < n*m*2; i++){
extra+=(siz[i]&1);
}
cout << ans/2+(extra+1)/2 << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |