poj 2239 Selecting Courses 匹配

课程为x节点,时间为y节点,连边求最大匹配

#include <stdio.h>
#include <string.h>
#define maxn 310
int nx,ny;
int g[maxn][maxn],ans,sx[maxn],sy[maxn];
int cx[maxn],cy[maxn];
int path(int u)
{
    sx[u]=1;
    int v;
    for(v=1;v<=ny;v++)
    {
        if(g[u][v]>0&&!sy[v])
        {
            sy[v]=1;
            if(!cy[v]||path(cy[v]))
            {
                cx[u]=v;cy[v]=u;
                return 1;
            }
        }
    }
    return 0;
}
int solve()
{
    ans=0;
    int i;
    memset(cx,0,sizeof(cx));
    memset(cy,0,sizeof(cy));
    for(i=1;i<=nx;i++)
    {
        if(!cx[i])
        {    
            memset(sx,0,sizeof(sx));
            memset(sy,0,sizeof(sy));
            ans+=path(i);
        }
    }
    return 0;
}

int main()
{
	int n,i,j,x,y;
	while(scanf("%d",&n)!=EOF)
	{
		memset(g,0,sizeof(g));
		nx=n;ny=7*12;
		int num;
		for(i=1;i<=n;i++)
		{
			scanf("%d",&num);
			for(j=1;j<=num;j++)
			{
				scanf("%d%d",&x,&y);
				g[i][(x-1)*12+y]=1;
			}
		}
		solve();
		printf("%d\n",ans);
	}
	return 0;
}


 

 

posted @ 2014-03-08 15:56  贝尔摩德  阅读(146)  评论(0编辑  收藏  举报