HDU Redraw Beautiful Drawings determines whether the maximum stream is the unique solution, hduredraw

Source: Internet
Author: User

HDU Redraw Beautiful Drawings determines whether the maximum stream is the unique solution, hduredraw
Click Open Link
Redraw Beautiful DrawingsTime Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission (s): 1660 Accepted Submission (s): 357


Problem DescriptionAlice and Bob are playing together. Alice is crazy about art and she has visited into 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 shoshould also tell Bob whether he can get a unique drawing.
InputThe 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.
OutputFor 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 always ways for Bob to redraw the drawing, output "Not Unique" in one line (without the quotation mark ).
Sample Input

2 2 44 24 2  4 2 22 2 5 05 41 4 391 2 3 3
 
Sample Output
Not UniqueImpossibleUnique1 2 3 3
 
AuthorFudan University
Give you a matrix of n * m, and then there is a number smaller than k in the grid, and tell you the sum of each row and each column, so that you can determine whether there is a solution, and determine whether it is unique. Graph creation: The Source Vertex and the connected edge of each line. The capacity is the sum of each line. Edge connecting each column and sink point. The capacity is the sum of each column. The size of each row and each column is k. Determine whether a unique solution exists and whether a ring can be found in the residual network.
// 515MS7304K # include <stdio. h> # include <string. h> # define M 1007int s, t, n, m, k, sum1, sum2; int row [M], col [M], vis [M], g [M] [M]; const int MAXN = 20010; // maximum number of points const int MAXM = 880010; // maximum number of edges const int INF = 0x3f3f3f3f; struct Node {int from, to, next; int cap;} edge [MAXM]; int tol; int head [MAXN]; int dis [MAXN]; int gap [MAXN]; // gap [x] = y: indicates that the number of dis [I] = x in the residual network is yvoid init () {tol = 0; memset (head,-1, sizeof (head);} void addedge (I Nt u, int v, int w) {edge [tol]. from = u; edge [tol]. to = v; edge [tol]. cap = w; edge [tol]. next = head [u]; head [u] = tol ++; edge [tol]. from = v; edge [tol]. to = u; edge [tol]. cap = 0; edge [tol]. next = head [v]; head [v] = tol ++;} void BFS (int start, int end) {memset (dis,-1, sizeof (dis )); memset (gap, 0, sizeof (gap); gap [0] = 1; int que [MAXN]; int front, rear; front = rear = 0; dis [end] = 0; que [rear ++] = end; while (front! = Rear) {int u = que [front ++]; if (front = MAXN) front = 0; for (int I = head [u]; I! =-1; I = edge [I]. next) {int v = edge [I]. to; if (dis [v]! =-1) continue; que [rear ++] = v; if (rear = MAXN) rear = 0; dis [v] = dis [u] + 1; ++ gap [dis [v] ;}} int SAP (int start, int end) {int res = 0, nn = end + 1; BFS (start, end); int cur [MAXN]; int S [MAXN]; int top = 0; memcpy (cur, head, sizeof (head); int u = start; int I; while (dis [start] <nn) {if (u = end) {int temp = INF; int inser; for (I = 0; I <top; I ++) if (temp> edge [S [I]. cap) {temp = edge [S [I]. cap; inser = I ;}for (I = 0; I <top; I ++) {e Dge [S [I]. cap-= temp; edge [S [I] ^ 1]. cap + = temp;} res + = temp; top = inser; u = edge [S [top]. from;} if (u! = End & gap [dis [u]-1] = 0) // fault occurs, without the zengguang break; for (I = cur [u]; I! =-1; I = edge [I]. next) if (edge [I]. cap! = 0 & dis [u] = dis [edge [I]. to] + 1) break; if (I! =-1) {cur [u] = I; S [top ++] = I; u = edge [I]. to;} else {int min = nn; for (I = head [u]; I! =-1; I = edge [I]. next) {if (edge [I]. cap = 0) continue; if (min> dis [edge [I]. to]) {min = dis [edge [I]. to]; cur [u] = I ;}-- gap [dis [u]; dis [u] = min + 1; ++ gap [dis [u]; if (u! = Start) u = edge [S [-- top]. from ;}} return res;} void build () {sum1 = 0, sum2 = 0; for (int I = 1; I <= n; I ++) {scanf ("% d", & row [I]); sum1 + = row [I]; addedge (s, I, row [I]);} for (int I = 1; I <= m; I ++) {scanf ("% d", & col [I]); sum2 + = col [I]; addedge (n + I, t, col [I]);} for (int I = 1; I <= n; I ++) for (int j = 1; j <= m; j ++) addedge (I, n + j, k);} bool iscycle (int u, int fa) {for (int I = head [u]; I! =-1; I = edge [I]. next) {if (I = (fa ^ 1) continue; if (edge [I]. cap) {if (vis [edge [I]. to]) return true; vis [edge [I]. to] = true; if (iscycle (edge [I]. to, I) return true; vis [edge [I]. to] = false ;}} return false;} void solve () {if (sum1! = Sum2) {printf ("Impossible \ n"); return;} int anss = SAP (s, t); // printf ("anss = % d \ n ", anss); if (anss! = Sum1) {printf ("Impossible \ n"); return;} memset (vis, false, sizeof (vis); int flag = 0; for (int I = 1; I <= n; I ++) // determine whether the residual network has a ring if (iscycle (I,-1) {flag = 1; break;} if (flag) printf ("Not Unique \ n"); else {printf ("Unique \ n"); for (int u = 1; u <= n; u ++) for (int I = head [u]; I! =-1; I = edge [I]. next) {int v = edge [I]. to; if (v> n & v <= n + m) g [u] [v-n] = k-edge [I]. cap ;}for (int I = 1; I <= n; I ++) {for (int j = 1; j <m; j ++) printf ("% d", g [I] [j]); printf ("% d \ n", g [I] [m]) ;}} int main () {while (scanf ("% d", & n, & m, & k )! = EOF) {s = 0, t = n + m + 1; init (); build (); solve ();}}





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.