POJ1469 匈牙利算法
http://poj.org/problem?id=1469
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
//POJ1469 #include <iostream> #include <cstdio> #include <cstring> using namespace std; const int MM=100010; int P,N; bool vis[MM]; int pre[MM]; int head[MM], NE; struct Edge{int v,next;}edge[MM]; void add_edge(int u,int v) { edge[NE].v=v; edge[NE].next=head[u]; head[u]=NE++; } void get_data() { int i,j,k,x,y; scanf("%d%d",&P,&N); NE=0; memset(head,-1,sizeof(head)); for(i=1;i<=P;i++) { scanf("%d",&x); for(j=0;j<x;j++) { scanf("%d",&y); add_edge(i,y); } } } bool dfs(int u) { int i,j,k,v; for(i=head[u];i!=-1;i=edge[i].next) { v=edge[i].v; if(!vis[v]) { vis[v]=true; if(pre[v]==-1||dfs(pre[v])) { pre[v]=u; return true; } } } return false; } void solve() { int i,j,k,ans=0; memset(pre,-1,sizeof(pre)); for(i=1;i<=P;i++) { memset(vis,false,sizeof(vis)); if(dfs(i)) ans++; } if(ans==P) puts("YES"); else puts("NO"); } int main() { int ca; scanf("%d",&ca); while(ca--) get_data(), solve(); return 0; }