[Network Flow 24 questions] questions of question Bank

Source: Internet
Author: User

Description

Suppose a question bank has $n$ road questions, each question is marked with the category, the same problem may have more than one category attribute. Now to take $m$ from the question bank to form a test paper, and asked to include a specific type of test paper. A group volume scheme that satisfies the requirements.

Input

The $1$ line has a $2$ positive integer $n,k,k$ represents the total number of question types in the bank, $n $ indicates the total number of questions in the library.

Line $2$ has a $k$ positive integer, and the $i$ positive integer indicates the number of $i$ of the type to be selected $a_i$. The sum of the $k$ number is the total number of questions to be selected $m$.

The next $n$ line gives the type information for each question in the bank. The $1$ positive integer $p$ of each row indicates that the title can belong to the $p$ class, and then the number of $p$ is the type number to which the question belongs.

Output

Line $i$ Output $ "I:" After the number of type $i$. If there are multiple scenarios that meet the requirements, just output $1$ scenarios.

If the problem is not solved, the output "$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

1:1 6 8
2:7 9 10
3:2 3 4 5

HINT

$2\;\leq\;k\;\leq\;20,k\;\leq\;n\;\leq\;1000$

Solution

The category $i$ is $x_i$, the title $i$ is $y_i$.

From $s$ to $x_i$, a forward edge with a capacity of $a_i$ is connected,

From $x_i$ to $y_j$, a forward edge with a capacity of $1$ (title $j\in$ category $i$) is connected to the

From $y_i$ to $t$, a forward edge with a capacity of $1$ is connected,

To find the maximum flow.

If the maximum flow $=m$, there is a solution, otherwise there is no solution.

All full-stream edges flowing from the collection $x$ to the collection $y$ are the current scheme.

#include <cmath>#include<ctime>#include<queue>#include<stack>#include<cstdio>#include<vector>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>#defineM 25#defineN 1005using namespacestd;structgraph{intnxt,to,f;} E[m*n<<1];intA[m],g[n+m],dep[n+m],n,m,s,t,sum,cnt=1; queue<int>Q;inlinevoidAddedge (intXintYintf) {e[++cnt].nxt=g[x];g[x]=cnt;e[cnt].to=y;e[cnt].f=F;} InlinevoidAdde (intXintYintf) {Addedge (x,y,f); Addedge (Y,x,0);} InlineBOOLBFsintu) {memset (DEP,0,sizeof(DEP)); Q.push (U);d Ep[u]=1;  while(!Q.empty ()) {u=Q.front (); Q.pop ();  for(intI=g[u];i;i=e[i].nxt)if(e[i].f>0&&!Dep[e[i].to])                {Q.push (e[i].to); Dep[e[i].to]=dep[u]+1; }    }    returndep[t];} InlineintDfsintUintf) {    intret=0; if(u==t)returnF;  for(intI=g[u],d;i&&f;i=e[i].nxt)if(e[i].f>0&&dep[e[i].to]>Dep[u]) {D=Dfs (E[i].to,min (F,E[I].F)); RET+=d;f-=d;e[i].f-=d;e[i^1].f+=D; }    returnret;} InlineintDinic () {intret=0;  while(true){        if(!bfs (s))returnret; RET+=DFS (S,M); }}inlinevoidAireen () {scanf ("%d%d",&m,&N); S=m+n+1; t=s+1;  for(intI=1; i<=m;++i) {scanf ("%d",&A[i]); Adde (s,i,a[i]); Sum+=A[i]; }     for(intI=1, j,k;i<=n;++i) {scanf ("%d",&k);  while(k--) {scanf ("%d", &j); Adde (J,i+m,1); } adde (I+m,t,1); }    if(Dinic ()!=sum) puts ("No solution!"); Else{         for(intI=1; i<=m;++i) {printf ("%d:", i);  for(intj=g[i];j;j=e[j].nxt)if(!e[j].f&&! (j&1)) printf ("%d", e[j].to-m); printf ("\ n"); }        }}intMain () {Freopen ("data.in","R", stdin); Freopen ("Data.out","W", stdout);    Aireen ();    Fclose (stdin);    Fclose (stdout); return 0;}

[Network Flow 24 questions] questions of question Bank

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.