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. SSH本地端口转发的理解

    ssh -L 3307:127.0.0.1:3306 user@ssh-server -N 其中127.0.0.1:3306是指 ssh-server要访问资源的ip和端口 而3307则是隧道的开口, ...

  2. freemarker -include

    在ftl中使用<#include >时 ,页面被强制显示 需要在struts.xml增加跳转type ,或则可以加同一<result-types></result-typ ...

  3. Get started with Docker for Windows

    Welcome to Docker for Windows! Docker is a full development platform for creating containerized apps ...

  4. 51-nod(1443)(最短路)

    解题思路:最短路+记录前驱和,刚开始一直以为是最短路+MST,结果发现,因为无向图的原因,有些边权很小的边再最短路处理后可能这条边也符合某两个点的最短路径,所以我们觉得这条边也是可以在MST处理中使用 ...

  5. css背景色 透明字体不透明

    .demo{ padding: 25px; background-color: rgba(,,,0.5);/* IE9.标准浏览器.IE6和部分IE7内核的浏览器(如QQ浏览器)会读懂 */ }

  6. Django-urls路由系统

    Django的路由系统 Django 1.11版本 URLConf官方文档 URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表. ...

  7. Android Dialog 简单封装

    转载:https://www.cnblogs.com/zjjne/archive/2013/10/03/3350382.html public class MyAlertDialog { //regi ...

  8. Treasure Hunting HDU - 3468

    题意: 输入一个n行m列的图 每次按字母顺序走最短路, 从一个字母走到下一个字母的过程中,只能拿走一个金子,求走完当前图中所有的字母后能拿到的金子的最大值 解析: bfs求最短路 对于一个金子如果 d ...

  9. 【BZOJ1419】Red is good 期望DP

    题目大意 桌面上有\(R\)张红牌和\(B\)张黑牌,随机打乱顺序后放在桌面上,开始一张一张地翻牌,翻到红牌得到\(1\)美元,黑牌则付出\(1\)美元.可以随时停止翻牌,在最优策略下平均能得到多少钱 ...

  10. npm config 删除变量

    问题 安装npm时,使用npm config set 命令重新设置了变量,但是设置变量时少了个空格,设置错了.使用npm config ls -l 查看环境变量 添加错的这一个,应该如何删除? 解决 ...