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:

  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

题意:有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的更多相关文章

  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: 9641 Accepted: 2636 Descri ...

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

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

  5. POJ 3687 Labeling Balls (top 排序)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15792   Accepted: 4630 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. 一个完整的http请求分析

    Request URL:http://localhost:8080/test.jhtmlRequest Method:POSTStatus Code:200 OKRemote Address:[::1 ...

  2. 阿里云服务器安装ss使用

    下载安装服务器版shadowsocks yum install epel-release yum update yum install python-setuptools m2crypto super ...

  3. 从0开始搭建SQL Server 2012 AlwaysOn 第三篇(安装数据,配置AlwaysOn)

    这一篇是从0开始搭建SQL Server 2012 AlwaysOn 的第三篇,这一篇才真正开始搭建AlwaysOn,前两篇是为搭建AlwaysOn 做准备的 操作步骤: 1.安装SQL server ...

  4. Farseer.net轻量级ORM开源框架 V1.2.1版本升级消息

    提交版本V1.2.11.修复实体未设置主键时,无法找到主键ID字段,改为无主键时默认为"ID”字段2.新增:SqlServer2000Provider数据库驱动3.新增:DbContextI ...

  5. [Windows Server 2008] 搭建数据云备份

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:如何搭建数 ...

  6. AIX RAC 安装失败完全卸载

    1,删除软件安装目录 rm -rf /u01/app 2,删除以下目录内容 rm -rf/tmp/.oracle rm -rf/tmp/* rm -rf/tmp/ora* rm -rf/var/tmp ...

  7. eclipse下tomcat临时目录位置

    eclipse 开发web程序,启动tomcat服务器的时候.临时目录在你的工作区间workspace\.metadata\.plugins\org.eclipse.wst.server.core\t ...

  8. SweetAlert如何实现点击Confirm之后自动关闭

    swal({ title: "Are you sure?", text: "You will not be able to recover this imaginary ...

  9. classpath 路径和classpath*的区别

    classpath和classpath*区别:  classpath:只会到你的class路径中查找找文件. classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找 ...

  10. JDK1.8中的Stream详解

    Stream简介 Stream 作为 Java 8 的一大亮点,它与 java.io 包里的 InputStream 和 OutputStream 是完全不同的概念.它也不同于 StAX 对 XML ...