Description
Input
Output
Sample Input
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 const int N=1205,INF=1999999999; 7 int gi(){ 8 int str=0;char ch=getchar(); 9 while(ch>'9'||ch<'0')ch=getchar(); 10 while(ch>='0' && ch<='9')str=str*10+ch-'0',ch=getchar(); 11 return str; 12 } 13 int n,m,S=0,T,num=1,head[N]; 14 struct Lin{ 15 int next,to,dis; 16 }a[N*N]; 17 void init(int x,int y,int z){ 18 a[++num].next=head[x]; 19 a[num].to=y; 20 a[num].dis=z; 21 head[x]=num; 22 a[++num].next=head[y]; 23 a[num].to=x; 24 a[num].dis=0; 25 head[y]=num; 26 } 27 int dep[N],q[N]; 28 bool bfs() 29 { 30 memset(dep,0,sizeof(dep)); 31 dep[S]=1;q[1]=S;int u,t=0,sum=1,x; 32 while(t!=sum) 33 { 34 x=q[++t]; 35 for(int i=head[x];i;i=a[i].next){ 36 u=a[i].to; 37 if(dep[u] || a[i].dis<=0)continue; 38 dep[u]=dep[x]+1;q[++sum]=u; 39 } 40 } 41 return dep[T]; 42 } 43 int dfs(int x,int flow) 44 { 45 if(x==T || !flow)return flow; 46 int u,tmp,sum=0; 47 for(int i=head[x];i;i=a[i].next){ 48 u=a[i].to; 49 if(dep[u]!=dep[x]+1 || a[i].dis<=0)continue; 50 tmp=dfs(u,min(flow,a[i].dis)); 51 a[i].dis-=tmp;a[i^1].dis+=tmp; 52 sum+=tmp;flow-=tmp; 53 if(!flow)break; 54 } 55 return sum; 56 } 57 int main() 58 { 59 int x,k,sum1=0; 60 m=gi();n=gi(); 61 T=n+m+1; 62 for(int i=1;i<=m;i++){ 63 x=gi();init(i+n,T,x);sum1+=x; 64 } 65 for(int i=1;i<=n;i++){ 66 init(S,i,1); 67 k=gi(); 68 for(int j=1;j<=k;j++)x=gi(),init(i,x+n,1); 69 } 70 int tot=0,tmp; 71 while(bfs()){ 72 tmp=dfs(S,INF); 73 while(tmp)tot+=tmp,tmp=dfs(S,INF); 74 } 75 if(tot<sum1){ 76 puts("No Solution!"); 77 return 0; 78 } 79 for(int i=1+n;i<=n+m;i++){ 80 printf("%d:",i-n); 81 for(int j=head[i];j;j=a[j].next) 82 if(a[j].to!=T && a[j].dis==1)printf(" %d",a[j].to); 83 puts(""); 84 } 85 return 0; 86 }