一.原题链接:http://poj.org/problem?id=1469
二.题目大意:有P个课程,N个学生,问能不能在每个课程都有课代表的情况下,保证每个是课代表的学生代表不同的课。(可以有学生不是课代表,英语不好读错题)
三,思路:二分匹配裸题,不能再裸了。图都直接给了啊。
四.代码:
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <cstdlib>
using namespace std;
const int INF = 0x3f3f3f3f,
MAX_SIZE = 300;
class maxMatch
{
public:
bool connected[MAX_SIZE][MAX_SIZE],
visited[MAX_SIZE];
int xMatch[MAX_SIZE], yMatch[MAX_SIZE],
xNum, yNum;
void init()
{
xNum = yNum = 0;
memset(connected, 0, sizeof(connected));
memset(xMatch, -1, sizeof(xMatch));
memset(yMatch, -1, sizeof(yMatch));
}
bool path(int u)
{
int v;
for(v = 1; v <= yNum; v++)
if(connected[u][v] && !visited[v]){
visited[v] = true;
if(-1 == yMatch[v] || path(yMatch[v])){
yMatch[v] = u;
xMatch[u] = v;
return true;
}
}
return false;
}
int getRes()
{
int i, res = 0;
for(i = 1; i <= xNum; i++)
if(-1 == xMatch[i]){
memset(visited, 0, sizeof(visited));
if(path(i))
res++;
}
return res;
}
}G;
// student: y;
// course : x;
int main()
{
//freopen("in.txt", "r", stdin);
int test, i, j, stuId, num;
scanf("%d", &test);
while(test--){
G.init();
scanf("%d%d", &G.xNum, &G.yNum);
for(i = 1; i <= G.xNum; i++){
scanf("%d", &num);
for(j = 1; j <= num; j++){
scanf("%d", &stuId);
G.connected[i][stuId] = true;
}
}
if(G.getRes() != G.xNum)
printf("NO\n");
else
printf("YES\n");
}
}