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

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 思路:
这题是很明显的top排序,只是这个排序有一点技巧。
那就是逆序排序,为什么要这么做?
我们知道,top排序删掉一个点,那么就会解锁后面的某些点,虽然正向操作,可以让当前的数字最小,但是不一定让后面解锁的更优。此时你要时刻记得,入队的顺序,并不是你输出的顺序。
而如果采用逆序的话,小的元素一定可以留到最后,再进行标号(当然标号也要大到小呀)。毕竟让字典序最小,是从小的元素开始算起的。
代码:
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<queue>
using namespace std;
vector<int>u[208];
int n,m,num;
int ss[208];
int in[208];
void init()
{
memset(in,0,sizeof(in));
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
u[i].clear();
}
int x,y;
for(int i=1;i<=m;i++){
scanf("%d%d",&x,&y);
u[y].push_back(x);
in[x]++;
}
} void Top_sort()
{
priority_queue<int> q;
queue<int>ans;
for(int i=1;i<=n;i++){
if(in[i]==0){q.push(i);}
}
int t;
while(!q.empty()){
t=q.top();q.pop();
ans.push(t);
int siz=u[t].size();
for(int i=0;i<siz;i++){
in[u[t][i]]--;
if(in[u[t][i]]==0){
q.push(u[t][i]);
}
}
}
int num=n;
if(ans.size()!=n){printf("-1\n");return;}
else{
while(!ans.empty()){
t=ans.front();ans.pop();
ss[t]=num--;
} printf("%d",ss[1]);
for(int i=2;i<=n;i++){
printf(" %d",ss[i]);
}
printf("\n");
}
} int main()
{
int T;
scanf("%d",&T);
while(T--){
init();
Top_sort();
}
}

  

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

  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(拓扑排序)

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

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

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

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

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12246   Accepted: 3508 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()

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

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

  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. 个人用的感觉比较舒服的 idea 插件,不定时更新

    1.mybatis plugin 用的最舒服的 idea 上的 plugin 之一,快速跳转 dao 的映射的 xml 文件,生成配置文件.语法提示等 不过这个收费,,具体步骤百度吧 2.Rainbo ...

  2. Fiddler-学习笔记-远程抓包

    1 操作系统低于win7用 fiddler 2 win7 或win7以上版本,用 fiddler4片本 2 fiddler开关:左下角或点击F12控件fiddler开关,开=capturing 3 启 ...

  3. 魔术方法之__call与__callStatic方法

    <?php class human{ private function t(){ } /** * 魔术方法__call * * @param string $method 获得方法名 * @pa ...

  4. layui loading

    layer.msg('加载中', { icon: 16 ,shade: 0.4}); layer.load(2);风格二 setTimeout(function(){ layer.closeAll(' ...

  5. Linux下 rewrite_mod 的配置

    以下使用最新的 Ubuntu 16.04 测试; 安装好apache后先确认有没有rewrite模块,大多数情况下是有的:ls /etc/apache2/mods-available |grep re ...

  6. DotNetty 实现 Modbus TCP 系列 (二) ModbusFunction 类图及继承举例

    本文已收录至:开源 DotNetty 实现的 Modbus TCP/IP 协议 ModbusFunction 类图如下: 如前文所述,所有请求/相应的 PDU 均继承自 ModbusFunction, ...

  7. henrylee2cn简单的rpc服务

    server 端package controllers import ( "github.com/henrylee2cn/teleport" "model/system& ...

  8. BZOJ2480Spoj3105 Mod&BZOJ1467Pku3243 clever Y——EXBSGS

    题目描述 已知数a,p,b,求满足a^x≡b(mod p)的最小自然数x. 输入     每个测试文件中最多包含100组测试数据.     每组数据中,每行包含3个正整数a,p,b.     当a=p ...

  9. [NOIp2016] 蚯蚓

    类型:单调队列 传送门:>Here< 题意:有$N$只蚯蚓,每秒都会伸长$q$.每一次都会有人选出最长的一条切成两半,长度分别是$\left \lfloor px \right \rflo ...

  10. GCD HDU - 2588

    输入 N 和 M (2<=N<=1000000000, 1<=M<=N), 找出所有满足1<=X<=N 且 gcd(X,N)>=M 的 X 的数量. Inpu ...