HDU 4888 Redraw Beautiful Drawings (最大流)

HDU 4888

Redraw Beautiful Drawings

Redraw Beautiful Drawings

Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1856 Accepted Submission(s): 415

Problem Description

Alice and Bob are playing together. Alice is crazy about art and she has visited many museums around the world. She has a good memory and she can remember all drawings she has seen. Today Alice designs a game using these drawings in her memory. First, she matches K+1 colors appears in the picture to K+1 different integers(from 0 to K). After that, she slices the drawing into grids and there are N rows and M columns. Each grid has an integer on it(from 0 to K) representing the color on the corresponding position in the original drawing. Alice wants to share the wonderful drawings with Bob and she tells Bob the size of the drawing, the number of different colors, and the sum of integers on each row and each column. Bob has to redraw the drawing with Alice’s information. Unfortunately, somtimes, the information Alice offers is wrong because of Alice’s poor math. And sometimes, Bob can work out multiple different drawings using the information Alice provides. Bob gets confused and he needs your help. You have to tell Bob if Alice’s information is right and if her information is right you should also tell Bob whether he can get a unique drawing.

Input

The input contains mutiple testcases. For each testcase, the first line contains three integers N(1 ≤ N ≤ 400) , M(1 ≤ M ≤ 400) and K(1 ≤ K ≤ 40). N integers are given in the second line representing the sum of N rows. M integers are given in the third line representing the sum of M columns. The input is terminated by EOF.

Output

For each testcase, if there is no solution for Bob, output “Impossible” in one line(without the quotation mark); if there is only one solution for Bob, output “Unique” in one line(without the quotation mark) and output an N * M matrix in the following N lines representing Bob’s unique solution; if there are many ways for Bob to redraw the drawing, output “Not Unique” in one line(without the quotation mark).

Sample Input

2 2 4 4 2 4 2 4 2 2 2 2 5 0 5 4 1 4 3 9 1 2 3 3

Sample Output

Not Unique Impossible Unique 1 2 3 3

用最大流去判断有没有解。 判断多解的话,在残余网络中看能不能找到长度大于2的环。  
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/* ***************

Author :kuangbin

Created Time :2014/8/3 13:06:32

File Name :E:\2014ACM\比赛\2014多校训练\2014多校3\HDU4888.cpp

************************************************ */

#include <stdio.h>

#include <string.h>

#include <iostream>

#include <algorithm>

#include <vector>

#include <queue>

#include <set>

#include <map>

#include <string>

#include <math.h>

#include <stdlib.h>

#include <time.h>

using namespace std;

const int MAXN = 1010;//点数的最大值

const int MAXM = 400010;//边数的最大值

const int INF = 0x3f3f3f3f;

struct Edge

{

int to,next,cap,flow;

}edge[MAXM];//注意是MAXM

int tol;

int head[MAXN];

int gap[MAXN],dep[MAXN],pre[MAXN],cur[MAXN];

void init()

{

tol = 0;

memset(head,-1,sizeof(head));

}

//加边,单向图三个参数,双向图四个参数

void addedge(int u,int v,int w,int rw=0)

{

edge[tol].to = v;edge[tol].cap = w;edge[tol].next = head[u];

edge[tol].flow = 0;head[u] = tol++;

edge[tol].to = u;edge[tol].cap = rw;edge[tol].next = head[v];

edge[tol].flow = 0;head[v]=tol++;

}

//输入参数:起点、终点、点的总数

//点的编号没有影响,只要输入点的总数

int sap(int start,int end,int N)

{

memset(gap,0,sizeof(gap));

memset(dep,0,sizeof(dep));

memcpy(cur,head,sizeof(head));

int u = start;

pre[u] = -1;

gap[0] = N;

int ans = 0;

while(dep[start] < N)

{

if(u == end)

{

int Min = INF;

for(int i = pre[u];i != -1; i = pre[edge[i^1].to])

if(Min > edge[i].cap - edge[i].flow)

Min = edge[i].cap - edge[i].flow;

for(int i = pre[u];i != -1; i = pre[edge[i^1].to])

{

edge[i].flow += Min;

edge[i^1].flow -= Min;

}

u = start;

ans += Min;

continue;

}

bool flag = false;

int v;

for(int i = cur[u]; i != -1;i = edge[i].next)

{

v = edge[i].to;

if(edge[i].cap - edge[i].flow && dep[v]+1 == dep[u])

{

flag = true;

cur[u] = pre[v] = i;

break;

}

}

if(flag)

{

u = v;

continue;

}

int Min = N;

for(int i = head[u]; i != -1;i = edge[i].next)

if(edge[i].cap - edge[i].flow && dep[edge[i].to] < Min)

{

Min = dep[edge[i].to];

cur[u] = i;

}

gap[dep[u]]--;

if(!gap[dep[u]])return ans;

dep[u] = Min+1;

gap[dep[u]]++;

if(u != start) u = edge[pre[u]^1].to;

}

return ans;

}

int a[500];

int b[500];

int id[500][500];

bool vis[MAXN];

bool dfs(int u,int pre)

{

//printf("%d %d\n",pre,u);

if(vis[u])return true;

vis[u] = true;

for(int i = head[u];i != -1;i = edge[i].next)

{

int v = edge[i].to;

if(edge[i].cap <= edge[i].flow)continue;

if(v == pre)continue;

if(dfs(v,u))return true;

}

vis\[u\] = false;
return false;

}

int main()

{

//freopen("in.txt","r",stdin);

//freopen("out.txt","w",stdout);

int n,m,K;

while(scanf("%d%d%d",&n,&m,&K) == 3)

{

init();

int sum1 = 0;

for(int i = 1;i <= n;i++)

{

scanf("%d",&a[i]);

sum1 += a[i];

addedge(0,i,a[i]);

}

int sum2 = 0;

for(int i = 1;i <= m;i++)

{

scanf("%d",&b[i]);

sum2 += b[i];

addedge(i+n,n+m+1,b[i]);

}

if(sum1 != sum2)

{

printf("Impossible\n");

continue;

}

for(int i = 1;i <= n;i++)

for(int j = 1;j <= m;j++)

{

id[i][j] = tol;

addedge(i,j+n,K);

}

int tmp = sap(0,n+m+1,n+m+2);

if(tmp != sum1)

{

printf("Impossible\n");

continue;

}

memset(vis,false,sizeof(vis));

bool flag = false;

for(int i = 1;i <= n;i++)

if(dfs(i,i))

{

flag = true;

break;

}

if(flag)

{

printf("Not Unique\n");

continue;

}

printf("Unique\n");

for(int i = 1;i <= n;i++)

for(int j = 1;j <= m;j++)

{

printf("%d",edge[id[i][j]].flow);

if(j < m)printf(" ");

else printf("\n");

}

}

return 0;

}
------ 本文结束------
  • 本文作者: kuangbin
  • 本文链接: 309.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
0%