CF274D
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.
The first line of the input contains two positive integers n and m (1 ≤ n·m ≤ 105). 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 109inclusive) represent filled entries.
If there exists no possible reordering of the columns print -1. Otherwise the output should contain m integers p1, p2, ..., pm 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 p2-th column of the initial matrix and so on.
3 3
1 -1 -1
1 2 1
2 -1 1
3 1 2
2 3
1 2 2
2 5 4
1 3 2
2 3
1 2 3
3 2 1
-1 加点 拓扑排序
//Serene
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn=2e5+10;
int n,m,tot; struct Node{
int pos,x;
bool operator <(const Node& b) const{return x<b.x;}
}node[maxn]; int aa,ff;char cc;
int read() {
aa=0;cc=getchar();
while((cc<'0'||cc>'9')&&cc!='-') cc=getchar();
if(cc=='-') ff=-1,cc=getchar(); else ff=1;
while(cc>='0'&&cc<='9') aa=aa*10+cc-'0',cc=getchar();
return aa*ff;
} int fir[maxn],nxt[2*maxn],to[2*maxn],rd[maxn],e=0;
void add(int x,int y) {
to[++e]=y;nxt[e]=fir[x];fir[x]=e;
rd[y]++;
} int zz[maxn],s=1,t=0;
void get_ans() {
int x,y;
for(int i=1;i<=m;++i) if(!rd[i]) zz[++t]=i;
while(s<=t) {
x=zz[s];
for(y=fir[x];y;y=nxt[y]) {
rd[to[y]]--;
if(!rd[to[y]]) zz[++t]=to[y];
}
s++;
}
if(t<tot) printf("-1");
else for(int i=1;i<=t;++i) if(zz[i]<=m) printf("%d ",zz[i]);
} int main() {
n=read();m=read();
int pos,last;
tot=last=m;
for(int i=1;i<=n;++i) {
for(int j=1;j<=m;++j) node[j].pos=j,node[j].x=read();
sort(node+1,node+m+1);
pos=1;
while(node[pos].x==-1&&pos<=m) ++pos;
if(pos>=m) continue;
add(node[pos].pos,++tot);pos++;
while(pos<=m&&node[pos-1].x==node[pos].x) add(node[pos].pos,tot),++pos;
for(;pos<=m;++pos) {
if(node[pos].x!=node[pos-1].x) tot++;
add(node[pos].pos,tot);
add(tot-1,node[pos].pos);
}
last=tot;
}
get_ans();
return 0;
}
CF274D的更多相关文章
随机推荐
- What is python .. (“dot dot”) notation syntax?
What you have is a float literal without the trailing zero, which you then access the __truediv__met ...
- java填坑记录
一.The absolute uri: [http://java.sun.com/jsp/jstl/core] cannot be resolved in either web.xml or the ...
- CSS3属性transform详解【转载】
CSS3属性transform详解之(旋转:rotate,缩放:scale,倾斜:skew,移动:translate) 在CSS3中,可以利用transform功能来实现文字或图像的旋转.缩放.倾 ...
- MyBatis框架的文件配置
第一步:log4j.properties的配置 原因:Mybatis的日志输出是依赖与log4j的,所以必须要配置 # Global logging configuration log4j.rootL ...
- Hadoop Serialization -- hadoop序列化详解 (2)【Text,BytesWritable,NullWritable】
回顾: 回顾序列化,其实原书的结构很清晰,我截图给出书中的章节结构: 序列化最主要的,最底层的是实现writable接口,wiritable规定读和写的游戏规则 (void write(DataOut ...
- 宝塔https
登录宝塔论坛,然后申请免费的https,然后在域名控制面板添加解析并验证,最后宝塔强制https跳转
- YOLO训练自己的数据集的一些心得
YOLO训练自己的数据集 YOLO-darknet训练自己的数据 [Darknet][yolo v2]训练自己数据集的一些心得----VOC格式 YOLO模型训练可视化训练过程中的中间参数 项目开源代 ...
- bootstrap-datetimepicker下ie8对indexOf的支持问题
问题: 由于ie8不支持indexOf这个方法,所以在引入bootstrap-datetimepicker.js的时候js会抛出错误. 解决: // 在bootstrap-datetimepicker ...
- 解决Cesium1.50对gltf2.0/3dtiles数据读取的问题
问题说明 Cesium 1.50(2018/10/01)版本打开3dtiles可能会出现加载不上导致渲染停止的错误. 错误说明为:RuntimeError: Unsupported glTF Exte ...
- mysql查询某个字段重复的数据
查询某个字段重复的数据 ; 查询股票重复的营业厅 ;