HDU 4888 Redraw Beautiful Drawings

Redraw Beautiful Drawings

Time Limit: 1500ms
Memory Limit: 65536KB
This problem will be judged on HDU. Original ID: 4888
64-bit integer IO format: %I64d      Java class name: Main
Alice and Bob are playing together. Alice is crazy about art and she has visited many 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 should also tell Bob whether he can 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 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.
 

Output

For 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 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

Source

 
解题:最大流判满流,判最大流的唯一性,即判残量网络中是否存在环,当然是长度不少于2的环。。
 
  1 #include <bits/stdc++.h>
  2 using namespace std;
  3 const int maxn = 810;
  4 const int INF = 0x3f3f3f3f;
  5 struct arc{
  6     int to,flow,next;
  7     arc(int x = 0,int y = 0,int z = -1){
  8         to = x;
  9         flow = y;
 10         next = z;
 11     }
 12 }e[maxn*maxn<<1];
 13 int head[maxn],d[maxn],cur[maxn],tot,S,T,n,m,k;
 14 bool vis[maxn];
 15 void add(int u,int v,int flow){
 16     e[tot] = arc(v,flow,head[u]);
 17     head[u] = tot++;
 18     e[tot] = arc(u,0,head[v]);
 19     head[v] = tot++;
 20 }
 21 bool bfs(){
 22     queue<int>q;
 23     memset(d,-1,sizeof d);
 24     d[S] = 1;
 25     q.push(S);
 26     while(!q.empty()){
 27         int u = q.front();
 28         q.pop();
 29         for(int i = head[u]; ~i; i = e[i].next){
 30             if(e[i].flow && d[e[i].to] == -1){
 31                 d[e[i].to] = d[u] + 1;
 32                 q.push(e[i].to);
 33             }
 34         }
 35     }
 36     return d[T] > -1;
 37 }
 38 int dfs(int u,int low){
 39     if(u == T) return low;
 40     int tmp = 0,a;
 41     for(int &i = cur[u]; ~i; i = e[i].next){
 42         if(e[i].flow && d[e[i].to] == d[u] + 1&&(a=dfs(e[i].to,min(low,e[i].flow)))){
 43             e[i].flow -= a;
 44             e[i^1].flow += a;
 45             low -= a;
 46             tmp += a;
 47             if(!low) break;
 48         }
 49     }
 50     if(!tmp) d[u] = -1;
 51     return tmp;
 52 }
 53 int dinic(){
 54     int ret = 0;
 55     while(bfs()){
 56         memcpy(cur,head,sizeof head);
 57         ret += dfs(S,INF);
 58     }
 59     return ret;
 60 }
 61 bool dfs2(int u,int fa){
 62     if(vis[u]) return true;
 63     vis[u] = true;
 64     for(int i = head[u]; ~i; i = e[i].next)
 65         if(e[i].flow && e[i].to != fa && dfs2(e[i].to,u)) return true;
 66     return vis[u] = false;
 67 }
 68 int main(){
 69     int tmp,sum,ans[maxn];
 70     while(~scanf("%d %d %d",&n,&m,&k)){
 71         memset(head,-1,sizeof head);
 72         S = n + m + 1;
 73         T = S + 1;
 74         sum = tot = 0;
 75         for(int i = 1; i <= n; ++i){
 76             scanf("%d",&tmp);
 77             add(S,i,tmp);
 78             sum += tmp;
 79             for(int j = 1; j <= m; ++j)
 80                 add(i,j+n,k);
 81         }
 82         for(int i = 1; i <= m; ++i){
 83             scanf("%d",&tmp);
 84             add(i+n,T,tmp);
 85         }
 86         sum -= dinic();
 87         if(sum) puts("Impossible");
 88         else{
 89             bool flag = false;
 90             memset(vis,false,sizeof vis);
 91             for(int i = 1; i <= n; ++i)
 92                 if(flag = dfs2(i,-1)) break;
 93             if(flag) puts("Not Unique");
 94             else{
 95                 puts("Unique");
 96                 for(int i = 1; i <= n; ++i){
 97                     for(int j = head[i]; ~j; j = e[j].next)
 98                         if(e[j].to > i) ans[e[j].to] = e[j^1].flow;
 99                     for(int i = 1; i <= m; ++i)
100                         printf("%d%c",ans[i+n],i==m?'\n':' ');
101                 }
102             }
103         }
104     }
105     return 0;
106 }
107 /*
108 4 2 2
109 2 2 5 0
110 5 4
111 */
View Code

 

posted @ 2015-05-02 14:03  狂徒归来  阅读(180)  评论(0编辑  收藏  举报