课程为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 ",ans); } return 0; }