Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13645   Accepted: 3955

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:

  1. No two balls share the same label.
  2. 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

  1. 5
  2.  
  3. 4 0
  4.  
  5. 4 1
  6. 1 1
  7.  
  8. 4 2
  9. 1 2
  10. 2 1
  11.  
  12. 4 1
  13. 2 1
  14.  
  15. 4 1
  16. 3 2

Sample Output

  1. 1 2 3 4
  2. -1
  3. -1
  4. 2 1 3 4
  5. 1 3 2 4

Source

输出满足所给限制条件的小球编号,要求靠前的球编号尽可能小。

转换思路,将靠后的球标记得尽可能大。根据题目关系反向建边,做类似拓扑排序的操作即可。(不能像一般的拓扑排序一样,将入度为0的点压进栈挨个处理,而应每次找入度为0的最靠后的点来处理,因为每个点处理完,都可能解锁新的较靠后的点)。

  1. /*by SilverN*/
  2. #include<iostream>
  3. #include<algorithm>
  4. #include<cstring>
  5. #include<cstdio>
  6. #include<cmath>
  7. using namespace std;
  8. const int mxn=;
  9. struct edge{
  10. int v,next;
  11. }e[mxn];
  12. int hd[mxn],cnt;
  13. int in[mxn];
  14. int ans[mxn];
  15. int n,m;
  16. void add_edge(int u,int v){
  17. e[++cnt].v=v;e[cnt].next=hd[u];hd[u]=cnt;
  18. }
  19. int st[mxn],top;
  20. int vis[mxn];
  21. void tp(){
  22. top=;
  23. int tot=;
  24. int i,j;
  25. while(){
  26. if(tot>=n)break;
  27. for(i=n;i>=;i--){//每次找最靠后的可行点
  28. if(!in[i] && !vis[i]){st[++top]=i,vis[i]=;break;}
  29. }
  30. if(!top){
  31. printf("-1\n");
  32. return;
  33. }
  34. while(top){
  35. int u=st[top--];
  36. for(i=hd[u];i;i=e[i].next){
  37. in[e[i].v]--;
  38. }
  39. ans[u]=++tot;
  40. }
  41. }
  42. for(i=;i<=n;i++){
  43. printf("%d ",n+-ans[i]);
  44. }
  45. printf("\n");
  46. return;
  47. }
  48. int main(){
  49. int T;
  50. scanf("%d",&T);
  51. while(T--){
  52. memset(e,,sizeof e);
  53. memset(hd,,sizeof hd);
  54. memset(in,,sizeof in);
  55. memset(vis,,sizeof vis);
  56. cnt=;
  57. scanf("%d%d",&n,&m);
  58. int i,j;int a,b;
  59. for(i=;i<=m;i++){
  60. scanf("%d%d",&a,&b);
  61. add_edge(b,a);
  62. in[a]++;
  63. }
  64. tp();
  65. }
  66. return ;
  67. }

POJ3687 Labeling Balls的更多相关文章

  1. POJ3687——Labeling Balls(反向建图+拓扑排序)

    Labeling Balls DescriptionWindy has N balls of distinct weights from 1 unit to N units. Now he tries ...

  2. POJ3687.Labeling Balls 拓扑排序

    Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13201 Accepted: 3811 Descr ...

  3. POJ3687 Labeling Balls(拓扑排序\贪心+Floyd)

    题目是要给n个重量1到n的球编号,有一些约束条件:编号A的球重量要小于编号B的重量,最后就是要输出字典序最小的从1到n各个编号的球的重量. 正向拓扑排序,取最小编号给最小编号是不行的,不举出个例子真的 ...

  4. POJ3687 Labeling Balls(拓扑)

    题目链接. 题目大意: N个球,从1-N编号,质量不同,范围1-N,无重复.给出小球间的质量关系(<), 要求给每个球贴标签,标签表示每个球的质量.按编号输出每个球的标签.如果解不唯一,按编号小 ...

  5. POJ-3687 Labeling Balls(拓扑)

    不一样的拓扑排序 给定一些标记为1到n的数, 求出满足a < b 的序列, 如果有多个输出, 按先标签1往前的位置, 然后按标签2往前的位置, 对于每个标签, 位置都尽量往前. 因为位置要往前, ...

  6. Labeling Balls(poj3687)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13109   Accepted: 3782 D ...

  7. [poj3687]Labeling Balls_拓扑排序

    Labeling Balls poj-3687 题目大意:给出一些球之间的大小关系,求在满足这样的关系下,编号小的尽量比编号大的球的方案. 注释:1<=N(球的个数)<=200,1< ...

  8. POJ 3687 Labeling Balls()

    Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9641 Accepted: 2636 Descri ...

  9. Labeling Balls 分类: POJ 2015-07-28 19:47 10人阅读 评论(0) 收藏

    Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11893 Accepted: 3408 Descr ...

随机推荐

  1. ls显示前几行或后几行数据

    显示前3行数据 ls -l|head -n 3 显示后3行数据 ls -l|tail -n 3

  2. 《TensorFlow实战》中AlexNet卷积神经网络的训练中

    TensorFlow实战中AlexNet卷积神经网络的训练 01 出错 TypeError: as_default() missing 1 required positional argument: ...

  3. 将xml转为array 输出xml字符

    //将xml转为array private function fromXml($xml){ // 禁止引用外部xml实体 libxml_disable_entity_loader(true); ret ...

  4. debug模式开启会做哪些事(源码分析)

    以往开发中不管是django框架下开发还是其它框架下开发, 只知道在开发阶段要开启debug模式, 却一直没有深究它会我们做哪些事, 今天使用tornado时偶然看到源码中写的很清楚,故写下来加深印象 ...

  5. python3调取百度地图API输出某地点的经纬度信息

    1. 查看API接口说明 地址:http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding 注:callback ...

  6. [BZOJ1503]郁闷的出纳员(Splay)

    Description OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经常 ...

  7. Debug调试文件

    在debug.h中设置g_debug_switch即可控制调试级别. /* debug.c */ #include "debug.h" const char *get_log_le ...

  8. 2、大O表示法

    一.大O表示法 大O表示法不是一种算法.它是用来表示一个算法解决问题的速度的快慢.一般我们描述一件事情完成的快慢是用时间描述的,比如说我完成一道计算题用了多少分钟.但算法的运算是很难用准确的时间来描述 ...

  9. 9、python中的控制流

    学习完python的基础与数据后,我们就可以编写一些简单的命令了.但这时我们发现,目前位置写出来的程序都是自上而下顺序地执行的.要想程序改变这种自上而下的流程多一点变化,我们就要学习三种程序中的语句. ...

  10. Java面向对象---抽象类与接口

    final关键字 1.final关键字在java中被称为完结器,表示最终的意思: 2.final能声明类.方法.属性: 3.使用final声明的类不能被继承,使用final声明的方法不能被重写,使用f ...