poj——3687 Labeling Balls
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 14835 | Accepted: 4346 |
Description
Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 to N in such a way that:
- No two balls share the same label.
- The labeling satisfies several constrains like "The ball labeled with a is lighter than the one labeled with b".
Can you help windy to find a solution?
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 ball labeled with a must be lighter than the one labeled with b. (1 ≤ a, b ≤ N) There is a blank line before each test case.
Output
For each test case output on a single line the balls' weights from label 1 to label N. If several solutions exist, you should output the one with the smallest weight for label 1, then with the smallest weight for label 2, then with the smallest weight for label 3 and so on... If no solution exists, output -1 instead.
Sample Input
5 4 0 4 1 1 1 4 2 1 2 2 1 4 1 2 1 4 1 3 2
Sample Output
1 2 3 4 -1 -1 2 1 3 4 1 3 2 4 题意:有T组测试数据,每组测试数据第一行有两个数n,m。代表有n个球,接下来m行,每行有两个数a,b,代表a球比b球轻,让你从小到大输出球的重量,若是相同重量,按照字典序输出。思路:这道题可以用拓扑排序来做、、拓扑排序是一定的,但是我们的拓扑排序里面是要用到大根堆,而且我们是逆向来存的层数、、、首先显而易见是拓扑排序.但是考虑到要让前面的数尽量小。我们就不可以采用正向建图了、、 举个例子: 1 我们得到了下面这样一个图、、 5 4 正向建图得到的点的次序是1 4 5 3 2
1 4 1到n的质量为1 5 4 3 2 4 2 正确答案为1 5 3 4 2 5 3 3 2 为什么?!
我们正向建图时,1出去以后的入度为0的点只有4 5我们并没有考虑到5还指向3于是我们an小根堆先选择了4 这样就会造成错误、、、 所以我们这个地方就要反向建图,当然这也就决定了我们要用大根堆、、、、这样我们每次把标号最大的放在后面就可以很好地解决字典序这个问题
注意:我们这个地方输出的是他们的质量、、、、、
代码:
#include<queue> #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #define N 41000 using namespace std; priority_queue<int>q; int n,m,x,y,s,t,tot,sum,in[N],ans[N],head[N]; int read() { ,f=;char ch=getchar(); ; ch=getchar();} +ch-'; ch=getchar();} return x*f; } struct Edge { int from,to,next; }edge[N]; void add(int x,int y) { tot++; edge[tot].to=y; edge[tot].next=head[x]; head[x]=tot; } void begin() { sum=,tot=; memset(,sizeof(in)); memset(ans,,sizeof(ans)); memset(edge,,sizeof(edge)); memset(head,,sizeof(head)); } int main() { t=read(); while(t--) { begin(); n=read(),m=read(); ;i<=m;i++) { x=read(),y=read(); in[x]++,add(y,x); } ;i<=n;i++) ) q.push(i); tot=n; while(!q.empty()) { x=q.top(),q.pop();ans[x]=tot; tot--;sum++; for(int i=head[x];i;i=edge[i].next) { int t=edge[i].to; in[t]--; ) q.push(t); } } if(sum!=n) printf("-1\n"); else { ;i<n;i++) printf("%d ",ans[i]); printf("%d\n",ans[n]); } } ; }
poj——3687 Labeling Balls的更多相关文章
- [ACM] POJ 3687 Labeling Balls (拓扑排序,反向生成端)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10161 Accepted: 2810 D ...
- POJ 3687 Labeling Balls(反向拓扑+贪心思想!!!非常棒的一道题)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16100 Accepted: 4726 D ...
- POJ 3687 Labeling Balls()
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9641 Accepted: 2636 Descri ...
- poj 3687 Labeling Balls【反向拓扑】
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12246 Accepted: 3508 D ...
- POJ 3687 Labeling Balls (top 排序)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15792 Accepted: 4630 D ...
- poj 3687 Labeling Balls - 贪心 - 拓扑排序
Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 to N ...
- poj 3687 Labeling Balls(拓扑排序)
题目:http://poj.org/problem?id=3687题意:n个重量为1~n的球,给定一些编号间的重量比较关系,现在给每个球编号,在符合条件的前提下使得编号小的球重量小.(先保证1号球最轻 ...
- POJ 3687 Labeling Balls 逆向建图,拓扑排序
题目链接: http://poj.org/problem?id=3687 要逆向建图,输入的时候要判重边,找入度为0的点的时候要从大到小循环,尽量让编号大的先入栈,输出的时候注意按编号的顺序输出重量, ...
- poj 3687 Labeling Balls(拓补排序)
Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them ...
随机推荐
- JAVA 高级特性 JDBC
需要的jdbc jar 包: mysql-connector-java-5.1.38-b...960.9 KB ojdbc6.jar2.0 MB sqljdbc4.jar455.4 KB 数据持 ...
- html5表单新增元素与属性2
1.标签的control属性 在html5中,可以在标签内部放置一个表单元素,并且通过该标签的control属性来访问该表单元素. <script> function setValue() ...
- 转载:如何使用RFT自动打开IE
如何在RFT测试脚本中打开IE浏览器? 第一步,配置应用程序进行测试: “配置”菜单 ——> “配置应用程序进行测试...”,进入下面这个界面,默认三个自带的应用程序,点击“添加”加入IE. ...
- Bmob使用心得
1.在 Project 的 build.gradle 文件中添加 Bmob的maven仓库地址,示例如下:(注意文字说明部分): allprojects { repositories { jcente ...
- iOS Programming Camera 1
iOS Programming Camera 1 1 Displaying Images and UIImageView 1.1 put an instance of UIImageView o ...
- 强大的云存储与应用管理工具DzzOffice1.0 Beta(大桌子办公)发布下载
之前在9月份我们发布了一份内测版,得到了1000多位朋友参与下载测试.经过2个月,结合测试后朋友们反馈的问题,和开发建议.终于完成了这次Beta版的开发.感谢这两个月中参与测试,和帮助我们完善程序的朋 ...
- basename - 从文件名中剥离目录和后缀
总览 (SYNOPSIS) basename NAME [SUFFIX] basename OPTION 描述 (DESCRIPTION) 显示 去掉 目录成分 后的 NAME. 如果 指定了 SUF ...
- 异常详细信息: System.ComponentModel.Win32Exception: 信号灯超时时间已到
- 小b和矩阵
2486 小b和矩阵 2 秒 262,144 KB 5 分 1 级题 小b有一个m行n列的矩阵. 她会从(1,1)开始,顺时针螺旋访问该矩阵,每个元素恰好被访问一次. 请你按小b的访问顺序输出每个 ...
- 10C++类和对象
类和对象 8.1 面向对象程序设计方法概述 到目前为止,我们介绍的是C++在面向过程的程序设计中的应用.对于规模比较小的程序,编程者可以直接编写出一个面向过程的程序,详细地描述每一瞬时的数据结构及对其 ...