http://poj.org/problem?id=1469
水题,直接套模版,注意学生可以见习0门课程,所以必须p匹配n而不是n匹配p。检查匹配数是不是p即可。
还有一点,今天才发现,二部图 建图的时候,具体看下面注释吧...
贴代码
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 305
int p,n;
int mat[N][N];
int cx[N],cy[N];
bool mk[N];
int path(int u)
{
int v;
for(v=1;v<=n;v++)
{
if(mat[u][v]&&!mk[v])
{
mk[v]=1;
if(cy[v]==-1||path(cy[v]))
{
cx[u]=v;
cy[v]=u;
return 1;
}
}
}
return 0;
}
int MaxMatch()
{
int res=0,i;
memset(cx,-1,sizeof(cx));
memset(cy,-1,sizeof(cy));
for(i=1;i<=p;i++)
{
if(cx[i]==-1)
{
memset(mk,0,sizeof(mk));
res+=path(i);
}
}
return res;
}
int main()
{
//freopen("poj 1469.txt","r",stdin);
int i,j,ncase,num,v,ans;
scanf("%d",&ncase);
while(ncase--)
{
memset(mat,0,sizeof(mat));
scanf("%d%d",&p,&n);
for(i=1;i<=p;i++)
{
scanf("%d",&num);
for(j=0;j<num;j++)
{
scanf("%d",&v);
mat[i][v]=1;
/*这里不是mat[i][v]=mat[v][i]=1;要和无向图建图区分啊,
无向图节点编号1,2,3...而二部图,分两块,1,2,3... 1,2,3...
或者说邻接矩阵里mat[i][j],i,j有固定的属于哪部分的意义*/
}
}
ans=0;
ans=MaxMatch();
if(ans==p)printf("YES\n");
else printf("NO\n");
}
return 0;
}