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
 


#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;
}



HDOJ 4888 Redraw Beautiful Drawings

上一篇:谷歌眼镜--UI指南


下一篇:Xcode出现may cause a leak非忽略的解决方法