Click to 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 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
give you a n*m matrix, and then a lattice has a number less than K, and tells you each row and each column of the and, let you ask whether there is a solution, and infer whether the unique.
Map: The source point and each row of the edge, the capacity for each row of the and. Each column and sink point is connected, and the capacity is the and of each column. Each row and each column is connected with a capacity of K.
Infer whether there is a unique solution and whether a ring can be found in the remaining 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;//The maximum value of the const int maxm=880010;//number of sides of the maximum const int inf=0x3f3f3f3f;struct node{int fr Om,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 remaining network is yvoid init () {tol=0; memset (head,-1,sizeof (Head));} void Addedge (int 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++) {edge[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)//There is a fault, no augmented road 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++)//infer if the residual network exists 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%d%d", &n,&m,&k)!=eof) {s=0,t=n+m+1; Init (); Build (); Solve (); }}
HDU Redraw Beautiful Drawings infers if the maximum flow is a unique solution