题目描述 Description
假设一个试题库中有 n 道试题。每道试题都标明了所属类别。同一道题可能有多个类别属性。现要从题库中抽取 m 道题组成试卷。并要求试卷包含指定类型的试题。试设计一个满足要求的组卷算法。
对于给定的组卷要求,计算满足要求的组卷方案。
输入描述 Input Description
第1行有2个正整数n和k (2 <=k<= 20, k<=n<= 1000) k 表示题库中试题类型总数,n 表示题库中试题总数。第 2 行有 k 个正整数,第 i 个正整数表示要选出的类型 i 的题数。这 k 个数相加就是要选出的总题数 m。接下来的 n 行给出了题库中每个试题的类型信息。每行的第 1 个正整数 p 表明该题可以属于 p 类,接着的 p 个数是该题所属的类型号。
输出描述 Output Description
第 i 行输出 “i:” 后接类型 i 的题号。如果有多个满足要求的方案,只要输出 1 个方案。如果问题无解,则输出“No Solution!”。
样例输入 Sample Input
3 15
3 3 4
2 1 2
1 3
1 3
1 3
1 3
3 1 2 3
2 2 3
2 1 3
1 2
1 2
2 1 2
2 1 3
2 1 2
1 1
3 1 2 3
样例输出 Sample Output
题目可以再这里提交:http://wikioi.com/homework/23/1: 1 6 8
2: 7 9 10
3: 2 3 4 5
分析:二分多重匹配,跑完最大流后,从X集合出发,终点在Y集合,且满流的边就是一个可行解。
代码:
//Isap算法,复杂度O(n^2m)
#pragma comment(linker,"/STACK:102400000,102400000")
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
typedef long long ll; //记得必要的时候改成无符号
const int maxn=505;
const int maxm=1000005;
const int INF=1000000000;
struct EdgeNode
{
int from;
int to;
int cost;
int next;
}edge[maxm];
int head[maxn],cnt;
void add(int x,int y,int z)
{
edge[cnt].from=x;edge[cnt].to=y;edge[cnt].cost=z;edge[cnt].next=head[x];head[x]=cnt++;
edge[cnt].from=y;edge[cnt].to=x;edge[cnt].cost=0;edge[cnt].next=head[y];head[y]=cnt++;
}
void init()
{
cnt=0;
memset(head,-1,sizeof(head));
}
int S,T,n,m;
int d[maxn],gap[maxn],curedge[maxn],pre[maxn];
//curedge[]为当前弧数组,pre为前驱数组
int sap(int S,int T,int n) //n为点数
{
int cur_flow,flow_ans=0,u,tmp,neck,i;
memset(d,0,sizeof(d));
memset(gap,0,sizeof(gap));
memset(pre,-1,sizeof(pre));
for(i=0;i<=n;i++)curedge[i]=head[i]; //初始化当前弧为第一条邻接表
gap[0]=n;
u=S;
while(d[S]<n) //当d[S]>=n时,网络中肯定出现了断层
{
if(u==T)
{
cur_flow=INF;
for(i=S;i!=T;i=edge[curedge[i]].to)
{ //增广成功,寻找瓶颈边
if(cur_flow>edge[curedge[i]].cost)
{
neck=i;
cur_flow=edge[curedge[i]].cost;
}
}
for(i=S;i!=T;i=edge[curedge[i]].to)
{ //修改路径上的边容量
tmp=curedge[i];
edge[tmp].cost-=cur_flow;
edge[tmp^1].cost+=cur_flow;
}
flow_ans+=cur_flow;
u=neck; //下次增广从瓶颈边开始
}
for(i=curedge[u];i!=-1;i=edge[i].next)
if(edge[i].cost&&d[u]==d[edge[i].to]+1)
break;
if(i!=-1)
{
curedge[u]=i;
pre[edge[i].to]=u;
u=edge[i].to;
}
else
{
if(0==--gap[d[u]])break; //gap优化
curedge[u]=head[u];
for(tmp=n,i=head[u];i!=-1;i=edge[i].next)
if(edge[i].cost)
tmp=min(tmp,d[edge[i].to]);
d[u]=tmp+1;
++gap[d[u]];
if(u!=S)u=pre[u]; //重标号并且从当前点前驱重新增广
}
}
return flow_ans;
}
vector<int>V[maxn];
int main()
{
int x,y,i,j,sum;
while(~scanf("%d%d",&n,&m))
{
init(); S=0; T=n+m+1; sum=0;
for(i=1;i<=n;i++){
scanf("%d",&x); sum+=x;
add(m+i,T,x);
}
for(i=1;i<=m;i++){
add(S,i,1);
scanf("%d",&x);
while(x--){
scanf("%d",&y);
add(i,y+m,1);
}
}
if(sum!=sap(S,T,T+1))printf("No Solution!\n");
else{
for(i=1;i<=n;i++)V[i].clear();
for(i=0;i<cnt;i++){
x=edge[i].from; y=edge[i].to;
if(x>=1&&x<=m&&y>m&&y<=n+m&&edge[i].cost==0)
V[y-m].push_back(x);
}
for(i=1;i<=n;i++){
printf("%d:",i);
for(j=0;j<V[i].size();j++){
printf(" %d",V[i][j]);
}
printf("\n");
}
}
}
return 0;
}