题解:
裸如飞行员的二分图匹配问题。
直接上代码:
#include<queue> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define K 50 #define N 1250 const int inf = 0x3f3f3f3f; inline int rd() { int f=1,c=0;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){c=10*c+ch-'0';ch=getchar();} return f*c; } int k,n,m,S,T,hed[N],cnt=-1,cur[N]; struct EG { int to,nxt,w; }e[N*K*2]; void ae(int f,int t,int w) { e[++cnt].to = t; e[cnt].nxt = hed[f]; e[cnt].w = w; hed[f] = cnt; } int dep[N]; bool vis[N]; queue<int>q; bool bfs() { memset(dep,0x3f,sizeof(dep)); memcpy(cur,hed,sizeof(cur)); dep[S]=0,vis[S]=1;q.push(S); while(!q.empty()) { int u = q.front(); q.pop(); for(int j=hed[u];~j;j=e[j].nxt) { int to = e[j].to; if(e[j].w&&dep[to]>dep[u]+1) { dep[to] = dep[u]+1; if(!vis[to]) { vis[to] = 1; q.push(to); } } } vis[u] = 0; } return dep[T]!=inf; } int dfs(int u,int lim) { if(u==T||!lim)return lim; int fl = 0,f; for(int j=cur[u];~j;j=e[j].nxt) { cur[u] = j; int to = e[j].to; if(dep[to]==dep[u]+1&&(f=dfs(to,min(lim,e[j].w)))) { fl+=f,lim-=f; e[j].w-=f,e[j^1].w+=f; if(!lim)break; } } return fl; } int dinic() { int ret = 0; while(bfs())ret+=dfs(S,inf); return ret; } int main() { k = rd(),n = rd(); S = n+k+1,T=n+k+2; memset(hed,-1,sizeof(hed)); for(int x,i=1;i<=k;i++) { x = rd(); ae(S,i,x); ae(i,S,0); m+=x; } for(int c,x,i=1;i<=n;i++) { c = rd(); ae(i+k,T,1); ae(T,i+k,0); while(c--) { x = rd(); ae(x,i+k,1); ae(i+k,x,0); } } int tmp = dinic(); if(tmp==m) { for(int i=1;i<=k;i++) { printf("%d: ",i); for(int j=hed[i];~j;j=e[j].nxt) { int to = e[j].to; if(to==S)continue; if(e[j].w)continue; printf("%d ",to-k); } puts(""); } }else { puts("No Solution!"); } return 0; }