标签:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 21527 | Accepted: 8460 |
Description
Input
Output
Sample Input
2 3 3 3 1 2 3 2 1 2 1 1 3 3 2 1 3 2 1 3 1 1
Sample Output
YES NO
【分析】最大匹配模板题。
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> #include <string> #include <vector> #define inf 0x7fffffff #define met(a,b) memset(a,b,sizeof a) typedef long long ll; using namespace std; const int N = 405; const int M = 40005; int n,m,cnt; int x[M],y[M]; int t[M],head[N]; struct man { int to,next; }g[M]; bool dfs(int u) { for(int i=head[u];i!=-1;i=g[i].next) { int v=g[i].to; if(!t[v]) { t[v]=1; if(!y[v]||dfs(y[v])) { x[u]=v; y[v]=u; return true; } } } return false; } void MaxMatch() { int ans=0; for(int i=1; i<=m; i++) { if(!x[i]) { met(t,0); if(dfs(i))ans++; } } if(ans==m)printf("YES\n"); else printf("NO\n"); } void add(int u,int v) { g[cnt].to=v;g[cnt].next=head[u];head[u]=cnt++; } int main() { int T; int k,number; bool flag; scanf("%d",&T); for(int k=1; k<=T; k++) { met(g,0); met(x,0); met(y,0); met(head,-1);cnt=0; scanf("%d%d",&m,&n); for(int i=1; i<=m; i++) { int t,u; scanf("%d",&t); while(t--) { scanf("%d",&u); add(i,u); } } MaxMatch(); } return 0; }
标签:
原文地址:http://www.cnblogs.com/jianrenfang/p/5932627.html