题意:P门课程,N个学生。要求,每门课程都要有一个学生代表,而且一个学生不能做两节课的代表。
思路:课程为二分图X部,学生为二分图Y部,如果最大匹配=P,输出YES,否则,输出NO。
读错了题目,WA了N次。
http://acm.hdu.edu.cn/showproblem.php?pid=1083
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define rep(i,a,b) for(int i = a ; i <= b ; i ++)
#define rrep(i,a,b) for(int i = b ; i >= a ; i --)
#define repE(p,u) for(Edge * p = G[u].first ; p ; p = p -> next)
#define cls(a,x) memset(a,x,sizeof(a))
#define eps 1e-8
using namespace std;
const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e3+5;
const int MAXE = 1e6+5;
typedef long long LL;
typedef unsigned long long ULL;
struct Edge {
int to;
Edge * next;
}E[MAXE],*EE;
struct Gragh {
Edge * first;
}G[MAXN];
int N,M;
bool visit[MAXN];
int match[MAXN];
void addedge(int u,int v) {
EE->to = v ; EE -> next = G[u].first ; G[u].first = EE ++;
}
int T,n,m,k;
void init() {
EE = E; N = M = 0;
cls(G,0);
}
bool find_path(int u) {
int v;
repE(p,u) {
v = p->to;
if(!visit[v]) {
visit[v] = 1;
if(match[v] == -1 || find_path(match[v])) {
match[v] = u;
return true;
}
}
}
return false;
}
int Max_match() {
cls(match,-1);
int cnt = 0;
rep(i,1,N) {
cls(visit,0);
if(find_path(i)) cnt ++;
}
return cnt;
}
void input() {
scanf("%d %d",&N,&M);
int NUM,tmp;
rep(i,1,N) {
scanf("%d",&NUM);
rep(j,1,NUM) {
scanf("%d",&tmp);
addedge(i,N+tmp);
}
}
}
void solve() {
if(Max_match() == N ) puts("YES");
else puts("NO");
}
int main(void) {
scanf("%d",&T);
while(T--) {
init();
input();
solve();
}
return 0;
}