poj 1469 COURSES
最基本的二分匹配,用匈牙利算法DFS实现。
#include<iostream> #include<string.h> #include<stdio.h> #define MAXD 301 #define INF 0x3f3f3f3f using namespace std; int P,N,res,cx[MAXD],cy[MAXD]; bool edge[MAXD][MAXD],vis[MAXD]; int path(int u) { int i; for(i=1;i<=N;i++) { if(edge[u][i]&&!vis[i]) { vis[i]=true; if(cy[i]==-1||path(cy[i])) { cx[u]=i; cy[i]=u; return 1; } } } return 0; } int maxmatch() { int sum=0; memset(cx,-1,sizeof(cx)); memset(cy,-1,sizeof(cy)); int i; for(i=1;i<=P;i++) { if(cx[i]==-1) { memset(vis,0,sizeof(vis)); sum+=path(i); } } return sum; } int main() { //freopen("test.txt","r",stdin); int T; scanf("%d",&T); while(T--) { scanf("%d%d",&P,&N); int k,temp,i,j; memset(edge,0,sizeof(edge)); for(i=1;i<=P;i++) { scanf("%d",&k); for(j=1;j<=k;j++) { scanf("%d",&temp); edge[i][temp]=true; } } res=maxmatch(); if(res==P) printf("YES\n"); else printf("NO\n"); } }