//基本上能想到二分匹配就能做得出来了。。把第j门课与星期a的第b节课相连,,然后匈牙利算法就直接出来了。。
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int const maxn=500;
int mk[maxn] ;

int nx, ny; //X和Y集合中顶点的个数
int g[maxn][maxn]; //邻接矩阵, X集合和Y集合中顶点间边的信息
int cx[maxn] , cy[maxn];
//cx[i]表示最终求得的最大匹配中与Xi匹配的Y顶点, cy[i]同理
int path(int u)
{
 for(int v = 0 ; v < ny ; v++) //考虑所有Yi顶点v
 {
  if(g[u][v] && !mk[v])
  {
   mk[v] = 1;
   //如果v没有匹配,或者如果v已经匹配了,
   //但从y[v]出发可以找到一条增广路
   if(cy[v] == -1 || path(cy[v]))
   {
    cx[u] = v; //把v匹配给u
    cy[v] = u; //把u匹配给v
    return 1; //找到可增广路
   }
  }
 }
 return 0 ; //如果不存在从u出发的增广路
}
int MaxMatch() //求二部图最大匹配的匈牙利算法
{
 int res(0) ;
 memset(cx , 0xff , sizeof(cx)) ; //从0匹配开始增广
 memset(cy , 0xff , sizeof(cy)) ;
 for(int i = 0 ; i <= nx ; i++)
 {
  if(cx[i] == -1) //从每个未盖点出发进行寻找增广路
  {
   memset(mk , 0 , sizeof(mk)) ;
   res += path(i) ; //每找到一条增广路,可使得匹配数加1
  }
 }
 return res ;
}
int main()
{
 int cas;
 int num;
 int i;
 int a,b;
 while(scanf("%d",&cas)!=EOF)
 {
  nx=cas;ny=84;
  memset(g,0,sizeof(g));
  for(int j=0;j<cas;j++)
  {
   scanf("%d",&num);
   for(i=0;i<num;i++)
   {
    scanf("%d%d",&a,&b);
    g[j][(a-1)*12+b-1]=1;
   }
  }
  printf("%d\n",MaxMatch());
 }
 return 0;
}

posted on 2012-02-02 16:57  →木头←  阅读(242)  评论(0)    收藏  举报