Labeling Balls

Time Limit: 1000MS
Memory Limit: 65536K

Total Submissions: 9641
Accepted: 2636

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

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

                       [Submit]   [Go Back]   [Status]   [Discuss]

 

::做这道题,原先的思路就是从前面往后扫,入度为0的就赋予未被使用的最小值,可是这样无法得到字典序最小。

看了题解才知道,这道题要逆向建图,且从后往前扫,入度为0的赋予当前未使用的最大值。

 

   1: #include <iostream>

   2: #include <cstdio>

   3: #include <cstring>

   4: #include <algorithm>

   5: using namespace std;

   6: typedef long long ll;

   7: const int maxn=40010;

   8: int head[maxn],in[220],ans[220],vis[220];

   9: int cnt,n,m;

  10:  

  11: struct node

  12: {

  13:     int u,v,p;

  14: }e[maxn];

  15:  

  16: int topo()

  17: {

  18:     memset(vis,0,sizeof(vis));//标记数组

  19:     int k=n;

  20:     int i,j,u;

  21:     for(i=1; i<=n; i++)

  22:     {

  23:         for(u=n; u>0; u--)

  24:         {

  25:             if(!vis[u]&&in[u]==0)

  26:                break;

  27:         }

  28:         if(u<=0) return 0;

  29:         vis[u]=1;

  30:         ans[u]=k--;

  31:         for(j=head[u]; j!=-1; j=e[j].p)

  32:         {

  33:             in[e[j].v]--;

  34:         }

  35:     }

  36:     return 1;

  37: }

  38:  

  39: int main()

  40: {

  41:     int T;

  42:     scanf("%d",&T);

  43:     while(T--)

  44:     {

  45:         int ok=1,a,b;

  46:         cnt=0;

  47:         scanf("%d%d",&n,&m);

  48:         memset(head,-1,sizeof(head));

  49:         memset(in,0,sizeof(in));

  50:         while(m--)

  51:         {

  52:             scanf("%d%d",&a,&b);

  53:             e[cnt].u=b;

  54:             e[cnt].v=a;

  55:             e[cnt].p=head[b];

  56:             head[b]=cnt++;

  57:             in[a]++;

  58:             if(a==b) ok=0;

  59:         }

  60:         if(ok==0||topo()==0)

  61:            printf("-1\n");

  62:         else

  63:         {

  64:             for(int i=1; i<=n; i++)

  65:                 printf("%d ",ans[i]);

  66:             printf("\n");

  67:         }

  68:         //printf("\n");

  69:     }

  70:     return 0;

  71: }

POJ 3687 Labeling Balls()的更多相关文章

  1. [ACM] POJ 3687 Labeling Balls (拓扑排序,反向生成端)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10161   Accepted: 2810 D ...

  2. POJ 3687 Labeling Balls(反向拓扑+贪心思想!!!非常棒的一道题)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16100   Accepted: 4726 D ...

  3. poj 3687 Labeling Balls【反向拓扑】

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12246   Accepted: 3508 D ...

  4. POJ 3687 Labeling Balls (top 排序)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15792   Accepted: 4630 D ...

  5. poj——3687 Labeling Balls

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14835   Accepted: 4346 D ...

  6. 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 ...

  7. poj 3687 Labeling Balls(拓扑排序)

    题目:http://poj.org/problem?id=3687题意:n个重量为1~n的球,给定一些编号间的重量比较关系,现在给每个球编号,在符合条件的前提下使得编号小的球重量小.(先保证1号球最轻 ...

  8. POJ 3687 Labeling Balls 逆向建图,拓扑排序

    题目链接: http://poj.org/problem?id=3687 要逆向建图,输入的时候要判重边,找入度为0的点的时候要从大到小循环,尽量让编号大的先入栈,输出的时候注意按编号的顺序输出重量, ...

  9. poj 3687 Labeling Balls(拓补排序)

    Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them ...

随机推荐

  1. C#设计模式——原型模式(Prototype Pattern)

    一.概述 在软件开发中,经常会碰上某些对象,其创建的过程比较复杂,而且随着需求的变化,其创建过程也会发生剧烈的变化,但他们的接口却能比较稳定.对这类对象的创建,我们应该遵循依赖倒置原则,即抽象不应该依 ...

  2. ASP.NET 使用AJAX让GridView的数据行显示提示框(ToolTip)

    介绍ASP.NET AJAX可以使你的web应用程序具有更丰富的功能和更多的用户响应. 本文中,我将演示如何通过ASP.NET AJAX的帮助,给像GridView这样的数据绑定控件的数据行增加pop ...

  3. 重新想象 Windows 8.1 Store Apps (91) - 后台任务的新特性: 下载和上传的新特性, 程序启动前预下载网络资源, 后台任务的其它新特性

    [源码下载] 重新想象 Windows 8.1 Store Apps (91) - 后台任务的新特性: 下载和上传的新特性, 程序启动前预下载网络资源, 后台任务的其它新特性 作者:webabcd 介 ...

  4. *nix下传统编程入门之GCC

    准备工作 注意:本文可能会让你失望,如果你有下列疑问的话:为什么要在终端输命令啊? GCC 是什么东西,怎么在菜单中找不到? GCC 不能有像 VC 那样的窗口吗?…… 那么你真正想要了解的可能是 a ...

  5. 2016 一中培训 day 5 ksum

    又是一天的爆零!!!!! 原本第一题 很容易做 竟然优化过度 丢了答案 1693: ksum Time Limit 1000 ms Memory Limit 524288 KBytes Judge S ...

  6. hdu 1518 拼正方形

    本题来自:http://acm.hdu.edu.cn/showproblem.php?pid=1518 题意:输入几个长度,判断能否拼成正方形. 以下部分参考了网友代码,终于ac啦. #include ...

  7. jQuery实现图片伦播效果(淡入淡出+左右切换)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. C++模板元编程

    ABC

  9. js 数组常用方法

    var arr =[0,1,2,3,4,5,6,7,8,9]; 1,shift() 删除数组的第一个元素,返回删除的值  //这里返回0 2,unshift(1,2)  把参数添加到数组的前面,返回值 ...

  10. CSS 选择器汇总

    CSS 选择器 CSS 元素选择器 CSS 选择器分组 CSS 类选择器详解 CSS ID 选择器详解 CSS 属性选择器详解 CSS 后代选择器 CSS 子元素选择器 CSS 相邻兄弟选择器 CSS ...