Submission #539432

#TimeUsernameProblemLanguageResultExecution timeMemory
539432AntekbDango Maker (JOI18_dango_maker)C++14
100 / 100
739 ms185744 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...