# CodeForces 274D Lovely Matrix：“冗余点” 建边 + 拓扑

2018 年 8 月 6 日 10:41

## Problem

Lenny had an n × m matrix of positive integers. He loved the matrix so much, because each row of the matrix was sorted in non-decreasing order. For the same reason he calls such matrices of integers lovely.

One day when Lenny was at school his little brother was playing with Lenny's matrix in his room. He erased some of the entries of the matrix and changed the order of some of its columns. When Lenny got back home he was very upset. Now Lenny wants to recover his matrix.

Help him to find an order for the columns of the matrix so that it's possible to fill in the erased entries of the matrix to achieve a lovely matrix again. Note, that you can fill the erased entries of the matrix with any integers.

## Input

The first line of the input contains two positive integers n and m $(1 ≤ n·m ≤ 10^5)$. Each of the next n lines contains m space-separated integers representing the matrix. An integer -1 shows an erased entry of the matrix. All other integers (each of them is between $0$ and $10^9$ inclusive) represent filled entries.

## Output

If there exists no possible reordering of the columns print -1. Otherwise the output should contain m integers $p_1, p_2, ..., p_m$ showing the sought permutation of columns. So, the first column of the lovely matrix will be p1-th column of the initial matrix, the second column of the lovely matrix will be $p_2$-th column of the initial matrix and so on.

## Examples

### Input #1

3 3
1 -1 -1
1 2 1
2 -1 1


### Output #1

3 1 2


### Input #2

2 3
1 2 2
2 5 4


### Output #2

1 3 2


### Input #3

2 3
1 2 3
3 2 1


### Output #3

-1


## Analysis

inline void Build(int k){ // K 代表当前行，rcd[].x 存储了已经排好序的每行数据
for (int i=1;i<=m;i++) ans[i].id=i;
int s=-1;bool fst=true; // fst 标记最小的元素，由于没有比其更小的元素，所以它之前不需要增加冗余点。最大元素也类似。
while (rcd[k][s+1].x==-1) s++; // 对于-1可以忽略，不加入拓扑排序
for (int i=s+1;i<m;){ // 排序去重的做法
int j=i;cnt++;
while (rcd[k][j+1].x==rcd[k][i].x&&j+1<m){
j++;
}
fst=false;
if (j+1<m) for (int t=i;t<=j;t++) add(rcd[k][t].id,cnt+m);
i=j+1;
}
}


## Code

#include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn=200005,maxe=400005;
int n,m,cnt=0;
int tot=0,lnk[maxn],nxt[maxe],son[maxe],ind[maxe];
struct WT{
int x,id;
}ans[maxn];
vector<int> a[maxn];
vector<WT> rcd[maxn];
queue<int> que;

int ret=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9') ret=ret*10+ch-'0',ch=getchar();
return ret*f;
}
x++;y++;
tot++;ind[y]++;son[tot]=y;nxt[tot]=lnk[x];lnk[x]=tot;
//printf("Add an edge: %d  -> %d\n",x,y);
}
inline bool cmp(WT aa,WT bb){
return aa.x<bb.x;
}
inline void Build(int k){
for (int i=1;i<=m;i++) ans[i].id=i;
int s=-1;bool fst=true;
while (rcd[k][s+1].x==-1) s++;
for (int i=s+1;i<m;){
int j=i;cnt++;
while (rcd[k][j+1].x==rcd[k][i].x&&j+1<m){
j++;
}
fst=false;
if (j+1<m) for (int t=i;t<=j;t++) add(rcd[k][t].id,cnt+m);
i=j+1;
}
}
inline void Topology(){
for (int i=1;i<=m;i++) if (ind[i]==0) ans[i].x=1,que.push(i);
while (!que.empty()){
int x=que.front();que.pop();
for (int i=lnk[x];i;i=nxt[i]){
ind[son[i]]--;
ans[son[i]].x=max(ans[son[i]].x,ans[x].x+bool(son[i]<=m));
if (ind[son[i]]==0) que.push(son[i]);
}
}
}
int main(){
for (int i=1;i<=n;i++){
for (int j=0;j<m;j++){
a[i].push_back(now);
rcd[i].push_back((WT){now,j});
}
sort(rcd[i].begin(),rcd[i].end(),cmp);
}
for (int i=1;i<=n;i++) Build(i);
Topology();
sort(ans+1,ans+1+m,cmp);
if (ans[1].x==0){printf("-1\n");return 0;}
for (int i=1;i<=m;i++) printf("%d ",ans[i].id);
printf("\n");
return 0;
}