匹配必须大于或者等于P,学生人数必须大于P。
CODE:
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
using namespace std;
const int MAXN = 310;
const int MAXM = 310*310;
int first[MAXN], xlink[MAXN], ylink[MAXN];
bool vis[MAXN];
int G[MAXN][MAXN];
int nx, ny;
void init()
{
memset(G, 0, sizeof(G));
memset(xlink, -1, sizeof(xlink));
memset(ylink, -1, sizeof(ylink));
}
bool ED(int u)
{
for(int v = 1; v <= ny; v++) if(G[u][v])
{
if(!vis[v])
{
vis[v] = 1;
if(ylink[v] == -1 || ED(ylink[v]))
{
xlink[u] = v; ylink[v] = u;
return true;
}
}
}
return false;
}
void solve()
{
int ans = 0;
if(ny < nx) { printf("NO\n"); return ;}
for(int i = 1; i <= nx; i++)
{
if(xlink[i] == -1)
{
memset(vis, 0, sizeof(vis));
ans += ED(i);
}
}
printf(ans >= nx? "YES\n" : "NO\n");
}
void solve_case()
{
int T;
scanf("%d", &T);
while(T--)
{
init();
scanf("%d%d", &nx, &ny);
for(int u = 1; u <= nx; u++)
{
int people;
scanf("%d", &people);
while(people--)
{
int v;
scanf("%d", &v);
G[u][v] = 1;
}
}
solve();
}
}
int main()
{
solve_case();
return 0;
}
#include <cstdlib>
#include <cstring>
#include <cstdio>
using namespace std;
const int MAXN = 310;
const int MAXM = 310*310;
int first[MAXN], xlink[MAXN], ylink[MAXN];
bool vis[MAXN];
int G[MAXN][MAXN];
int nx, ny;
void init()
{
memset(G, 0, sizeof(G));
memset(xlink, -1, sizeof(xlink));
memset(ylink, -1, sizeof(ylink));
}
bool ED(int u)
{
for(int v = 1; v <= ny; v++) if(G[u][v])
{
if(!vis[v])
{
vis[v] = 1;
if(ylink[v] == -1 || ED(ylink[v]))
{
xlink[u] = v; ylink[v] = u;
return true;
}
}
}
return false;
}
void solve()
{
int ans = 0;
if(ny < nx) { printf("NO\n"); return ;}
for(int i = 1; i <= nx; i++)
{
if(xlink[i] == -1)
{
memset(vis, 0, sizeof(vis));
ans += ED(i);
}
}
printf(ans >= nx? "YES\n" : "NO\n");
}
void solve_case()
{
int T;
scanf("%d", &T);
while(T--)
{
init();
scanf("%d%d", &nx, &ny);
for(int u = 1; u <= nx; u++)
{
int people;
scanf("%d", &people);
while(people--)
{
int v;
scanf("%d", &v);
G[u][v] = 1;
}
}
solve();
}
}
int main()
{
solve_case();
return 0;
}