HDOJ 4888 Redraw Beautiful Drawings

最大流判断多解

建图:

源点连接到每一个代表行的节点容量为行总和,每一个代表列的节点连接到汇点容量为列总和,行和列之间互相连接容量为Limit

多解:

做一遍ISAP后,在残量图上DFS看能否找到点数大于2的环即可

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

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

Author

Fudan University

Source

2014 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>=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;
}
时间: 2024-11-03 22:36:36

HDOJ 4888 Redraw Beautiful Drawings的相关文章

hdoj 4888 Redraw Beautiful Drawings 【最大流满流+唯一性判断】

题目:hdoj 4888 Redraw Beautiful Drawings 分类:最大流满流 , 最大流唯一性 来源:2014 Multi-University Training Contest 3 题意:一个矩阵的每行每列的和都知道,然后让你求能不能填,是否唯一,唯一的话输出解. 分析:这个题目能看出来是最大流,但是难点有2. 首先:题中矩阵400 * 400 ,好在题目中矩阵和没有特殊要求,所以可以直接以行列建图,建图方案: 1. 源点 -> 每一行对应的点,流量限制为该行的和 2. 每一

HDOJ 4888 Redraw Beautiful Drawings &amp;&amp; HDOJ 4975 A simple Gaussian elimination problem

解题思路: 这两道题题目大致相同,都是已知一个矩阵每一行的和和每一列的和,并且每个点的数小于K  还原原矩阵并判断答案是否唯一.建图方式相同,新建一个原点S 和一个汇点T ,S到行连边,容量为该行之和,列到T连边,容量为该列之和, 对于每一个点 i 和 j ,i 行向 j 列连边 , 容量为K , 求一遍最大流.并且通过判断是否存在环来判断是否唯一. 区别在于 第二道题N 与 M 均扩大,找环需要采用更高效的算法,这里采用Tarjan 算法来找环. HDOJ 4888 #include <ios

HDU 4888 Redraw Beautiful Drawings 网络流 建图

题意: 给定n, m, k 下面n个整数 a[n] 下面m个整数 b[n] 用数字[0,k]构造一个n*m的矩阵 若有唯一解则输出这个矩阵,若有多解输出Not Unique,若无解输出Impossible 思路:网络流,,, n行当成n个点,m列当成m个点 从行-列连一条流量为k的边,然后源点-行连一条a[i]的边, 列-汇点 流量为b[i] 瞎了,该退役了 T^T #include<stdio.h> #include<string.h> #include<iostream&

HDU 4888 Redraw Beautiful Drawings(2014 Multi-University Training Contest 3)

题意:给定n*m个格子,每个格子能填0-k 的整数.然后给出每列之和和每行之和,问有没有解,有的话是不是唯一解,是唯一解输出方案. 思路:网络流,一共 n+m+2个点   源点 到行连流量为 所给的 当前行之和.    每行 连到每一列 一条流量为  k的边,每列到汇点连 列和.如果流量等于总和则有解,反之无解(如果列总和不等于行总和也无解).  判断方案是否唯一 找残留网络是否存在长度大于2的环即可,有环说明不唯一. #include<cstdio> #include<cstring&

HDU 4888 Redraw Beautiful Drawings(最大流+判最大流网络是否唯一)

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

【HDU】4888 Redraw Beautiful Drawings 网络流【推断解是否唯一】

传送门:pid=4888">[HDU]4888 Redraw Beautiful Drawings 题目分析: 比赛的时候看出是个网络流,可是没有敲出来.各种反面样例推倒自己(究其原因是不愿意写暴力推断的).. 首先是简单的行列建边.源点向行建边.容量为该行元素和,汇点和列建边.容量为该列元素和.全部的行向全部的列建边,容量为K. 跑一次最大流.满流则有解,否则无解. 接下来是推断解是否唯一. 这个题解压根没看懂.还是暴力大法好. 最简单的思想就是枚举在一个矩形的四个端点.设A.D为主对角

HDU 4888 Redraw Beautiful Drawings (2014-多校3-1002,最大流,判最大流有多解)

题目: http://acm.hdu.edu.cn/showproblem.php?pid=4888 题意: 给一个n*m的矩阵的n行之和和m列之和以及限制k,使用0-k的数字填充矩阵使得其行与列之和为给定值 如果不行则输出Impossible 如果有多解则输出Not Unique 如果有一解则输出Unique,并输出构造的矩阵 方法: 最大流,判最大流有多解 1.建图: 每一行一个点,每一列一个点 源点与第i个行点连边,权值为第i行之和 第j个列点与汇点连边,权值为第j行之和 第i个行点与第j

hdu 4888 Redraw Beautiful Drawings(最大流,判环)

http://acm.hdu.edu.cn/showproblem.php?pid=4888 加入一个源点与汇点,建图例如以下: 1. 源点 -> 每一行相应的点,流量限制为该行的和 2. 每一行相应的点 -> 每一列相应的点,流量限制为 K 3. 每一列相应的点 -> 汇点,流量限制为该列的和 求一遍最大流,若最大流与矩阵之和相等,说明有解,否则无解.推断唯一解,是推断残量网络中是否存在一个长度大于2的环.若存在说明有多解,否则有唯一解,解就是每条边行i->列j的流量. #inc

hdu - 4888 - Redraw Beautiful Drawings(最大流)

题意:给一个N行M列的数字矩阵的行和以及列和,每个元素的大小不超过K,问这样的矩阵是否存在,是否唯一,唯一则求出各个元素N(1 ≤ N ≤ 400) , M(1 ≤ M ≤ 400), K(1 ≤ K ≤ 40). 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4888 -->>建图: 1)超级源S = 0,超级汇T = N + M + 1: 2)S到每个行和各连一条边,容量为该行行和: 3)每个行和到每个列和各连一条边,容量为K: 4)每个列和