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();
}
}

  

最新文章

  1. 路由集合中已存在名为“ XXXX” 的路由
  2. python 安装
  3. select、poll、epoll区别总结
  4. asp.net 事件加载顺序
  5. FZU-2075 Substring(后缀数组)
  6. linux和mac下的nginx和php的安装
  7. 必应词典UWP版-开发小结
  8. JAVA 8 Lambda表达式-Lambda Expressions
  9. 关于Android中图片大小、内存占用与drawable文件夹关系的研究与分析
  10. HotSpot Builder Utility安装指南
  11. Android ActionBar中的下拉菜单
  12. 一、oracle 高水位线详解
  13. 利用 Composer 一步一步构建自己的 PHP 框架(二)——构建路由
  14. [转] c# 数据类型占用的字节数
  15. session与cookie的区别,有哪些不同之处
  16. XML 序列化与PULL解析
  17. 不同数据库oracle mysql SQL Server DB2 infomix sybase分页查询语句
  18. OC之消息调用过程
  19. 南京邮电大学 JavaA期末复习要点总结
  20. Spring MVC @RequestMapping注解详解

热门文章

  1. 【转】console.log 用法
  2. How to execute a Stored Procedure with Entity Framework Code First
  3. codeforces982F
  4. 基于MMSE的预测
  5. Django-urls路由系统
  6. 67 个JavaScript和CSS实用工具、库与资源
  7. BZOJ1319Sgu261Discrete Roots——BSGS+exgcd+原根与指标+欧拉定理
  8. Android 下载App
  9. 【agc013d】Piling Up(动态规划)
  10. python 逻辑运算符问题