-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10092.cpp
174 lines (167 loc) · 2.93 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
10092 - The Problem with the Problem Setter
*/
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1105;
int graph[maxn][maxn];
int n,m,tt,sum;
bool bfs(int s,int t,int parent[])
{
bool visit[tt+1]= {false};
parent[s]=-1;
queue<int>q;
q.push(s);
while(!q.empty())
{
int u = q.front();
q.pop();
for(int v=0; v<=tt; v++)
{
if(visit[v]==false && graph[u][v]>0)
{
if(v==t)
{
parent[v]=u;
return true;
}
q.push(v);
visit[v]=true;
parent[v]=u;
}
}
}
return false;
}
int ford_fulkerson(int s,int t)
{
int u,v;
int parent[tt+1];
int max_flow=0;
while(bfs(s,t,parent))
{
int path_flow=INT_MAX;
for(v=t; v!=s; v=parent[v])
{
u = parent[v];
path_flow=min(path_flow,graph[u][v]);
}
for(v=t; v!=s; v=parent[v])
{
u = parent[v];
graph[u][v]-=path_flow;
graph[v][u]+=path_flow;
}
max_flow+=path_flow;
}
return max_flow;
}
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
while(cin>>n>>m && !(n==0 && m==0))
{
tt = n+m+1;
for(int i=0; i<=tt; i++)
{
for(int j=0; j<=tt; j++)
{
graph[i][j]=0;
}
}
sum=0;
for(int i=1; i<=n; i++)
{
cin>>graph[0][i];
sum+=(graph[0][i]);
}
for(int i=1; i<=m; i++)
{
int x;
cin>>x;
for(int j=1; j<=x; j++)
{
int y;
cin>>y;
///from y to i+n connected
graph[y][i+n]=1;
}
///from problem to sink
graph[i+n][tt]=1;
}
///making graph is done...
///now max flow
int ans = ford_fulkerson(0,tt);
if(ans==sum)
{
cout<<1<<endl;
for(int i=1; i<=n; i++)
{
int cnt=0;
for(int j=1; j<=m; j++)
{
if(graph[j+n][i]==1)
{
cnt++;
if(cnt>1)
cout<<" "<<j;
else
cout<<j;
}
}
cout<<endl;
}
}
else
{
cout<<0<<endl;
}
}
return 0;
}
/*
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
3 15
7 3 4
2 1 2
1 1
1 2
1 2
1 3
3 1 2 3 2 2 3
2 2 3
1 2
1 2
2 2 3
2 2 3
2 1 2
1 1
3 1 2 3
0 0
Sample Output
1
8 11 12
1 6 7
2 3 4 5
0
*/