HDU4888 Redraw Beautiful drawings (maximum flow uniqueness determination: Residual network censored)

Source: Internet
Author: User

TopicsSource

http://acm.hdu.edu.cn/showproblem.php?pid=4888

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

Analysis

The topic probably said that there is a matrix, the value of each unit is known between 0 to K and the rows of each column of the and, the need to restore the matrix, if the only output solution.

It's a classic composition, POJ2396.

    • Consider rows and columns as points, and each unit as an edge

The crux of the problem is to determine the maximum flow uniqueness.

This conclusion is that the residual network does not exist The ring is the maximum flow is the only necessary and sufficient conditions. Find the flow around a circle to adjust the top of the ring, such as Flow 1 (at the same time the corresponding opposite side or forward edge traffic + 1), which would be a maximum flow that satisfies the equilibrium condition.

Finding loops in the residual network cannot be sorted by topology. Because the forward side and the opposite side should be regarded as one, that is, if the positive side of the contribution of the degree of access, then the reverse edge of the degree of access does not need statistics, and vice versa, and we can not determine whether the sides are selected forward or reverse.

So the ring was searched for:

BOOL Dfs (int u,int fa) {    vis[u]=1;    for (int i=head[u]; i!=-1; i=edge[i].next) {        int v=edge[i].v;        if (V==fa | | edge[i].cap==edge[i].flow) continue;        if (Vis[v]) return 1;        if (Dfs (V,U)) return 1;    }    vis[u]=0;    return 0;}

But this is all about the complexity of the time-scale.

I saw the practice of deleting edges on this blog. Indeed, after a retrospective explanation to the current side of the walk is not find the ring, and then do not need to go to this side, so that each side only walk once to ensure that the time complexity of the polynomial level.

But I do not understand the way the blog is deleted, I will be relatively low to mark the deletion of the edge, encountered the marked on the skip. But this is HDU4975 is tle, so I think about how to really delete the edge, and then realized the next, or tle, this is not linear time complexity of it? Well, I chose to give up the question.

There is also a can be optimized is the starting point of the enumeration ring as long as the enumeration capacity network row corresponding points, I probably think of the next is because the problem of building a two-point map, so that the points from the column corresponding to find the ring must be through the line corresponding points, so from the line corresponding to the point can also form this ring.

Code
#include <cstdio> #include <cstring> #include <queue> #include <algorithm>using namespace std;# Define INF (1<<30) #define MAXN 888#define maxm 444*888struct edge{int v,cap,flow,next;} Edge[maxm];int vs,vt,ne,nv;int head[maxn];void addedge (int u,int v,int cap) {edge[ne].v=v; edge[ne].cap=cap; edge[NE].f    low=0; Edge[ne].next=head[u];    head[u]=ne++; Edge[ne].v=u; Edge[ne].cap=0;    edge[ne].flow=0; EDGE[NE].NEXT=HEAD[V]; head[v]=ne++;}    int level[maxn];int gap[maxn];void BFS () {memset (level,-1,sizeof (level));    memset (Gap,0,sizeof (GAP));    level[vt]=0;    gap[level[vt]]++;    Queue<int> que;    Que.push (VT);        while (!que.empty ()) {int U=que.front (); Que.pop ();            for (int i=head[u]; i!=-1; i=edge[i].next) {int v=edge[i].v;            if (level[v]!=-1) continue;            level[v]=level[u]+1;            gap[level[v]]++;        Que.push (v);    }}}int pre[maxn];int Cur[maxn];int Isap () {BFS (); memset (pre,-1,sizeof (pre));    memcpy (cur,head,sizeof (head));    int u=pre[vs]=vs,flow=0,aug=inf;    GAP[0]=NV;        while (LEVEL[VS]&LT;NV) {bool Flag=false;            for (int &i=cur[u]; i!=-1; i=edge[i].next) {int v=edge[i].v;                if (Edge[i].cap!=edge[i].flow && level[u]==level[v]+1) {flag=true;                Pre[v]=u;                U=v;                Aug= (Aug==-1?edge[i].cap:min (Aug,edge[i].cap));                Aug=min (Aug,edge[i].cap-edge[i].flow);                    if (V==VT) {Flow+=aug;                        for (u=pre[v]; v!=vs; V=u,u=pre[u]) {Edge[cur[u]].flow+=aug;                    Edge[cur[u]^1].flow-=aug;                    }//aug=-1;                Aug=inf;            } break;        }} if (flag) continue;        int MINLEVEL=NV;            for (int i=head[u]; i!=-1; i=edge[i].next) {int v=edge[i].v; if (Edge[i].cap!=edge[i].Flow && level[v]<minlevel) {minlevel=level[v];            Cur[u]=i;        }} if (--gap[level[u]]==0) break;        level[u]=minlevel+1;        gap[level[u]]++;    U=pre[u]; } return flow;}    BOOL Vis[maxn];bool dfs (int u,int fa) {vis[u]=1; for (int i=head[u],pre=i; i!=-1;)        {int v=edge[i].v;            if (V==fa | | edge[i].cap==edge[i].flow) {pre=i;            I=edge[i].next;        Continue        } if (Vis[v]) return 1;        if (Dfs (V,U)) return 1;            if (I==head[u]) {i=edge[i].next;        Head[u]=i;            }else{I=edge[i].next;        edge[pre].next=i;    }} vis[u]=0; return 0;}    int Ans[444][444];int Main () {int n,m,k,a; while (~SCANF ("%d%d%d", &n,&m,&k)) {vs=0; vt=n+m+1; nv=vt+1;        ne=0;        memset (head,-1,sizeof (head));        int row=0,col=0;            for (int i=1; i<=n; ++i) {scanf ("%d", &a); AddedgE (vs,i,a);        Row+=a;            } for (int i=1; i<=m; ++i) {scanf ("%d", &a);            Addedge (I+n,vt,a);        Col+=a;            } if (Row!=col) {puts ("impossible");        Continue            } for (int i=1, i<=n; ++i) {for (int j=1; j<=m; ++j) {Addedge (i,j+n,k);            }} if (Isap ()!=row) {puts ("impossible");        Continue } for (int u=1, u<=n; ++u) {for (int i=head[u]; i!=-1; i=edge[i].next) {if (I&AMP;1) cont                Inue;                int v=edge[i].v-n;            Ans[u][v]=edge[i].flow;        }} bool Flag=1;        memset (vis,0,sizeof (VIS));                for (int i=1; i<=n; ++i) {if (Dfs (i,i)) {flag=0;            Break            }} if (!flag) {puts ("not Unique");        Continue        } puts ("Unique");   for (int i=1; i<=n; ++i) {         for (int j=1; j<=m; ++j) {if (j!=1) Putchar (');            printf ("%d", ans[i][j]);        } putchar (' \ n '); }} return 0;}

HDU4888 Redraw Beautiful drawings (maximum flow uniqueness determination: Residual network censored)

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.