//756K 469MS G++
#include <stdio.h>
#include <string.h>
#define MAX 310
int G[MAX][MAX];
int V1[MAX];
int V2[MAX];
int P; // course num
int N; // student num
int waittingMove[MAX];
char getPair(int curId) {
for (int i = 1; i <= N; i++) { // check if students visit this course
if (G[curId][i]) { // if student i visit curId course
if (V2[i] == 0) { // if student i is not assigned yet.
V1[curId] = i;
V2[i] = curId;
return 1;
} else {
if (!waittingMove[V2[i]]) { // if i's owener is not waitting move
waittingMove[V2[i]] = 1; // try to move i's owner
if (getPair(V2[i])) { // if i's owener move success
V1[curId] = i;
V2[i] = curId;
return 1;
}
}
}
}
}
return 0;
}
void solve() {
int maxMatch = 0;
for (int i = 1; i <= P; i++) { // find commitee for each course
memset(waittingMove, 0, sizeof(waittingMove));
if (getPair(i)) {
maxMatch++;
}
}
// printf("%d %d\n", maxMatch, P);
if (maxMatch == P) { // all course can find a unqiue student who visited
printf("YES\n");
} else {
printf("NO\n");
}
}
int main() {
int caseNum;
scanf("%d", &caseNum);
for (int i = 0; i < caseNum; i++) {
memset(G, 0, sizeof(G));
memset(V1, 0, sizeof(V1));
memset(V2, 0, sizeof(V2));
scanf("%d %d", &P, &N);
for (int j = 1; j <= P; j++) { // course, j is courseId
int studentNum = 0;
scanf("%d", &studentNum);
for (int k = 0 ; k < studentNum; k++) { // students visit this course
int studentId;
scanf("%d", &studentId);
G[j][studentId] = 1;
}
}
solve();
}
}
应该是最简单的二分图题了,没有转化难度,长着 “快用二分图最大匹配干我” 的脸,
简单记录下:
二分图两个子集的点,C一个代表course,S一个代表student,如果某个student Sx上某个course Cy,那么
Sx和Cy之间就有一条边,
而在要选出的course代表中,一个course只能有一个上它的student作为代表,也不能有一个student当多个sourse的代表,
在图里面的表示就是找出一个边集,该边集的边没有共同端点,互不相交,
而能否每个course都有代表,取决于有没有这样的边集,其边的数量等于course的数量(这样就肯定满足了一个course有一个代表,有因为互不相交,所以保证了
只有一个代表和每个student只能代表一个course), 而最大匹配就是这个边集的最大集合,
匈牙利算法求出最大匹配的边数,如果等于course num,那么YES, 否则NO。