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 st first
#define nd second
using namespace std;
using ll=long long;
const int N=3005, M=3e6+5;
const ll INF=1e18;
vector<int> E[M];
int vis[M], match[M];
int co[N][N], poz[N][N], pio[N][N];
int col=1;
bool dfs(int v){
vis[v]=col;
for(int u:E[v]){
if(!match[u]){
match[u]=v;
return 1;
}
else{
if(vis[match[u]]!=col && dfs(match[u])){
match[u]=v;
return 1;
}
}
}
return 0;
}
int main(){
int n, m;
cin>>n>>m;
for(int i=1; i<=n; i++){
string s;
cin>>s;
for(int j=0; j<m; j++){
if(s[j]=='R')co[i][j+1]=1;
else if(s[j]=='G')co[i][j+1]=2;
else co[i][j+1]=3;
}
}
int ans=0;
for(int s=2; s<=n+m; s++){
int wsk1=0, wsk2=0;
for(int i=1; i<=n; i++){
int j=s-i;
if(j>0 && j<=m){
if(co[i-1][j]==1 && co[i][j]==2 && co[i+1][j]==3){
pio[i][j]=++wsk1;
}
}
}
for(int i=1; i<=n; i++){
int j=s-i;
if(j>0 && j<=m){
if(co[i][j-1]==1 && co[i][j]==2 && co[i][j+1]==3){
poz[i][j]=++wsk2;
}
}
}
for(int i=1; i<=n; i++){
int j=s-i;
if(j>0 && j<=m){
if(pio[i][j] && poz[i-1][j+1])E[pio[i][j]].push_back(poz[i-1][j+1]);
if(pio[i][j] && poz[i+1][j-1])E[pio[i][j]].push_back(poz[i+1][j-1]);
if(pio[i][j] && poz[i][j])E[pio[i][j]].push_back(poz[i][j]);
}
}
ans+=wsk1+wsk2;
for(int i=1; i<=wsk1; i++){
if(dfs(i))ans--;
else{
col++;
if(dfs(i))ans--;
}
}
for(int i=0; i<=wsk1+wsk2; i++){
match[i]=vis[i]=0;
E[i].clear();
}
}
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... |