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>
using namespace std;
#define forw(i,a,b) for(int i=a;i<=b;i++)
#define forb(i,a,b) for(int i=a;i>=b;i--)
#define pu push
#define pb push_back
#define fi first
#define se second
#define all(a) a.begin(),a.end()
typedef long long int ll;
typedef pair<int,int> pii;
const ll maxN=3000+5;
const char base[3]={'R','G','W'};
const int tx[2]={1,0};
const int ty[2]={0,1};
bool vis[maxN][maxN],vis1[maxN];
int n,m,match[maxN*maxN],cnt;
char a[maxN][maxN];
vector <int> edge[maxN*maxN];
bool outmap(int x, int y)
{
if (1<=x && x<=n)
if (1<=y && y<=m) return 0;
return 1;
}
bool matching(int u)
{
for(int v:edge[u])
if (!vis1[v])
{
vis1[v]=1;
if (!match[v] || matching(match[v]))
{
match[v]=u;
return 1;
}
}
return 0;
}
void solve()
{
cin>>n>>m;
forw(i,1,n)
forw(j,1,m)
cin>>a[i][j];
forw(i,1,n)
forw(j,1,m)
if (a[i][j]==base[0])
forw(p,0,1)
{
int x=i; int y=j;
bool flag=1;
forw(k,1,2)
{
x+=tx[p]; y+=ty[p];
if (outmap(x,y) || a[x][y]!=base[k]) flag=0;
}
if (!flag) continue;
x=i; y=j;
cnt++;
if (vis[x][y])
{
edge[cnt].pb(vis[x][y]);
edge[vis[x][y]].pb(cnt);
// cout<<cnt<<" "<<vis[x][y]<<endl;
}
vis[x][y]=cnt;
forw(k,1,2)
{
x+=tx[p]; y+=ty[p];
if (vis[x][y])
{
edge[cnt].pb(vis[x][y]);
edge[vis[x][y]].pb(cnt);
}
vis[x][y]=cnt;
}
}
int ans=0;
forw(i,1,cnt)
{
memset(vis1,0,sizeof vis1);
ans+=matching(i);
}
cout<<cnt-ans/2;
return;
}
int main()
{
ios::sync_with_stdio();
cin.tie(0);
//freopen("a.inp","r",stdin);
//freopen("a.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... |