1 #include<cstdio> 2 #include<algorithm> 3 #include<string> 4 #include<cstring> 5 #include<iostream> 6 #include<cmath> 7 #define LL long long 8 #define INF 999999999 9 #define Min(num1,num2) if(num1>num2) num1=num2 10 #define Max(num1,num2) if(num1<num2) num1=num2 11 #define N 100100 12 //using namespace std ; 13 int next[N],last[N],flow[N],to[N],op[N],G; 14 int Total,n,m,ans,nodes,S,T,dis[N],cnt[N]; 15 bool vis[N]; 16 void addedge(int x,int y,int v){ 17 next[++G]=last[x]; last[x]=G; flow[G]=v; to[G]=y; op[G]=G+1; 18 next[++G]=last[y]; last[y]=G; flow[G]=0; to[G]=x; op[G]=G-1; 19 } 20 int sap(int t,int delta){ 21 if(t==T) return delta; 22 int sum=0,mindis=nodes; 23 for(int i=last[t]; i ;i=next[i]){ 24 if(flow[i]>0 && dis[to[i]]+1==dis[t]){ 25 int save = sap(to[i],std::min(flow[i],delta-sum)); 26 sum += save; 27 flow[ i ] -= save; 28 flow[op[i]] += save; 29 if (dis[S]>=nodes||sum==delta) return sum; 30 } 31 if(flow[i]>0) Min(mindis,dis[to[i]]); 32 } 33 if(sum==0){ 34 if(!--cnt[dis[t]]) dis[S]=nodes; 35 else ++cnt[dis[t]=mindis+1]; 36 } 37 return sum; 38 } 39 void DFS(int t){ 40 for(int i=last[t];i;i=next[i]) 41 if(!vis[to[i]]&&flow[i]){ 42 vis[to[i]]=1; 43 DFS(to[i]); 44 } 45 } 46 int main(){ 47 scanf("%d%d",&m,&n); 48 S=0; 49 T=m+n+1; 50 for(int x,y,i=1;i<=m;i++){ 51 char flag; 52 scanf("%d%c",&x,&flag); 53 addedge(S,i,x); 54 Total+=x; 55 while(flag!='\n'){ 56 scanf("%d%c",&y,&flag); 57 addedge(i,y+m,INF); 58 } 59 } 60 for(int x,i=1;i<=n;i++){ 61 scanf("%d",&x); 62 addedge(i+m,T,x); 63 } 64 nodes=cnt[0]=T+1; 65 while(nodes>dis[S]) ans+=sap(S,INF); 66 DFS(S); 67 bool use=false ; 68 for(int i=1;i<=m;i++) 69 if(vis[i]){ 70 if(use) printf(" "); 71 printf("%d",i); 72 use=1; 73 } 74 use=false ; 75 printf("\n"); 76 for(int i=m+1;i<=n+m;i++) 77 if(vis[i]){ 78 if(use) printf(" "); 79 printf("%d",i-m); 80 use=1; 81 } 82 printf("\n%d",Total-ans); 83 }