Whether the maximum flow judgment equation has a solution

Source: Internet
Author: User

Redraw Beautiful Drawings

Time limit:3000/1500 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 3813 Accepted Submission (s): 1140


Problem Description
Alice and Bob are playing together. Alice was crazy about art and she had visited many museums around the world. She had a good memory and she can remember all drawings she had 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 is N rows and M columns. Each grid has a integer on it (from 0 to K) representing the color of 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 co Lors, and the sum of integers on each row and each column. Bob had 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 had to tell Bob if Alice's information is right and if she information was right you should also tell Bob whether he C An 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 is given in the second line representing the sum of n rows.
m integers is given in the third line representing the sum of M columns.

The input is terminated by EOF.

Output
For each testcase, if there are no solution for Bob, output "impossible" in one line (without the quotation mark); If there is a 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 is 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

Test instructions: Give a matrix of the number of rows and columns, and then give the matrix element range K ([0,k]), then the n number represents the sum of each row, and then the number of M represents the sum of each column, ask if the matrix exists, if there is no output impossible, if there is more than one solution output not Unique, otherwise the output is unique and outputs a viable solution.

Analysis: First build two, the source point and the meeting point, the left part of the two chart is a row (1-n), the right part is a column (n+1,n+m), and then the source and row connection weights are the row and, the column and the meeting point of the value is columns, and then each row and 22 of each column value is K, First Judge Sigma ( and Sigma (Col[i]) are equal, if not equal, then judge impossible, otherwise continue, and then determine the maximum flow max_flow is not the same as Sigma (Cow[i]), if not, then impossible, otherwise continue, Next from the column number to the residue network search, determine whether there is a ring, if there are rings and the number of vertices of the ring is greater than 2, there are multiple solutions, otherwise there is a unique solution

Program

#include"string.h"#include"stdio.h"#include"iostream"#include"algorithm"#include"Queue"#include"Stack"#include"stdlib.h"#include"math.h"#defineINF 10000000#defineINF 0x3f3f3f3f#defineM 1009using namespacestd;structnode{intU,v,w,next;} Edge[m* -];intT,head[m],dis[m],use[m],work[m],mp[m][m];voidinit () {T=0; memset (Head,-1,sizeof(head));}voidAddintUintVintWintFW) {edge[t].u=T; EDGE[T].V=v; EDGE[T].W=W; Edge[t].next=Head[u]; Head[u]=t++; EDGE[T].U=v; EDGE[T].V=T; EDGE[T].W=FW; Edge[t].next=Head[v]; HEAD[V]=t++;}intBFsintSourceintsink) {Queue<int>Q; memset (DIS,-1,sizeof(DIS));    Q.push (source); Dis[source]=0;  while(!Q.empty ()) {        intu=Q.front ();        Q.pop ();  for(intI=head[u];~i;i=Edge[i].next) {            intv=edge[i].v; if(edge[i].w&&dis[v]==-1) {Dis[v]=dis[u]+1;                Q.push (v); if(V==sink)return 1; }        }    }    return 0;}intDfsintCurintAintsink) {    if(Cur==sink)returnA;  for(int&i=work[cur];~i;i=Edge[i].next) {        intv=edge[i].v; if(edge[i].w&&dis[v]==dis[cur]+1)        {            inttt=Dfs (V,min (A,EDGE[I].W), sink); if(TT) {EDGE[I].W-=tt; Edge[i^1].w+=tt; returntt; }        }    }    return 0;}intDinic (intSourceintsink) {    intcn1=0;  while(BFS (Source,sink)) {memcpy (Work,head,sizeof(head));  while(inttt=DFS (Source,inf,sink)) ans+=tt; }    returnans;}intDFS (intUintID) {Use[u]=1;  for(intI=head[u];~i;i=Edge[i].next) {        intv=edge[i].v; if(id== (i^1))Continue; if(EDGE[I].W) {if(Use[v])return 1; if(DFS (v,i))return 1; }} Use[u]=0; return 0;}intSolveintNintm) {memset (use,0,sizeof(use));  for(intI=1; i<=n;i++)    {        if(DFS (i,-1))            return 1; }    return 0;}intMain () {intN,m,k,row,col;  while(SCANF ("%d%d%d", &n,&m,&k)!=-1) {init (); intRows=0, cols=0;  for(intI=1; i<=n;i++) {scanf ("%d",&row); Add (0, I,row,0); Rows+=Row; }         for(intI=1; i<=m;i++) {scanf ("%d",&col); Add (i+n,m+n+1, col,0); Cols+=Col; }        if(cols!=rows) {printf ("impossible\n"); Continue; }         for(intI=1; i<=n;i++)        {             for(intj=1; j<=m;j++) {Add (I,j+n,k,0); }        }        intAns=dinic (0, m+n+1); intmsg=solve (n,m); if(ans!=rows) printf ("impossible\n"); Else if(msg) printf ("Not unique\n"); Else{printf ("unique\n");  for(intI= (m+n) *2; i<t;i+=2)            {                intu=edge[i].u; intv=edge[i].v; intw=edge[i^1].W; Mp[u][v-n]=W; }             for(intI=1; i<=n;i++)            {                 for(intj=1; j<=m;j++)                {                    if(j==1) printf ("%d", Mp[i][j]); Elseprintf ("%d", Mp[i][j]); } printf ("\ n"); }        }    }    return 0;}

Whether the maximum flow judgment equation has a solution

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.