HDU 4888

HDU 4888

大家好,又见面了,我是全栈君,祝每个程序员都可以多学几门语言。

Redraw Beautiful Drawings

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1993    Accepted Submission(s): 446

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

已知一个矩阵,n行m列。已知每一行,每一列的和,推断是否存在这种矩阵,而且推断是否唯一。

非常裸的最大流模型,前面的部分非常水,关键是,最大流判环部分有点纠结。

代码:

/* ***********************************************
Author :rabbit
Created Time :2014/8/4 18:18:37
File Name :11.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
const int maxn=20100;
const int maxm=1002000;
struct Edge{
	int next,to,cap;
	Edge(){};
	Edge(int _next,int _to,int _cap){
		next=_next;to=_to;cap=_cap;
	}
}edge[maxm];
int head[maxn],tol,dep[maxn],gap[maxn];
void addedge(int u,int v,int flow){
    edge[tol]=Edge(head[u],v,flow);head[u]=tol++;
    edge[tol]=Edge(head[v],u,0);head[v]=tol++;
}
void bfs(int start,int end){
    memset(dep,-1,sizeof(dep));
    memset(gap,0,sizeof(gap));
    gap[0]++;int front=0,rear=0,Q[maxn];
    dep[end]=0;Q[rear++]=end;
    while(front!=rear){
        int u=Q[front++];
        for(int i=head[u];i!=-1;i=edge[i].next){
            int v=edge[i].to;if(dep[v]==-1)
                Q[rear++]=v,dep[v]=dep[u]+1,gap[dep[v]]++;
        }
    }
}
int cur[maxn],S[maxn];
int sap(int s,int t,int N){
	int res=0;bfs(s,t);
	int top=0,u=s,i;
	memcpy(cur,head,sizeof(head));
	while(dep[s]<N){
		if(u==t){
			int temp=INF,id;
		    for( i=0;i<top;i++)
			   if(temp>edge[S[i]].cap)
				   temp=edge[S[i]].cap,id=i;
		    for( i=0;i<top;i++)
			      edge[S[i]].cap-=temp,edge[S[i]^1].cap+=temp;
		    res+=temp;top=id;u=edge[S[top]^1].to;
		}
		if(u!=t&&gap[dep[u]-1]==0)break;
		for( i=cur[u];i!=-1;i=edge[i].next)
			if(edge[i].cap&&dep[u]==dep[edge[i].to]+1)break;
		if(i!=-1)cur[u]=i,S[top++]=i,u=edge[i].to;
		else{
			int MIN=N;
			for( i=head[u];i!=-1;i=edge[i].next)
				if(edge[i].cap&&MIN>dep[edge[i].to])
					MIN=dep[edge[i].to],cur[u]=i;
			--gap[dep[u]];++gap[dep[u]=MIN+1];
			if(u!=s)u=edge[S[--top]^1].to;
		}
	}
	return res;
}
int hang[500],lie[500];
int flow[500][500],vis[maxn],m,n,k;
bool dfs(int u,int fa){
	if(vis[u])return 1;
	vis[u]=1;
	for(int i=head[u];i!=-1;i=edge[i].next){
		int v=edge[i].to;
		if(v==fa||v==0||v==n+m+1||edge[i].cap==0)continue;
		if(dfs(v,u))return 1;
	}
	vis[u]=0;
	return 0;
}
int main()
{
     freopen("1002.in","r",stdin);
     freopen("data.out","w",stdout);
	 while(~scanf("%d%d%d",&n,&m,&k)){
		 memset(head,-1,sizeof(head));tol=0;
		 int L=0,R=0;
		 for(int i=1;i<=n;i++)scanf("%d",&hang[i]),L+=hang[i];
		 for(int i=1;i<=m;i++)scanf("%d",&lie[i]),R+=lie[i];
		 if(L!=R){
			 puts("Impossible");continue;
		 }
		 for(int i=1;i<=n;i++)
			 addedge(0,i,hang[i]);
		 for(int i=1;i<=m;i++)
			 addedge(i+n,n+m+1,lie[i]);
		 for(int i=1;i<=n;i++)
			 for(int j=n+1;j<=n+m;j++)
				 addedge(i,j,k);
		 int ans=sap(0,n+m+1,m+n+100);
		 if(ans!=L){
			 puts("Impossible");continue;
		 }
		 int flag=0;
		 memset(vis,0,sizeof(vis));
		 for(int i=1;i<=n;i++)
			 if(dfs(i,-1)){
				 flag=1;break;
			 }
		 if(flag)puts("Not Unique");
		 else {
			 puts("Unique");
			 for(int i=1;i<=n;i++)
				 for(int j=head[i];j!=-1;j=edge[j].next){
					 int v=edge[j].to;
					 if(v>n&&v<=n+m)
						 flow[i][v-n]=k-edge[j].cap;
				 }
			 for(int i=1;i<=n;i++){
				 printf("%d",flow[i][1]);
				 for(int j=2;j<=m;j++)
					 printf(" %d",flow[i][j]);
				 puts("");
			 }
		 }
	 }
     return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/118593.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 微信朋友圈如何自动点赞

    微信朋友圈如何自动点赞我一直都不太喜欢给别人点赞,某一年(貌似是17年)微信出了一次朋友圈年报,那一整年我就点出去了几个赞,要知道当时我微信好友应该有300+。我觉得这是我不喜欢参与社交活动在网络世界的一种体现吧。不给别人点赞也没啥坏处,但你不评不赞,难免会让你和好友之间有些疏远,给别人点赞吧我又嫌麻烦,于是一直想着做个自动点赞的东西,今天基本实现了,虽然诸多不完整和诸多限制,但还是决定分享出来,主要是我觉得还挺好玩的。Autojs先来介绍下Autojs,看githubid貌似是个95后,真是年轻有为了。我这个朋…

  • Project interpreter not specified(eclipse+pydev)

    Project interpreter not specified(eclipse+pydev)

  • java内部类和静态内部类区别_静态内部类对象的创建

    java内部类和静态内部类区别_静态内部类对象的创建在阅读JavaCollectionFramwork时,LinkedList中有个静态内部类:privatestaticclassNode&lt;E&gt;{Eitem;Node&lt;E&gt;next;Node&lt;E&gt;prev;Node(Node&lt;E&gt;prev,Eelem…

    2022年10月11日
  • log4j2 debug_log4j原理

    log4j2 debug_log4j原理长话短说吧。相信大家已经被Log4j2的重大漏洞刷屏了,估计有不少小伙伴此前为了修bug已经累趴下了。很不幸,我的小老弟小二的SpringBoot项目中恰好用的就是Log4j2,版本特喵的还是2.14.1,在这次漏洞波及的版本范围之内。第一时间从网上得知这个漏洞的消息后,小二吓尿了。赶紧跑过来问老王怎么解决。老王先是给小二提供了一些临时性的建议,比如说:JVM参数添加-Dlog4j2.formatMsgNoLookups=truelog4j2.formatMsgNoLooku

    2022年10月29日
  • beanutils.copyproperties属性值丢失_beanutils.populate用法

    beanutils.copyproperties属性值丢失_beanutils.populate用法问题场景例如有个对象要提交,提交一次,第二次提交我们希望是对上次提交的完善。。那么用其他方式实现很麻烦,本身的BeanUtils.copyProperties也是不大支持。解决方案hutool开源库为我们提供了更为强大的Bean工具-BeanUtil,只需要一句代码就搞定BeanUtil.copyProperties(oldDetail.get(),userDetail,true,Copy…

  • 更新pycharm版本_pycharm怎么更改python环境

    更新pycharm版本_pycharm怎么更改python环境如下图,记录要勾选Makeavaliabletoallprojects,然后查看是否加载出所有的第三方插件,会遇到插件安装后,运行还是找不到插件的问题,就是这里选择的不对,Pycharm总是会自己修改Baseinterperter路径,安装好后,在这里修改路径,看到有加载出来就可以了…

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号