建立超级源点和超级汇点
超级源点向每道题目连一条流量为1的边
每种题型向超级汇点连一条流量为该题型的题目数的边,表示这种题型最多选这么多题
对于每道题的若干类型,在题目和类型之间连一条流量为1的边
跑最大流,判断maxflow是否等于sigma(所有题型要求的题数总和)
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <utility>
#include <map>
#include <stack>
#include <set>
#include <vector>
#include <queue>
#include <deque>
#include <sstream>
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define LL long long
#define Pair pair<int,int>
#define LOWBIT(x) x & (-x)
using namespace std;
const int MOD=1e9+7;
const int INF=0x7ffffff;
const int magic=348;
int t,tot,head[10048],nxt[200048],to[200048],f[200048];
inline void addedge(int s,int t,int cap)
{
to[++tot]=t;nxt[tot]=head[s];head[s]=tot;f[tot]=cap;
to[++tot]=s;nxt[tot]=head[t];head[t]=tot;f[tot]=0;
}
int k,p;
bool table[1048][48];
int depth[10048];queue<int> q;
bool bfs()
{
int i,x,y;
for (i=0;i<=t;i++) depth[i]=-1;
depth[0]=0;q.push(0);
while (!q.empty())
{
x=q.front();q.pop();
for (i=head[x];i;i=nxt[i])
{
y=to[i];
if (f[i] && depth[y]==-1)
{
depth[y]=depth[x]+1;
q.push(y);
}
}
}
if (depth[t]==-1) return false; else return true;
}
int dfs(int x,int maxf)
{
if (x==t) return maxf;
int i,y,now,ans=0,minf;
for (i=head[x];i;i=nxt[i])
{
y=to[i];
if (f[i] && depth[y]==depth[x]+1)
{
minf=min(f[i],maxf-ans);
now=dfs(y,minf);
f[i]-=now;
f[i^1]+=now;
ans+=now;
if (now==0) continue;
if (1<=x && x<=p && p+1<=y && y<=p+k) table[x][y-p]=true;
if (1<=y && y<=p && p+1<=x && x<=p+k) table[y][x-p]=false;
}
}
return ans;
}
int main ()
{
int i,j,u,x,num,sum;
while (scanf("%d%d",&k,&p) && (k || p))
{
t=k+p+1;tot=1;for (i=0;i<=t;i++) head[i]=0;
for (i=1;i<=p;i++)
for (j=1;j<=k;j++)
table[i][j]=false;
for (i=1;i<=p;i++) addedge(0,i,1);
sum=0;
for (i=1;i<=k;i++)
{
scanf("%d",&x);
sum+=x;
addedge(p+i,t,x);
}
for (i=1;i<=p;i++)
{
scanf("%d",&num);
while (num--)
{
scanf("%d",&x);
addedge(i,p+x,1);
}
}
int ans=0;
while (bfs()) ans+=dfs(0,2e9);
if (ans<sum)
{
printf("0\n");
continue;
}
else
{
printf("1\n");
for (i=1;i<=k;i++)
{
for (j=1;j<=p;j++)
if (table[j][i]) printf("%d ",j);
printf("\n");
}
}
}
return 0;
}