Hdoj 4888 Redraw Beautiful drawings

Source: Internet
Author: User


Maximum Flow judgment multiple solutions

Build diagram:

The source point is connected to each of the row's node capacity for the row sum, each node that represents the column is connected to the sink capacity as a column sum, and the rows and columns are connected to each other with a limit of capacity

Multiple solutions:

After doing the ISAP, on the residual map DFS see whether the number of points greater than 2 can be found

Redraw Beautiful Drawings Time limit:3000/1500 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 3519 Accepted Submission (s): 1060


Problem Descriptionalice 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. 
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 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.
Outputfor each testcase, if there are no solution for Bob, output "impossible" on 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 2  4 2 22 2 5 05 41 4 391 2 3 3

Sample Output
Not UniqueImpossibleUnique1 2 3 3

Authorfudan University
Source2014 multi-university Training Contest 3

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int maxn=1000;const int maxm=1001000;const int inf=0x3f3f3f3f;struct edge{int To,next,cap,flow;} Edge[maxm];int size,adj[maxn];int gap[maxn],dep[maxn],pre[maxn],cur[maxn];void init () {Size=0; memset (Adj,-1,sizeof (ADJ));} void Addedge (int u,int v,int w,int rw=0) {edge[size].to=v; edge[size].cap=w; edge[size].next=adj[u];edge[size].flow=0; Adj[u]=size++;edge[size].to=u; EDGE[SIZE].CAP=RW; edge[size].next=adj[v];edge[size].flow=0; adj[v]=size++;} int sap (int start,int end,int N) {memset (gap,0,sizeof (GAP)); Memset (Dep,0,sizeof (DEP)); memcpy (Cur,adj,sizeof (ADJ)); 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;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;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;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=adj[u];~i;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 n,m,limit;int a[maxn],b[maxn];int id[maxn][maxn];bool vis[maxn];bool dfs (int u,int pre) {if (Vis[u]) return true;vis[ u]=true;for (int i=adj[u];~i;i=edge[i].next) {int v=edge[i].to;if (EDGE[I].FLOW&GT;=EDGE[I].CAP) continue;if (V==pre) Continue;if (Dfs (V,U)) return true; Vis[u]=false;return false;} int main () {while (scanf ("%d%d%d", &n,&m,&limit)!=eof) {init (); int sum1=0,sum2=0;for (int i=1;i<=n;i++) scanf ("%d", A+i), sum1+=a[i];for (int i=1;i<=m;i++) scanf ("%d", B+i), Sum2+=b[i];if (sum1!=sum2) {puts ("impossible") ; continue;} /**************build graph *****************/for (int i=1; i<=n;i++) for (int j=1;j<=m;j++) {Id[i][j]=size;addedge (i,n+j,limit);} for (int i=1;i<=n;i++) Addedge (0,i,a[i]), for (int i=1;i<=m;i++) Addedge (N+i,n+m+1,b[i]);/**************build Graph *****************/int Maxflow=sap (0,n+m+1,n+m+1+2);//cout<< "Maxflow:" <<maxflow<<endl;if ( MAXFLOW!=SUM1) {puts ("impossible"); continue;} Else{memset (vis,0,sizeof (VIS)), bool flag=false;for (int i=1;i<=n&&!flag;i++) if (Dfs (i,i)) flag=true;if ( Flag) {puts ("not Unique"); continue;} Puts ("Unique"); for (int. i=1;i<=n;i++) for (int j=1;j<=m;j++) {printf ("%d%c", Edge[id[i][j]].flow, (j==m), 10:32);}}} return 0;}



Hdoj 4888 Redraw Beautiful drawings

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.