2、试题库问题
2、试题库问题
洛谷链接:试题库问题
题意:
试题库中有
分析:
因为我们要满足特性属性要求,我们很自然地就想到,把每种属性和汇点
接着,我们在每套试题和它对应的每种属性之间都连上一条流量为
图建好了,如果最大流≠属性之和,说明我们找不到一种满足所有属性的方法,我们的程序就应该输出对应信息;如果最大流
对于每种属性和试题的匹配,我们只需要遍历以每种属性为起点的边(也就是链式前向星存图中
代码:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<ctime>
#include<algorithm>
#include<complex>
#include<iostream>
#include<map>
#include<queue>
#include<vector>
#define ll long long
using namespace std;
const int maxn=1e4+1000;
const int inf=1e9+7;
const ll INF=1e15+7;
int head[maxn],h[maxn]; //head:链式前向星存图,h[]:保存顶点的“层数”
int vis[maxn],stored[maxn]; //stored[]:当前节点所暂存的流量
int gap[maxn]; //gap优化
int n,m,s,t; //n:顶点数 m:边数 s:起始点 t:终点
int cnt=1; //从e[0]开始保存
struct node{
int next;
int to;
int flow;
}e[250000]; //next:链式前向星存图
void add(int u,int v,int w){ //添加《有向边》,如果是无向边的话下面得改一改
e[++cnt].to=v;
e[cnt].flow=w;
e[cnt].next=head[u];
head[u]=cnt;
e[++cnt].to=u;
e[cnt].flow=0; //要改的就是这里,无向边的话反向改为w
e[cnt].next=head[v];
head[v]=cnt;
}
inline bool bfs(){
for(register int i=1;i<=n;i++){
h[i]=inf; //初始化节点高度
}
h[t]=0; //反向搜索“高度”
queue<int> q;
q.push(t);
while(!q.empty()){
int tmp=q.front();
q.pop();
for(register int i=head[tmp];i;i=e[i].next){
int v=e[i].to;
if(e[i^1].flow&&h[v]>h[tmp]+1){ //e[i^1]判断反向边(因为是反向搜索),如果高度有改变就要重新入队
h[v]=h[tmp]+1;
q.push(v);
}
}
}
return(h[s]!=inf); //存在增广路与否
}
struct cmp{
inline bool operator()(int xi,int yi)const{ //以结点高度为关键字的优先队列
return h[xi]<h[yi];
}
};
priority_queue<int,vector<int>,cmp> pq;
inline void push(int u){ //将“除了起点和终点”以外的可行点放进优先队列(终点和起点需要特别判断)
for(register int i=head[u];i;i=e[i].next){
int v=e[i].to;
if(e[i].flow&&h[u]==h[v]+1){
int df=min(stored[u],e[i].flow);
e[i].flow-=df; //正向边流量减少
e[i^1].flow+=df; //反向边流量增加
stored[u]-=df; //当前节点存储流量减少
stored[v]+=df; //目标节点存储流量增加
if(v!=s&&v!=t&&!vis[v]){
vis[v]=1;
pq.push(v);
}
if(!stored[u])break; //当前节点被榨干了
}
}
}
inline void relabel(int u){
h[u]=inf;
for(register int i=head[u];i;i=e[i].next){
int v=e[i].to;
if(e[i].flow&&(h[u]>h[v]+1))h[u]=h[v]+1; //将一个节点的高度抬高到“恰好”能够流到下一个节点
}
}
inline ll HLPP(){
if(!bfs())return 0;
h[s]=n;
memset(gap,0,sizeof(gap));
for(register int i=1;i<=n;i++)if(h[i]!=inf)gap[h[i]]++; //gap优化:记录当前层有多少节点
for(register int i=head[s];i;i=e[i].next){ //对原点进行特判,因为原点的容量是无限的(而不是我们定义的INF)
int v=e[i].to;
if(int f=e[i].flow){
e[i].flow-=f;
e[i^1].flow+=f;
stored[v]+=f;
if(v!=s&&v!=t&&!vis[v]){
pq.push(v);
vis[v]=1;
}
}
}
while(!pq.empty()){
int now=pq.top(); //取出最“高”的结点
vis[now]=0;
pq.pop();
push(now);
if(stored[now]){
if(h[now]==inf)continue;
gap[h[now]]--;
if(!gap[h[now]]){
for(register int v=1;v<=n;v++){
if(v!=s&&v!=t&&h[v]>h[now]&&h[v]<n+1){ //避免改动inf和以及处于n+1位置的结点
h[v]=n+1; //如果最高层只有一个,则它直接流回原点
}
}
}
relabel(now); //重新贴标签
gap[h[now]]++;
pq.push(now); //重贴标签后推流
vis[now]=1;
}
}
return stored[t];
}
inline int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0' && ch<='9')
x=x*10+ch-'0',ch=getchar();
return x*f;
}
int main(){
int sec=read(),fir=read(),tmp,tot=0;
s=sec+fir+1;
t=s+1;
n=t;
for(int i=1;i<=sec;i++){ //添加属性和汇点之间的边
tmp=read();
add(fir+i,t,tmp);
tot+=tmp;
}
for(int i=1;i<=fir;i++){
int num=read();
add(s,i,1); //添加试题和源点之间的边
for(int j=0;j<num;j++){
tmp=read();
add(i,fir+tmp,1); //添加试题和属性之间的边
}
}
ll ans=HLPP();
if(ans<tot)cout<<"No Solution!"<<endl; //无法成功匹配
else{
for(int i=1;i<=sec;i++){
cout<<i<<": ";
for(int j=head[fir+i];j;j=e[j].next){ //遍历属性,输入对应的试题
if(e[j].flow){
cout<<e[j].to<<" ";
}
}
cout<<endl;
}
}
return 0;
}