UVA 12549(p381)----Sentry Robots

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=1100;
struct point
{
    int x,y;
};
point mm[maxn][maxn];
int from[maxn],use[maxn];
int Map[maxn][maxn];
int n,m,t,numx,numy;
//vector<int> g[maxn];
int g[maxn][maxn];
int vis[maxn];
void make()
{
    numx=0,numy=0;
    for(int i=1; i<=n; i++)
    {
        int flag=1;
        for(int j=1; j<=m; j++)
        {
            if(Map[i][j]==1)
            {
                if(flag) numx++;
                mm[i][j].x=numx;
                flag=0;
            }
            else if(Map[i][j]==2) flag=1;
        }
    }
    for(int j=1; j<=m; j++)
    {
        int flag=1;
        for(int i=1; i<=n; i++)
        {
            if(Map[i][j]==1)
            {
                if(flag) numy++;
                mm[i][j].y=numy;
                flag=0;
            }
            else if(Map[i][j]==2) flag=1;
        }
    }
    for(int i=1; i<=n; i++)
        for(int j=1; j<=m; j++)
        {
            if(Map[i][j]==1)
            {
                g[mm[i][j].x][mm[i][j].y+numx]=1;
            }
        }
}
bool dfs(int u)
{
    for(int v = numx; v <=numx+numy; v++)
        if(g[u][v] && !vis[v])
        {
            vis[v] = true;
            if(from[v] == -1 || dfs(from[v]))
            {
                from[v] = u;
                return true;
            }
        }
    return false;
}
int hungary()
{
    int ans = 0;
    memset(from, -1, sizeof(from));
    for(int u = 1; u <=numx; u++)
    {
        memset(vis, false, sizeof(vis));
        if(dfs(u)) ans++;
    }
    return ans;
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        int num,x,y;
        memset(mm,0,sizeof(mm));
        memset(Map,0,sizeof(Map));
        memset(g,0,sizeof(g));
        scanf("%d%d",&n,&m);
        scanf("%d",&num);
        for(int i=0; i<num; i++)
        {
            scanf("%d%d",&x,&y);
            Map[x][y]=1;
        }
        scanf("%d",&num);
        for(int i=0; i<num; i++)
        {
            scanf("%d%d",&x,&y);
            Map[x][y]=2;
        }
        make();
        printf("%d\n",hungary());
    }
    return 0;
}

你可能感兴趣的:(UVA 12549(p381)----Sentry Robots)