Title Link: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=33631
Ideas
Maximum flow.
Composition:
1 build m x dots for stickers, n-1 y dots for Bob's friends, St for Yuanhui points.
2 S to X[j] a side with a capacity of sum[0][j], which means Bob can swap out as a sticker.
3 Y[j] to x[i] with a capacity of 1 side when and only if I have no j,x[i] to Y[j] The edge of the capacity sum[i][j]-1.
4 X[i] The number of different kinds is counted to t even one edge with a capacity of 1.
Code
1#include <cstdio>2#include <cstring>3#include <queue>4#include <vector>5 using namespacestd;6 7 Const intMAXN = -+Ten;8 Const intINF =1e9;9 Ten structedge{ One intU,v,cap,flow; A }; - structDinic { - intn,m,s,t; the BOOLVIS[MAXN]; - intD[MAXN],CUR[MAXN]; -vector<int>G[MAXN]; -Vector<edge>es; + - voidInitintN) { + This->n=N; A es.clear (); at for(intI=0; i<n;i++) g[i].clear (); - } - voidAddedge (intUintVintcap) { -Es.push_back (Edge) {u,v,cap,0}); -Es.push_back (Edge) {v,u,0,0}); -m=es.size (); inG[u].push_back (M-2); -G[v].push_back (M-1); to } + - BOOLBFS () { thequeue<int>Q; *memset (Vis,0,sizeof(Vis)); $Q.push (s); vis[s]=1; d[s]=0;Panax Notoginseng while(!Q.empty ()) { - intu=Q.front (); Q.pop (); the for(intI=0; I<g[u].size (); i++) { +edge& e=Es[g[u][i]]; A intv=e.v; the if(!vis[v] && e.cap>e.flow) { +vis[v]=1; -d[v]=d[u]+1; $ Q.push (v); $ } - } - } the returnVis[t]; - }Wuyi intDFS (intUinta) { the if(U==t | | a==0)returnA; - intflow=0, F; Wu for(int& I=cur[u];i<g[u].size (i++);){ -edge& e=Es[g[u][i]]; About intv=e.v; $ if(d[v]==d[u]+1&& (F=dfs (V,min (a,e.cap-e.flow)) >0 ) { -e.flow+=F; -es[g[u][i]^1].flow-=F; -flow+=f,a-=F; A if(!a) Break; + } the } - returnflow; $ } the intMaxflow (intSintt) { the This->s=s, This->t=T; the intflow=0; the while(BFS ()) { -memset (cur,0,sizeof(cur)); inflow+=DFS (s,inf); the } the returnflow; About } the }dinic; the the intt,n,m; + intSUM[MAXN][MAXN]; - the intMain () {Bayiscanf"%d",&T); the intKase=0; the while(t--) { -scanf"%d%d",&n,&m); -Dinic.init (n+m+1); thememset (SUM,0,sizeof(sum)); the intb; the for(intI=0; i<n;i++) { thescanf"%d",&a); - for(intj=0; j<a;j++){ thescanf"%d",&b); thesum[i][b-1]++; the }94 } the ints=n+m-1, t=n+m; the for(intI=0; i<m;i++) { the if(sum[0][i]) dinic. Addedge (s,i,sum[0][i]);98Dinic. Addedge (I,t,1); About } - for(intI=0; i<n;i++)101 for(intj=0; j<m;j++) {102 if(!sum[i][j]) dinic. Addedge (j,i+m-1,1);103 if(sum[i][j]>1) Dinic. Addedge (i+m-1, j,sum[i][j]-1);104 } theprintf"Case #%d:%d\n",++Kase,dinic. Maxflow (s,t));106 }107 return 0;108}
UVa10779 collectors problem (maximum flow)