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

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 这道题搜了下题解,大神的思路就是清新脱俗;附上自己的理解分析
There is a blank line before each test case. 这句话可把我坑死了 还以为每组测试数据前都要打印出一行空格,pe了好久
题意:输入n,m代表有n个数参加排序,接下来m行输入数据x ,y表示x要在y的前边,但是重量小的(即数字小的)要尽量放到前边;
题解:此题要用反向拓扑,正向是肯定不行的,如数据
5 3
1 4
4 2
3 5
正向的话输出结果为 3 1 5 2 4 显然不对,正确结果应该为 1 3 4 2 5 同时采用优先队列,重的最先输出,我们先将重的,赋给
最先出队的(因为设置的优先队列是大的先出队)这样就可以把轻的尽量留在最后(因为是要逆序,所以留到后边,输出时是在前边)
#include<stdio.h>
#include<string.h>
#include<vector>
#include<queue>
using namespace std;
int n,m;
int map[300][300];
int vis[300],a[300];
void getmap()
{
int i,j;
memset(vis,0,sizeof(vis));
memset(map,0,sizeof(map));
while(m--)
{
int a,b;
scanf("%d%d",&a,&b);
if(!map[b][a])
{
map[b][a]=1;
vis[a]++;
}
}
}
void tuopu()
{
int i,j,k,g=n;
k=0;
memset(a,0,sizeof(a));
priority_queue<int>q;
while(!q.empty())
q.pop();
for(i=1;i<=n;i++)
if(!vis[i])
q.push(i);
int u;
while(!q.empty())
{
u=q.top();
q.pop();
a[u]=g--;
for(i=1;i<=n;i++)
{
if(map[u][i])
{
vis[i]--;
if(vis[i]==0)
q.push(i);
}
}
}
if(g!=0)
printf("-1\n");
else
{
for(i=1;i<n;i++)
printf("%d ",a[i]);
printf("%d\n",a[n]);
}
}
int main()
{
int t,o=0;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
getmap();
tuopu();
}
return 0;
}

  

												

poj 3687 Labeling Balls【反向拓扑】的更多相关文章

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

  2. POJ 3687 Labeling Balls【拓扑排序 优先队列】

    题意:给出n个人,m个轻重关系,求满足给出的轻重关系的并且满足编号小的尽量在前面的序列 因为输入的是a比b重,但是我们要找的是更轻的,所以需要逆向建图 逆向建图参看的这一篇http://blog.cs ...

  3. POJ 3687 Labeling Balls(拓扑排序)题解

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

  4. POJ - 3687 Labeling Balls (拓扑)

    (点击此处查看原题) 题意 此处有n盏灯,编号为1~n,每盏灯的亮度都是唯一的,且在1~n范围之间,现已知m对灯之间的关系:a b ,说明灯a的亮度比灯b小,求出每盏灯的亮度,要求字典序最小(编号小的 ...

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

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

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

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

  7. poj——3687 Labeling Balls

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

  8. POJ 3687 Labeling Balls()

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

  9. POJ 3687 Labeling Balls (top 排序)

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

随机推荐

  1. CoreAnimation

    CoreAnimation 1.CABasicAnimation // position CABasicAnimation *ba = [CABasicAnimation animationWithK ...

  2. VS2008/MFC —常用控件使用总结 转载

    在公司培训期间,经理给了我们没人10个界面草图,让我们在VS2008下使用MFC设计,因为在经理的帮助和自己的努力下,终于在三天时间内完成,现在就根据在这三天 时间里所用到的控件做出如下总结: 1.D ...

  3. js快速排序法

    var quickSort = function(arr) { if (arr.length <= 1) { return arr; } var pivotIndex = Math.floor( ...

  4. mysql5.7下载与安装,php5.6与mysql5.7整合

    Part1 mysql5.7下载 百度“mysql下载”,打开官网    2. 在页面右上角点击,注册/登录 3. 登录后显示下载页面,选择windows 4. 然后选择MySQL Installer ...

  5. 关于【键鼠<局域网>共享软件:synergy】install

    Installation 另外,本人在centos6.5环境下作为server运行时,遇到一个问题,synergy1.5随着系统升级居然变成了1.3X,所以如果遇到类似问题,请您先用 rpm -qa ...

  6. directUI

    MFC界面开发中,习惯了使用控件,亦或者是自绘制控件来美化界面,但操作起来繁琐,还不太美观.DirectUI的出现,对于界面开发,给了我们一个新的选择,目前很多公司使用了该技术对其产品进行了美化,效果 ...

  7. Mac系统安装Lua(转)

    下载最新版的lua请点击,然后解压 运行“终端”进入到该文件夹下 ,主要是cd [文件夹名] 在“终端”输入 make macosx (回车) 在“终端”输入 make test (回车) 然后再输入 ...

  8. window—BAT脚本

    bat脚本注释方法: 1.:: 注释内容(第一个冒号后也可以跟任何一个非字母数字的字符) 2.rem 注释内容(不能出现重定向符号和管道符号) 3.echo 注释内容(不能出现重定向符号和管道符号)〉 ...

  9. Android 开发环境搭建9传送帖)

    ---恢复内容开始--- 首先,得安装软件,感觉我所找到的一些课本上写的都比较简略,走过一些弯路后,决定按照这个百度经验来 Android安卓开发环境搭建详细教程 http://jingyan.bai ...

  10. codeforces C. Painting Fence

    http://codeforces.com/contest/448/problem/C 题意:给你n宽度为1,高度为ai的木板,然后用刷子刷颜色,可以横着刷.刷着刷,问最少刷多少次可以全部刷上颜色. ...