转换成最大流求解,刚开始看最大流,留着当模板#include #include #include #include #include #include #include #include #define maxn 20#define Max 0x3f3f3f3fusing namespace std;struct node{ int to; int value; int pos;};vector G[405];bool used[405];int leve[405];int lazy[405];void addnode(int from,int to,int val){ G[from].push_back((node){to,val,G[to].size()}); G[to].push_back((node){from,0,G[from].size()-1});}int bfs(int s){ memset(leve,-1,sizeof(leve)); leve[s]=0; queue que; que.push(s); while(!que.empty()) { int now=que.front(); que.pop(); for (int i=0;i 0&&leve[tag.to]<0) { leve[tag.to]=leve[now]+1; que.push(tag.to); } } }}int dfs(int v,int t,int f){ if (v==t)return f; for (int & i=lazy[v];i 0&&leve[v] 0) { now.value-=tag; G[now.to][now.pos].value+=tag; return tag; } } } return 0;}int Maxflow(int s,int t){ int flow=0; for(;;) { bfs(s); if (leve[t]<0) { return flow; } int f; memset(lazy,0,sizeof(lazy)); while((f=dfs(s,t,Max))>0) { flow+=f; } }}int main(){ int n,k,d; int a,b,c,all; while(scanf("%d %d %d",&n,&k,&d)!=EOF) { all=n+k+d+n+1; for (int i=0;i<405;i++) { G[i].clear(); } for (int i=1;i<=k;i++)//食物跟源点连边 { addnode(0,i,1); } for (int i=1;i<=n;i++)//牛连边 { addnode(k+i,k+i+n,1); } for (int i=1;i<=d;i++) { addnode(k+n+n+i,all,1); } for (int i=1;i<=n;i++) { scanf("%d %d",&a,&b); while(a--) { scanf("%d",&c); addnode(c,k+i,1); } while(b--) { scanf("%d",&c); addnode(k+n+i,k+2*n+c,1); } } int num=Maxflow(0,all); printf("%d\n",num); } return 0;}