-
Notifications
You must be signed in to change notification settings - Fork 2
/
10092.cpp
175 lines (159 loc) · 2.56 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
175
#include<iostream>
#include<list>
#include<queue>
using namespace std;
#define maxn 1024
list<int>link[maxn];
list<int>Cat[22];
int Cost[maxn][maxn];
int parent[maxn];
char Match[maxn][maxn];
int Block[maxn];
int Req[22], A[22];
char Fg[maxn];
queue<int>Q;
int P, CT, Des, Ter, ind;
int SetAll() {
int i, d, r;
Des = P+CT+1;
Ter = 0;
for(i = 1; i <= CT; i++) {
d = P+i;
link[d].push_back(Des);
Cost[d][Des] = Req[i];
Ter += Req[i];
}
return 0;
}
void Print() {
int c, i, j;
list<int>::iterator p;
cout<<"1\n";
for(i = 1; i<= CT; i++) {
c = 0;
for(p = Cat[i].begin(); p != Cat[i].end(); p++) {
j = *p;
if(c++) cout<<" ";
cout<<j;
}
cout<<endl;
}
}
void Recur(int n) {
if(n < 0) return;
Recur(parent[n]);
A[ind++] = n;
}
void Mark(int n) {
int i, u , v;
ind = 0;
Recur(Des);
for(i = 1; i<ind; i++) {
u = A[i-1];
v = A[i];
Match[u][v] = 1;
Match[v][u] = 0;
Cost[u][v] -= 1;
Cost[v][u] += 1;
}
Ter --;
}
void BFS(int n) {
int u, v;
for(u = 1; u <= Des; u++)
Fg[u] = 0;
list<int>::iterator p;
parent[n] = -1;
Q.push(n);
Fg[n] = 1;
while(!Q.empty()) {
u = Q.front();
Q.pop();
for(p = link[u].begin(); p != link[u].end(); p++) {
v = *p;
if(Cost[u][v] == 0 || Block[v] || Fg[v]) continue;
parent[v] = u;
if(v == Des) {
Mark(n);
return;
}
Fg[v] = 1;
Q.push(v);
}
}
}
int Impossible() {
int i, d;
for(i = 1; i<= CT; i++) {
d = i + P;
if(Req[i] > link[d].size()) return 1;
}
return 0;
}
void Cal() {
int i;
/* if(Impossible()) {
cout<<"0\n";
return;
}*/
if(SetAll()) {
Print();
return;
}
for(i = 1; i<= P && Ter; i++) {
BFS(i);
while(!Q.empty()) Q.pop();
}
if(Ter) cout<<"0\n";
else {
for(i = 1; i<= P; i++) {
if(Block[i]) continue;
for(int j = 1; j<= CT ; j ++) {
if(Match[i][j+P]) {
Cat[j].push_back(i);
break;
}
}
}
Print();
}
}
void Free() {
int i, c;
list<int>::iterator p;
for(i = 1; i<= CT; i++)
Cat[i].clear();
for(i = 1; i<= P; i++) {
for(p = link[i].begin(); p != link[i].end(); p++) {
c = *p;
Match[i][c] = Match[c][i] = 0;
Cost[c][i] = Cost[i][c] = 0;
}
link[i].clear();
}
for(i = P+1; i<= P+CT; i++) {
link[i].clear();
Cost[i][Des] = Cost[Des][i] = 0;
}
}
int main() {
int i, c, j, nc;
//freopen("h.txt","r",stdin);
while(cin>>CT>>P) {
if(!P && !CT) break;
for(i = 1; i<= CT; i++)
cin>>Req[i];
for(i = 1; i<= P; i++) {
cin>>nc;
for( j = 0; j<nc; j++) {
cin>>c;
link[i].push_back(c+P);
link[c+P].push_back(i);
Cost[i][c+P] = 1;
}
}
Cal();
Free();
}
return 0;
}