HDU Redraw Beautiful Drawings 推断最大流是否唯一解

点击打开链接

Redraw Beautiful Drawings

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1660    Accepted Submission(s): 357

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

给你一个n*m的矩阵,然后个格子里面有一个小于k的数,而且告诉你每行和每列的和,让你求是否存在解,并推断是否唯一。

建图:源点和每行连边,容量为每行的和。每列和汇点连边,容量为每列的和。每行和每列连边,容量为k。

推断是否存在唯一解,在残留网络里面是否能找到一个环。

//515MS	7304K
#include<stdio.h>
#include<string.h>
#define M 1007
int s,t,n,m,k,sum1,sum2;
int row[M],col[M],vis[M],g[M][M];
const int MAXN=20010;//点数的最大值
const int MAXM=880010;//边数的最大值
const int INF=0x3f3f3f3f;
struct Node
{
    int from,to,next;
    int cap;
}edge[MAXM];
int tol;
int head[MAXN];
int dis[MAXN];
int gap[MAXN];//gap[x]=y :说明残留网络中dis[i]==x的个数为y
void init()
{
    tol=0;
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w)
{
    edge[tol].from=u;
    edge[tol].to=v;
    edge[tol].cap=w;
    edge[tol].next=head[u];
    head[u]=tol++;
    edge[tol].from=v;
    edge[tol].to=u;
    edge[tol].cap=0;
    edge[tol].next=head[v];
    head[v]=tol++;
}
void BFS(int start,int end)
{
    memset(dis,-1,sizeof(dis));
    memset(gap,0,sizeof(gap));
    gap[0]=1;
    int que[MAXN];
    int front,rear;
    front=rear=0;
    dis[end]=0;
    que[rear++]=end;
    while(front!=rear)
    {
        int u=que[front++];
        if(front==MAXN)front=0;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if(dis[v]!=-1)continue;
            que[rear++]=v;
            if(rear==MAXN)rear=0;
            dis[v]=dis[u]+1;
            ++gap[dis[v]];
        }
    }
}
int SAP(int start,int end)
{
    int res=0,nn=end+1;
    BFS(start,end);
    int cur[MAXN];
    int S[MAXN];
    int top=0;
    memcpy(cur,head,sizeof(head));
    int u=start;
    int i;
    while(dis[start]<nn)
    {
        if(u==end)
        {
            int temp=INF;
            int inser;
            for(i=0;i<top;i++)
               if(temp>edge[S[i]].cap)
               {
                   temp=edge[S[i]].cap;
                   inser=i;
               }
            for(i=0;i<top;i++)
            {
                edge[S[i]].cap-=temp;
                edge[S[i]^1].cap+=temp;
            }
            res+=temp;
            top=inser;
            u=edge[S[top]].from;
        }
        if(u!=end&&gap[dis[u]-1]==0)//出现断层,无增广路
          break;
        for(i=cur[u];i!=-1;i=edge[i].next)
           if(edge[i].cap!=0&&dis[u]==dis[edge[i].to]+1)
             break;
        if(i!=-1)
        {
            cur[u]=i;
            S[top++]=i;
            u=edge[i].to;
        }
        else
        {
            int min=nn;
            for(i=head[u];i!=-1;i=edge[i].next)
            {
                if(edge[i].cap==0)continue;
                if(min>dis[edge[i].to])
                {
                    min=dis[edge[i].to];
                    cur[u]=i;
                }
            }
            --gap[dis[u]];
            dis[u]=min+1;
            ++gap[dis[u]];
            if(u!=start)u=edge[S[--top]].from;
        }
    }
    return res;
}

void build()
{
    sum1=0,sum2=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&row[i]);
        sum1+=row[i];
        addedge(s,i,row[i]);
    }
    for(int i=1;i<=m;i++)
    {
         scanf("%d",&col[i]);
         sum2+=col[i];
         addedge(n+i,t,col[i]);
    }
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            addedge(i,n+j,k);
}

bool iscycle(int u,int fa)
{
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        if(i==(fa^1))continue;
        if(edge[i].cap)
        {
            if(vis[edge[i].to])return true;
            vis[edge[i].to]=true;
            if(iscycle(edge[i].to,i))return true;
            vis[edge[i].to]=false;
        }
    }
    return false;
}
void solve()
{
    if(sum1!=sum2){printf("Impossible\n");return;}
    int anss=SAP(s,t);
    //printf("anss=%d\n",anss);
    if(anss!=sum1){printf("Impossible\n");return;}
    memset(vis,false,sizeof(vis));
    int flag=0;
    for(int i=1;i<=n;i++)//推断残留网络是否存在环
        if(iscycle(i,-1))
            {flag=1;break;}
    if(flag)printf("Not Unique\n");
    else
    {
        printf("Unique\n");
        for(int u=1;u<=n;u++)
            for(int i=head[u];i!=-1;i=edge[i].next)
            {
                int v=edge[i].to;
                if(v>n&&v<=n+m)
                    g[u][v-n]=k-edge[i].cap;
            }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<m;j++)
                printf("%d ",g[i][j]);
            printf("%d\n",g[i][m]);
        }
    }
}
int main()
{
    while(scanf("%d%d%d",&n,&m,&k)!=EOF)
    {
        s=0,t=n+m+1;
        init();
        build();
        solve();
    }
}

时间: 2024-10-27 06:43:11

HDU Redraw Beautiful Drawings 推断最大流是否唯一解的相关文章

HDU Redraw Beautiful Drawings 判断最大流是否唯一解

点击打开链接 Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1660    Accepted Submission(s): 357 Problem Description Alice and Bob are playing together. Alice is crazy about

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

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

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

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)每个列和

HDU4888 Redraw Beautiful Drawings(最大流唯一性判定:残量网络删边判环)

题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=4888 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.

hdu4888 Redraw Beautiful Drawings(最大流)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4888 题意:给一个矩阵没行的和和每列的和,问能否还原矩阵,如果可以还原解是否唯一,若唯一输出该矩阵. 思路:设一个源点和汇点,每行的和和源点加边,权值为该行的和,每列的和和汇点加点,权值为该列的和. 每行和每列加边, 权值为k,跑最大流,如果满流(从源点流出的等于流入汇点的)则证明可以还原该矩阵.   判断是否有多节,就dfs搜,看残余网络中是否有环,无环则解唯一. AC代码: #include<i

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

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

HDU 4888 (杭电多校#3)Redraw Beautiful Drawings(网络流之最大流)

题目地址:HDU 4888 自己之所以弱真心是态度的问题,以后不能再偷懒了!!那次这个题一直没补,结果这次又遇到了..还有这次遇到的最小割权闭合问题,也一直没刷,所以这次遇到了也不会,连是最小割都不知道!!(突然想起来前面还有好多题拖到现在也没做...T U T)以后绝不能再拖拉了! 这题的建图是很容易的,主要是判断唯一性不好判断.这里是用的dfs找环来判断是否唯一,因为假如有环的话,说明环 中的数字是可以相互流动而且可以保证解依然正确.. 代码如下: #include <cstdio> #i

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