Matrix 网络流

You're given a matrix with n rows and n columns. A basic property of a square matrix is the main diagonal: all cells that have the same row and column number.

We consider all diagonals that are parallel to the main one. We consider them from left-low corner to the right-upper one. So, the first cell of each diagonal will be, in order: (n, 1) (n - 1, 1) ... (1, 1) (1, 2) ... (1, n).

You need to choose one number from each diagonal. More, all 2*n-1 numbers must be pairwise distinct.

Input

The first line contains number n (1 ≤ n ≤ 300). Next n lines contain n numbers, representing the elements of the matrix. All elements of the matrix are between 1 and 109.


Output

If there is no solution, output "NO". Otherwise, output "YES". Next, on the same line, output 2n-1 numbers, separated by spaces. Each number represents the chosen value from a diagonal. Diagonals are considered in the order given by the problem.

Example
Input
2
1 1
1 1
Output
NO


题目大意:给出一个矩阵,把矩阵元素按照平行于与主对角线的斜线分成2*n-1组,求是否能在每一组里面取一个数使得每两个数都不相同,如果存在输出一种方案。

把每一斜线当做一个点,把每一矩阵元素的值也当做点,如果一个值在某一条斜边上出现过,则从代表斜边的点向代表矩阵元素值的点联一条流量为1的边;然后从源向所以代表斜边的点连一条流量为1的边,从代表元素值的点向汇连一条流量为1的边。这样就限制了每一条斜边只能取一个矩阵元素值,每个矩阵元素值只能被选一次,如果这样情况下的最大流是2*n-1的话,就说明存在一个方案满足题意要求。输出方案的话就扫描以斜边点为起点有哪些边的正向流向剩余为0,则表示这条边的终点代表的矩阵元素值已经被选了,记录下来输出。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#define ll long long
#define maxn 500000
#define maxm 1000000
#define inf 2147483647
using namespace std;
inline void read(int &x){
    char ch;
    bool flag=false;
    for (ch=getchar();!isdigit(ch);ch=getchar())if (ch=='-') flag=true;
    for (x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar());
    x=flag?-x:x;
}
inline void write(int x){
    static const int maxlen=100;
    static char s[maxlen];
        if (x<0) {   putchar('-'); x=-x;}
    if(!x){ putchar('0'); return; }
    int len=0; for(;x;x/=10) s[len++]=x % 10+'0';
    for(int i=len-1;i>=0;--i) putchar(s[i]);
}
int deep[maxn],now[maxn];
int v[maxm],st[maxm],ed[maxm],pre[maxm],tot;

void build(int a,int b,int c){
pre[++tot]=now[a];
now[a]=tot;
st[tot]=a;
ed[tot]=b;
v[tot]=c;
}

void add_edge(int a,int b,int c){
build(a,b,c);
build(b,a,0);
}

int d[maxn],t,w;
int ST,ED;

bool bfs(){
memset(deep,-1,sizeof(deep));
d[1]=ST;t=1;w=1;deep[ST]=0;
while (t<=w)
    {
        int x=d[t];
        for (int p=now[x];p;p=pre[p])
            if (v[p])
            if (deep[ed[p]]<0)
            {
                int y=ed[p];
                deep[y]=deep[x]+1;
                d[++w]=y;
                if (y==ED)
                    return 1;
            }
        t++;
    }
return 0;
}

int dfs(int x,int Min){
if (x==ED) return Min;
int ans=0;
for (int p=now[x];p;p=pre[p])
{
    if (v[p] && deep[ed[p]]>deep[x])
        {
            int tmp=dfs(ed[p],min(Min,v[p]));
            v[p]-=tmp; v[p^1]+=tmp;
            ans+=tmp;  Min-=tmp;
            if (!Min) return ans;
        }
}
    deep[x]=-1; return ans;
}


int n;
int a[400][400];

map <int ,int> M;
map <int ,int> _M;
set <int >S;
int cnt=0;

int get_num(int x){
if (S.count(x)==0)
    {
        cnt++;
        S.insert(x);
        M.insert( make_pair(x,cnt ) );
        _M.insert( make_pair(cnt,x ) );
    }
return M.at( x );
}

int main(){
    read(n);
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
            read(a[i][j]);
    ST=1;
    tot=1;
    cnt=2*n;
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
            add_edge(j-i+n+1,get_num( a[i][j]) ,1 );
    for (int i=2;i<=2*n;i++)
        add_edge( ST, i , 1);
    ED=2*n+cnt+1;
    for (int i=2*n+1;i<=2*n+cnt;i++)
        add_edge( i , ED ,1 );
    int ans=0;
    while (bfs())
        ans+=dfs(ST,inf);
    if (ans!=2*n-1)
        puts("NO");
    else
        {
            puts("YES");
            for (int i=2;i<2*n;i++)
                {
                    for (int p=now[i];p;p=pre[p])
                        if (v[p]==0)
                        {
                            printf("%d ",_M.at( ed[p] ) );
                            break;
                        }
                }
                for (int p=now[2*n];p;p=pre[p])
                    if (v[p]==0)
                        {
                            printf("%d\n",_M.at( ed[p] ) );
                            break;
                        }
        }
return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值