您的位置:首页 > 其它

uoj 41 【清华集训2014】矩阵变换 婚姻稳定问题

2015-05-13 23:12 417 查看

【清华集训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

[b]题意 [/b]

[b]题解:
[/b]

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

[b]代码:[/b]

//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("\n");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: