Redraw Beautiful Drawings
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 3802 Accepted Submission(s): 1133
Problem Description
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.
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.
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
Author
Fudan University
Source
n*m的矩阵,每个点可以放0-k的数,给你每行每列的和,问可行否,可行的话解是否唯一,唯一则输出那组解
第一步,考虑如何求是否有解。使用网络流求解,每一行和每一列分别对应一个点,加上源点和汇点一共有N+M+2个点。有三类边:
1.
2.
3.
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009//根据题目要求进行修改
#define MAXN 20010//根据数据大小进行修改
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl;
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
using namespace std;
struct Edge
{
int from,to,cap,flow;
bool operator <(const Edge e) const
{
if(e.from!=from) return from<e.from;
else return to<e.to;
}
Edge() {}
Edge(int from,int to,int cap,int flow):from(from),to(to),cap(cap),flow(flow) {}
};
struct Dinic
{
vector<Edge> edges;
vector<int> G[MAXN];
bool vis[MAXN];//BFS使用
int d[MAXN]; //从起点到i的距离
int cur[MAXN]; //当前弧下标
int n,m,s,t,maxflow; //节点数 边数(包括反向弧) 源点编号和弧点编号
void init(int n)
{
this->n=n;
for(int i=0;i<=n;i++)
G[i].clear();
edges.clear();
}
void addedge(int from,int to,int cap)
{
edges.push_back(Edge(from,to,cap,0));
edges.push_back(Edge(to,from,0,0));
m=edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
bool bfs()
{
MEM(vis,0);
MEM(d,-1);
queue<int> q;
q.push(s);
d[s]=maxflow=0;
vis[s]=1;
while(!q.empty())
{
int u=q.front(); q.pop();
int sz=G[u].size();
for(int i=0;i<sz;i++)
{
Edge e=edges[G[u][i]];
if(!vis[e.to]&&e.cap>e.flow)
{
d[e.to]=d[u]+1;
vis[e.to]=1;
q.push(e.to);
}
}
}
return vis[t];
}
int dfs(int u,int a)
{
if(u==t||a==0) return a;
int sz=G[u].size();
int flow=0,f;
for(int &i=cur[u];i<sz;i++)
{
Edge &e=edges[G[u][i]];
if(d[u]+1==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0)
{
e.flow+=f;
edges[G[u][i]^1].flow-=f;
flow+=f;
a-=f;
if(a==0) break;
}
}
return flow;
}
int Maxflow(int s,int t)
{
this->s=s; this->t=t;
int flow=0;
while(bfs())
{
MEM(cur,0);
flow+=dfs(s,INF);
}
return flow;
}
// int judge(int u)
// {
// if(vis[u]) return 1;
// vis[u]=1;
// for(int i=0;i<G[u].size();i++)
// {
// int x=G[u][i];
// Edge e=edges[x];
// Edge ee=edges[x^1];
// int vv=e.to,capp=e.cap,floww=e.flow;
// int flow2=ee.flow;
// int qqq=capp-flow2;
// int ppp=capp-floww;
// if(vv!=s&&vv!=t&&vv!=u&&qqq)
// if(judge(vv)) return 1;
//
// }
// vis[u]=0;
// return 0;
// }
}Dic;
int a[450],b[450];
int suma,sumb;
int res[450][450];
int pp[450][450];
int if_circle(int n,int m,int k)
{
MEM(res,0);
for(int i=1;i<=n;i++)
{
for(int j=0;j<Dic.G[i].size();j++)
{
int x=Dic.G[i][j];
Edge &e=Dic.edges[x];
int v=e.to;
// cout<<"cap "<<cap<<endl;
if(v<=(n+m)&&v>n)
res[i][v-n]=e.flow;
}
}
MEM(pp,0);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
for(int kk=j+1;kk<=m;kk++)
{
bool v1=0,v2=0;
if(res[i][j]!=k&&res[i][kk])
{
if(pp[kk][j])
return 1;
v1=1;
}
if(res[i][j]&&res[i][kk]!=k)
{
if(pp[j][kk])
return 1;
v2=1;
}
if(v1) pp[j][kk]=1;
if(v2) pp[kk][j]=1;
}
}
}
return 0;
}
int main()
{
// fread;
int n,m,k;
while(scanf("%d%d%d",&n,&m,&k)!=EOF)
{
suma=sumb=0;
int s=0,t=n+m+1;
Dic.init(t);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
Dic.addedge(s,i,a[i]);
suma+=a[i];
}
for(int i=1;i<=m;i++)
{
scanf("%d",&b[i]);
Dic.addedge(i+n,t,b[i]);
sumb+=b[i];
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
Dic.addedge(i,j+n,k);
int ans=Dic.Maxflow(s,t);
// MEM(vis,0);
if(ans==suma&&ans==sumb)
{
// int flag=0;
// for(int i=1;i<=n;i++)
// {
// if(Dic.judge(i))
// {
// flag=1;
// break;
// }
// }
if(if_circle(n,m,k))
puts("Not Unique");
else
{
puts("Unique");
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(j>1) printf(" ");
printf("%d",res[i][j]);
}
printf("\n");
}
}
}
else
{
puts("Impossible");
}
}
return 0;
}