解题思路:
裸的匈牙利算法,看最大匹配是否等于P;
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int MAXN = 500;
int p, n;
int G[MAXN][MAXN];
int match[MAXN];
int vis[MAXN];
int path(int u)
{
for(int i=1;i<=p;i++)
{
if(G[u][i] && !vis[i])
{
vis[i] = 1;
if(match[i] == -1 || path(match[i]))
{
match[i] = u;
return 1;
}
}
}
return 0;
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
memset(G, 0, sizeof(G));
memset(vis, 0, sizeof(vis));
memset(match, -1, sizeof(match));
scanf("%d%d", &p, &n);
int c;
for(int i=1;i<=p;i++)
{
scanf("%d", &c);
while(c--)
{
int x;
scanf("%d", &x);
G[x][i] = 1;
}
}
int ans = 0;
for(int i=1;i<=n;i++)
{
memset(vis, 0, sizeof(vis));
ans += path(i);
}
if(ans == p) printf("YES\n");
else printf("NO\n");
}
return 0;
}