-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10092.cpp
145 lines (145 loc) · 2.52 KB
/
10092.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//网络流最大流(快)或者二分图多重匹配(简单)
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#define INF 100000
using namespace std;
struct edge
{
int v,cap,flow,left;
};
struct Graph
{
vector<edge> edges[1100];
int n,m;
};
Graph G;
int d[1100];
bool BFS()
{
bool vis[1100];
memset(vis,0,sizeof(vis));
queue<int> q;
q.push(0);
d[0]=0;
vis[0]=1;
while (!q.empty())
{
int x=q.front();q.pop();
for (int i=0;i<G.edges[x].size();i++)
if (!vis[G.edges[x][i].v] && G.edges[x][i].cap>G.edges[x][i].flow)
{
vis[G.edges[x][i].v]=1;
d[G.edges[x][i].v]=d[x]+1;
q.push(G.edges[x][i].v);
}
}
return vis[G.n];
}
void find(int u,int v,int f)
{
for (int i=0;i<G.edges[u].size();i++)
if (G.edges[u][i].v==v)
{
G.edges[u][i].flow-=f;
return ;
}
}
int dfs(int x,int a,int *cur)
{
if (x==G.n || a==0) return a;
int flow=0,f;
for (int i=cur[x];i<G.edges[x].size();i++)
if (d[x]+1==d[G.edges[x][i].v] && (f=dfs(G.edges[x][i].v,min(a,G.edges[x][i].cap-G.edges[x][i].flow),cur))>0)
{
G.edges[x][i].flow+=f;
find(G.edges[x][i].v,x,f);
flow+=f;
a-=f;
if (a==0) break;
}
return flow;
}
int maxflow()
{
int flow=0;
int cur[1100];
while (BFS())
{
memset(cur,0,sizeof(cur));
flow+=dfs(0,INF,cur);
}
return flow;
}
main()
{
int ink[22];
int nk,np;
while (scanf("%d%d",&nk,&np)==2)
{
if (nk==0 && np==0) break;
for (int i=0;i<=nk+np+1;i++) G.edges[i].clear();
int sum=0;
for (int i=1;i<=nk;i++)
{
scanf("%d",&ink[i]);
sum+=ink[i];
}
for (int i=nk+1;i<=nk+np;i++)
{
int t,s;
edge tmp;
tmp.flow=0,tmp.left=0;
scanf("%d",&t);
while (t--)
{
scanf("%d",&s);
tmp.v=s,tmp.cap=1;tmp.flow=0;
G.edges[i].push_back(tmp);
tmp.v=i,tmp.cap=0;tmp.flow=0;
G.edges[s].push_back(tmp);
}
}
G.n=nk+np+1;
for (int i=nk+1;i<=nk+np;i++)
{
edge tmp;
tmp.v=i,tmp.cap=1;
tmp.flow=0,tmp.left=0;
G.edges[0].push_back(tmp);
tmp.v=0;
G.edges[i].push_back(tmp);
}
for (int i=1;i<=nk;i++)
{
edge tmp;
tmp.v=i;
tmp.flow=0;tmp.cap=0;
G.edges[G.n].push_back(tmp);
tmp.v=G.n;
tmp.cap=ink[i];
G.edges[i].push_back(tmp);
}
int ans=maxflow();
if (ans==sum)
{
printf("1\n");
for (int i=1;i<=nk;i++)
{
bool space=false;
for (int j=0;j<G.edges[i].size();j++)
if (G.edges[i][j].cap-G.edges[i][j].flow)
{
if (space) printf(" ");
space=true;
printf("%d",G.edges[i][j].v-nk);
}
putchar('\n');
}
}
else printf("0\n");
}
return 0;
}