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的更多相关文章
随机推荐
- css-文本两行或多行文本溢出显示省略号(转)
转自:http://www.daqianduan.com/6179.html 感谢作者 1.单行文本的溢出显示省略号 overflow: hidden; text-overflow:ellipsis ...
- pickle序列化一个函数,将fun()取出文件
pickle序列化一个函数,将fun()取出文件
- 阿里云 Aliplayer高级功能介绍(九):自动播放体验
基本介绍 经常会碰到客户询问,为什么我设置了autoplay为true,但是没有自动播放,每次都要向客户解释这个是浏览器从用户体验角度考虑做的限制,客户会继续询问那我要怎么做? 针对这个问题Alipl ...
- HZOI20190814 B 不等式
不等式 题目大意:求解满足$L \leqslant(S×x)mod M\leqslant R$的x最小正整数解,无解输出-1 几种部分分: $L==R$,就是$ex_gcd$; 解在$1e6$以内:搜 ...
- 约束布局ConstraintLayout加快布局速度
Android Studio2.2更新布局设计器,同时,引人了约束布局ConstraintLayout. 简单来说,可以把它看做是相对布局的升级版本,但是区别与相对布局更加强调约束.何为约束,即控件之 ...
- bash: express: command not found及vue连接数据库调接口
今天在使用express -e . 的命令时,cmd给我报了一段不识别的错误: bash: express: command not found ,在网上查了一下,有人指出是express4的版本将命 ...
- jeecms添加站点
Step1:点击[站点管理],然后点击[添加站点]. Step2:按照下图填写,注意[路径]这一栏!!这里我随便写了个[aaa]. Step3:这个时候在本地部署的tomcat的模板路径:tomcat ...
- JAVA_环境配置
1:系统环境 windows10 64位 jdk版本:jdk-8u131-windows-x64.exe,下载地址:http://www.oracle.com/technetwork/java/jav ...
- IO流13 --- 转换流实现文件复制 --- 技术搬运工(尚硅谷)
InputStreamReader 将字节输入流转换为字符输入流 OutputStreamWriter 将字符输出流转换为字节输出流 @Test public void test2() { //转换流 ...
- 015-WebDriver API
1. 从定位元素开始 8种元素定位方法 id find_element_by_id( ) name find_element_by_name( ) tag find_element_by_tag_na ...