zoukankan      html  css  js  c++  java
  • uoj 41 【清华集训2014】矩阵变换 婚姻稳定问题

    【清华集训2014】矩阵变换

    Time Limit: 20 Sec  Memory Limit: 256 MB

    题目连接

    http://uoj.ac/problem/41

    Description

    给出一个 N 行 M 列的矩阵A, 保证满足以下性质:

        M>N。
        矩阵中每个数都是 [0,N] 中的自然数。
        每行中, [1,N] 中每个自然数都恰好出现一次。这意味着每行中 0 恰好出现 M−N 次。
        每列中,[1,N] 中每个自然数至多出现一次。

    现在我们要在每行中选取一个非零数,并把这个数之后的数赋值为这个数。我们希望保持上面的性质4,即每列中,[1,N] 中每个自然数仍然至多出现一次。

    Input

    第一行一个正整数 T,表示数据组数。

    后面包含 T 组数据,各组数据之间无空行。每组数据以两个正整数 N,M 开始,接下来 N 行,每行 M 个用空格隔开的整数,意义如题所述。

    Output

    对于每组数据输出一行。如果有解,则输出 N 个整数,依次表示每一行取的数是多少。(这应该是一个 1 到 N 的排列)如果无解,则输出任意卖萌表情。

    Sample Input

    2
    5 10
    0 1 0 2 3 0 0 4 0 5
    2 0 3 0 0 1 0 5 4 0
    4 2 1 0 0 0 3 0 5 0
    0 3 0 4 0 5 0 1 2 0
    1 0 0 3 2 4 5 0 0 0
    5 10
    0 1 0 2 3 0 0 4 0 5
    2 0 3 0 0 1 0 5 4 0
    4 2 1 0 0 0 3 0 5 0
    0 3 0 4 0 5 0 1 2 0
    1 0 0 3 2 4 5 0 0 0

    Sample Output

    4 5 3 1 2
    5 4 3 1 2

    HINT

    题意

    题解:

    行是男孩, 数是女孩.
    一行喜欢在这行靠前的数.
    数喜欢她出现的靠后的行.
    然后跑婚姻稳定问题就好啦~

    代码:

    //qscqesze
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    #include <map>
    #include <stack>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define maxn 200001
    #define mod 10007
    #define eps 1e-9
    int Num;
    char CH[20];
    //const int inf=0x7fffffff;   //нчоч╢С
    const int inf=0x3f3f3f3f;
    /*
    
    inline void P(int x)
    {
        Num=0;if(!x){putchar('0');puts("");return;}
        while(x>0)CH[++Num]=x%10,x/=10;
        while(Num)putchar(CH[Num--]+48);
        puts("");
    }
    */
    inline ll read()
    {
        int x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    inline void P(int x)
    {
        Num=0;if(!x){putchar('0');puts("");return;}
        while(x>0)CH[++Num]=x%10,x/=10;
        while(Num)putchar(CH[Num--]+48);
        puts("");
    }
    //**************************************************************************************
    
    int a[500][500];
    int pa[500][500];
    int pc[500];
    int qc[500];
    int main()
    {
        int t=read();
        while(t--)
        {
            memset(a,0,sizeof(a));
            memset(pa,0,sizeof(pa));
            memset(pc,0,sizeof(pc));
            memset(qc,0,sizeof(qc));
            int n=read(),m=read();
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=m;j++)
                {
                    a[i][j]=read();
                    if(a[i][j])
                        pa[i][a[i][j]]=j;
                }
            }
            for(int i=1;i<=n;i++)
            {
                int p=i;
                while(1)
                {
                    while(!a[p][pc[p]])
                        pc[p]++;
                    int v=a[p][pc[p]];
                    if(!qc[v])
                    {
                        qc[v]=p;
                        break;
                    }
                    int q=qc[v];
                    if(pa[p][v]>pa[q][v])
                        pc[q]++,qc[v]=p,p=q;
                    else
                        pc[p]++;
                }
            }
            for(int i=1;i<=n;i++)
                printf("%d ",a[i][pc[i]]);
            printf("
    ");
        }
    }
  • 相关阅读:
    P2660 zzc 种田
    ie发[e]的单词,ea发[e]的单词,e发i:的单词
    从员工表和部门表联合查询的不同方式看CBO对不同SQL的优化
    Delete..In.. 删除语句的优化再次探讨
    【Vue】第一个Vue例子
    再度思索:从配送表中选出订单号和配送者相同时的最新记录
    datx 编译打包命令
    kube-proxy iptables 模式源码分析
    kube-proxy ipvs 模式源码分析
    go json 反解析接口
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4502049.html
Copyright © 2011-2022 走看看