[清华集训2015 Day2T3][bzoj 3816]矩阵变换

稳定婚姻匹配问题
行为男,数为女
行喜欢靠前的数,数所在位置靠后的行

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,m;
#define Maxn 205
#define Maxm 405
int a[Maxn][Maxm],st[Maxn],num[Maxn],b[Maxn];
int at[Maxn][Maxn];
int sta[Maxn],top;
inline void rd(int &x){
    x=0;char ch=getchar();
    while(ch<'0'||ch>'9')ch=getchar();
    while(ch>='0'&&ch<='9'){
        x=x*10+ch-'0';
        ch=getchar();
    }
}
int main(){
    int T;
    rd(T);
    while(T--){
        rd(n);rd(m);
        for(register int i=1;i<=n;++i)
            for(register int j=1;j<=m;++j){
                rd(a[i][j]);
                at[i][a[i][j]]=j;
            }
        fill(st+1,st+n+1,1);
        for(int i=1;i<=n;++i)sta[i]=i;
        int top=n;
        memset(num,0,sizeof(num));
        memset(b,0,sizeof(b));
        while(top){
            int x=sta[top];
            for(int &i=st[x];i<=m;++i)
                if(a[x][i]){
                    int to=a[x][i];
                    if(!num[to]||at[num[to]][to]<i){
                        if(num[to])b[num[to]]=0,sta[top]=num[to];
                        else --top;
                        num[to]=x;b[x]=to;
                        ++i;
                        break;
                    }
                }
        }
        for(register int i=1;i<=n;++i)printf("%d ",b[i]);
        puts("");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值