#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pll pair<int, int>
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define endl '\n'
#define ld long double
vector<tuple<int,int,int>> hd={{-1,1,1},{0,2,0},{1,1,1},{1,-1,1},{0,-2,0},{-1,-1,1}},
vd={{-2,0,1},{-1,1,0},{1,1,0},{2,0,1},{1,-1,0},{-1,-1,0}};
signed main(){
int h,w;cin>>h>>w;
vector<vector<int>> m(h+2, vector<int>(w+2, 1));
for(int i=1;i<=h;i++){
for(int j=1;j<=w;j++){
char c;cin>>c;
if(c=='R')m[i][j]=0;
else if(c=='G')m[i][j]=1;
else m[i][j]=2;
}
}
vector<vector<array<int, 2>>> v(h+2, vector<array<int,2>>(w+2, array<int,2>{0, 0}));
vector<vector<int>> al(h*w/2);
int nw=1;
for(int i=1;i<=h;i++){
for(int j=1;j<=w;j++){
if(m[i][j]!=1)continue;
bool hori=false;
if((m[i][j-1]==0 and m[i][j+1]==2) or (m[i][j+1]==0 and m[i][j-1]==2)){
for (auto [dx,dy,t]:hd){
int nx=i+dx, ny=j+dy;
if(nx<=0 or nx>h or ny<=0 or ny>w or !v[nx][ny][t])continue;
al[nw].pb(v[nx][ny][t]);
al[v[nx][ny][t]].pb(nw);
}
v[i][j][0]=nw;
hori=true;
nw++;
//~ printf("%lld %lld, hori\n",i,j);
}
if((m[i-1][j]==0 and m[i+1][j]==2) or (m[i+1][j]==0 and m[i-1][j]==2)){
for (auto [dx,dy,t]:vd){
int nx=i+dx, ny=j+dy;
if(nx<=0 or nx>h or ny<=0 or ny>w or !v[nx][ny][t])continue;
al[nw].pb(v[nx][ny][t]);
al[v[nx][ny][t]].pb(nw);
}
v[i][j][1]=nw;
if(hori) {
al[nw].pb(nw-1);
al[nw-1].pb(nw);
}
nw++;
//~ printf("%lld %lld, vert\n",i,j);
}
}
}
vector<int> clr(nw+5, -1);
array<int,2> sm={0,0};
auto dfs=[&](auto && dfs, int x, int p)->void{
sm[p]++;
for(auto it:al[x]){
//~ printf("at %lld, going to %lld\n", x, it);
if(clr[it]!=-1) {
assert(clr[it]!=clr[x]);
continue;
}
clr[it]=!p;
dfs(dfs, it, !p);
}
};
int ans=0;
for(int i=1;i<nw;i++){
if(clr[i]==-1){
sm[0]=sm[1]=0;
clr[i]=0;
dfs(dfs, i, 0);
ans+=max(sm[0], sm[1]);
}
}
cout<<ans;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |