先上题目:

4110: PE class

Submit your solution     Discuss this problem     Best solutions
 

Description

The dwarven kingdom and the giant countries classmates have PE class together.
The students' height from 1 cm to N cm,everyone's height is different from other's.
There are N students in total, and there are M pairs of relationship, such as (i, j)
said the student's height stand at position i is less than the one stand at position j.
Can you find each position of the height of students?

Input

The first line of input is the number of test case.
The first line of each test case contains two integers, N (1 ≤ N ≤ 200) and M (0 ≤ M ≤ 40,000).
The next M line each contain two integers a and b, indicating the student's height stand at
position a must be less than the one stand at position b. (1 ≤ a, b ≤ N)
There is a blank line before each test case.

Output

For each test case output one line the students' heights from position 1 to position N.
If there are several solutions exist, you should output the one with the smallest height for label 1,
then with the smallest height for label 2, then with the smallest height for label 3 and so on...
If no solution exists, output one line,just a number -1. output a blank line after each test case

Sample Input

4

4 2
1 2
2 1 4 1
2 1 4 1
3 2 3 1
1 1

Sample Output

-1

2 1 3 4

1 3 2 4

-1

Source

ycg663@scuacm
Sichuan University Programming Contest 2012 Preliminary

  题意:有身高为1~n的n个人,每个人的身高都不一样,现在要这些人站成一列,给出一系列的关系(i,j)代表i位置的人要比j位置的人矮。现输出字典序最小的拓扑排序结果。

  这一题需要注意的地方是字典序最小,需要逆向构图(有(i,j)的关系,就要构造一条(j,i)的边)。

  为什么需要逆向构图?

  我的理解:字典序最小的逆否命题是除了遵守边的要求以外,还需要注意排在后面的数需要尽量大,所以我们可以在拓扑排序的时候每一次出来一个位置,就给它赋一个还没有用的最大值。

  为什么正向构图就不可以呢?

  先看一组数据

  4 2

  2 3

4 1

  如果按照正向构图,结果:4 1 2 3

  如果按照逆向构图,结果:1 3 2 4

  我的理解:并不一定所有的点都会被边连在一起,如果点被分成了几组的话,那么这几组的先后顺序就需要考虑了。就像上面的例子一组是2->3,另一组是4->1存在一组里面大位置在小位置前面,同时这个大位置比其他组的开头元素都要大,那就会得出错误的答案了(感觉这里说的还是不是很清楚)。

  感觉这就像需要贪心一样。但是这里在编号的时候因为它给了你限制条件位置编号大的尽量大(当然,这要先符合拓扑排序的要求),所以就需要逆向构图了。

  这一切的根本还是因为拓扑排序结果不唯一。

上代码:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define NUM 202
#define MAX 40002
using namespace std; int n,m; int e[NUM][NUM];
int tot[NUM];
int lo[NUM]; priority_queue<int,vector<int>,less<int> > p;
bool tp(){
int c=n;
int r=;
while(!p.empty()) p.pop();
for(int i=;i<=n;i++) if(tot[i]==) p.push(i);
while(!p.empty()){
int v=p.top();
lo[v]=c; c--;
r++;
p.pop();
for(int i=;i<=n;i++){
if(e[v][i]==){
tot[i]--;
if(tot[i]==){
p.push(i);
}
}
}
}
if(r<n) return ;
return ;
} int main()
{
int t,u,v;
bool f;
//freopen("data.txt","r",stdin);
scanf("%d",&t);
for(int z=;z<t;z++){
scanf("%d %d",&n,&m);
memset(e,,sizeof(e));
memset(tot,,sizeof(tot));
memset(lo,,sizeof(lo));
f=;
while(m--){
scanf("%d %d",&u,&v);
if(e[v][u]==){
if(u==v) f=;
e[v][u]=;
tot[u]++;
}
}
if(f || !tp()) printf("-1");
else{
for(int i=;i<=n;i++){
if(i!=) printf(" ");
printf("%d",lo[i]);
}
}
printf("\n\n");
}
return ;
}

4110

SCU - 4110 - PE class的更多相关文章

  1. PE Checksum Algorithm的较简实现

    这篇BLOG是我很早以前写的,因为现在搬移到CNBLOGS了,经过整理后重新发出来. 工作之前的几年一直都在搞计算机安全/病毒相关的东西(纯学习,不作恶),其中PE文件格式是必须知识.有些PE文件,比 ...

  2. 原创 C++应用程序在Windows下的编译、链接:第二部分COFF/PE文件结构

    2.1概述 在windows操作系统下,可执行文件的存储格式是PE格式:在Linux操作系统下,可执行文件的存储格式的WLF格式.它们都是COFF格式文件的变种,都是从COFF格式的文件演化而来的. ...

  3. 简单PE类代码

    三个文件分别是类定义文件pefile.h;类实现文件pefile.cpp;类调用文件petype.cpp. #ifndef PE_FILE_H #define PE_FILE_H #include & ...

  4. 获取pe文件的文件类型

    工程文件petype.cpp通过调用pefile类中的函数获取文件类型. 文件类型的判断通过5个监测点完成. 监测点1:dos头的e_magic 监测点2:nt头的Signature 监测点3:文件头 ...

  5. Reverse Core 第二部分 - 13章 - PE文件格式

    @date: 2016/11/24 @author: dlive ​ PE (portable executable) ,它是微软在Unix平台的COFF(Common Object File For ...

  6. PE读写

    // 仿PE文件.cpp : Defines the entry point for the console application.// #include "stdafx.h"# ...

  7. PE文件格式(加密与解密3)(一)

    本次的了解主要讲解 PE的基本概念.MS-DOS文件头.PE文件头.区块.输入表.输出表等. 这里我将会结合一个简单的小程序来加深我对PE文件结构的了解. 使用学习工具:有StudyPE.LordPE ...

  8. 再探.NET的PE文件结构(安全篇)

    一.开篇 首先写在前面,这篇文章源于个人的研究和探索,由于.NET有自己的反射机制,可以清楚的将源码反射出来,这样你的软件就很容易被破解,当然这篇文章不会说怎么样保护你的软件不被破解,相反是借用一个软 ...

  9. 浅析MSIL中间语言——PE文件结构篇

    一.开篇 开篇我想讲一下于本文无关的话题,其实我很想美化一下自己博客园一直没时间弄,无意间找了博客园李宝亨的博客园里面有一篇分享自己主题的文章,我就将这个模板暂时用作我的blog主题,我要讲述一个关于 ...

随机推荐

  1. Webstorm配置运行React Native

    Webstorm配置运行React Native 1.选择配置 2.选择npm,设置package等参数 3.添加拓展工具 4.配置拓展工具(核心啊) 5.运行测试,ok的.

  2. 可持久化Treap(fhq Treap,非旋转式Treap)学习(未完待续)

    简介:     Treap,一种表现优异的BST 优势:     其较于AVL.红黑树实现简单,浅显易懂     较于Splay常数小,通常用于树套BST表现远远优于Splay     或许有人想说S ...

  3. Spring Boot (7) JdbcTemplate访问数据库

    使用jdbcTemplate操作数据库 spring framework对数据库的操作在jdbc上面做了深层次的封装,通过依赖注入功能,可以将datasource注册到jdbcTemplate中,学习 ...

  4. Android Fragment间的广播消息接收

    这种方式不用在配置文件加东西,我比较喜欢. 广播注册,可以写在Activity(onCreate),也可以写在Fragment(onActivityCreated)里. LocalBroadcastM ...

  5. 【PostgreSQL-9.6.3】表操作语句

    1.创建数据表 create table table_name ( 字段1 数据类型[列级别约束条件][默认值], 字段2 数据类型[列级别约束条件][默认值], 字段3 数据类型[列级别约束条件][ ...

  6. SpringBoot入门系列(转)

    SpringBoot入门系列:第一篇 Hello World http://blog.csdn.net/lxhjh/article/details/51711148

  7. kerberos认证原理---讲的非常细致,易懂

    前几天在给人解释Windows是如何通过Kerberos进行Authentication的时候,讲了半天也别把那位老兄讲明白,还差点把自己给绕进去.后来想想原因有以下两点:对于一个没有完全不了解Ker ...

  8. sql 排序

    select count(*) from vote group by contents PERCENT * from vote order by contents)as A group by cont ...

  9. 微信小程序跳转以及跳转的坑

    一.首先小程序的跳转方法有一下几种 js控制跳转 // 保留当前页面,跳转到应用内的某个页面 wx.navigateTo({ url: '../blueberry/blueberry' }); // ...

  10. 关于java中的继承

    我们都知道Java中的继承是复用代码.扩展子类的一种方式,继承使得Java中重复的代码能够被提取出来供子类共用,对于Java程序的性能以及修改和扩展有很大的意义,所以这是一个非常重要的知识点. 那么对 ...