ZOJ - 1654Place the Robots 建图+二分图最大匹配
https://vjudge.net/problem/ZOJ-1654
分析: 每一个空地使得这个位置相邻的空地不能被选择,将空地分为一个一个块,即求最大点独立集,即最大匹配
想了好久没想到,一开始直接对点建图,死的不知道有多惨,愣是想了一个小时不知道怎么建图
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<queue>
#include<map>
#define ll long long
#define pb push_back
#define rep(x,a,b) for (int x=a;x<=b;x++)
#define repp(x,a,b) for (int x=a;x<b;x++)
#define W(x) printf("%d\n",x)
#define WW(x) printf("%lld\n",x)
#define pi 3.14159265358979323846
#define mem(a,x) memset(a,x,sizeof a)
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
using namespace std;
const int maxn=2500+7;
const int INF=1e9;
const ll INFF=1e18;
int mapp[maxn][maxn],hhh[51][51],ggg[51][51],n,m,xx,yy,t;
char s[51][51];
int linker[maxn];
bool vis[maxn];
bool dfs(int x)
{
for (int i=1;i<=yy;i++)
{
if (mapp[x][i]&&!vis[i])
{
vis[i]=true;
if (linker[i]==-1||dfs(linker[i]))
{
linker[i]=x;
return true;
}
}
}
return false;
}
int count_()
{
int res=0;
for (int i=1;i<=xx;i++)
{
mem(vis,false);
if (dfs(i))res++;
}
return res;
}
int main()
{
scanf("%d",&t);
rep(q,1,t)
{
mem(linker,-1);mem(mapp,0);mem(hhh,0);mem(ggg,0);
scanf("%d%d",&m,&n);
repp(i,0,m)scanf("%s",s[i]);
xx=0;yy=0;
for (int i=0;i<m;i++)
{
int k=0;
while(k<n)
{
if (s[i][k]=='o')
{
xx++;
while(k<n&&s[i][k]!='#')
{
hhh[i][k]=xx;
k++;
}
}
else k++;
}
}
for (int i=0;i<n;i++)
{
int k=0;
while(k<m)
{
if (s[k][i]=='o')
{
yy++;
while(k<m&&s[k][i]!='#')
{
ggg[k][i]=yy;
k++;
}
}
else k++;
}
}
repp(i,0,m)
repp(j,0,n)
if (s[i][j]=='o')mapp[hhh[i][j]][ggg[i][j]]=1;
printf("Case :%d\n",q);
W(count_());
}
return 0;
}