- http://acm.hdu.edu.cn/showproblem.php?pid=1083
- http://poj.org/problem?id=1469
- https://zoj.pintia.cn/problem-sets/91827364500/problems/91827364639
- https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=45
- http://bailian.openjudge.cn/practice/1469?lang=en_US
- http://acm.scu.edu.cn/soj/problem.action?id=1186
- http://www.hustoj.org/problem/1273
匈牙利算法经典题。
在每个课程与选修它的人之间两边,跑一遍最大匹配即可。
#include<bits/stdc++.h>
#define m(n,x) memset(n,x,sizeof n)
using namespace std;
int t,p,n,l;
int head[100*300*2+10],son[100*300*2+10],nxt[100*300*2+10],tot,ans,ma[410];
bool vis[410];
void add(int x,int y) {
son[++tot]=y;
nxt[tot]=head[x];
head[x]=tot;
}
void init() {
m(head,0),m(ma,0);
tot=0;
ans=0;
}
bool dfs(int x) {
for(int i=head[x],y; i; i=nxt[i])
if(!vis[son[i]]) {
y=son[i];
vis[y]=1;
if(!ma[y]||dfs(ma[y])) {
ma[y]=x;
return 1;
}
}
return 0;
}//标准匈牙利
int main() {
scanf("%d",&t);
for(; t; --t) {
scanf("%d %d",&p,&n);
init();
for(int i=1,x; i<=p; i++) {
scanf("%d",&l);
for(int j=1; j<=l; j++) {
scanf("%d",&x);
add(i,x+p);
add(x+p,i);
//printf("add %d--->%d
",i,x+p);
}
}
for(int i=1; i<=p; i++) {
m(vis,0);
if(dfs(i))
ans++;
}
if(ans==p)
puts("YES");
else
puts("NO");
}
}