题目链接
AC代码
#include<bits/stdc++.h>
using namespace std;
const int N = 520;
int g[N][N],used[N];
int k,p,n;
int match[N];
int found(int x)
{
for(int i=1; i<=n; i++)
{
if(g[x][i]&&!used[i])
{
used[i] = 1;
if(!match[i]||found(match[i]))
{
match[i] = x;
return 1;
}
}
}
return 0;
}
int main()
{
int t;
cin>>t;
while(t--)
{
memset(g, 0, sizeof g);
memset(match,0,sizeof match);
scanf("%d%d",&p,&n);
for(int i=1; i<=p; i++)
{
int stnum;
scanf("%d",&stnum);
for(int j=1; j<=stnum; j++)
{
int st;
scanf("%d",&st);
g[i][st] = 1;
}
}
int ans = 0;
for(int i=1; i<=p; i++)
{
memset(used, 0, sizeof used);
if(found(i)) ans++;
}
if(ans==p) puts("YES");
else puts("NO");
}
return 0;
}