博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3687 Labeling Balls(拓扑排序)题解
阅读量:4612 次
发布时间:2019-06-09

本文共 2031 字,大约阅读时间需要 6 分钟。

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

54 04 11 14 21 22 14 12 14 13 2

Sample Output

1 2 3 4-1-12 1 3 41 3 2 4

题意和思路:给你标号为1~N的球,a b代表a比b轻,需要你排列出这N个球的顺序,要求按编号字典序排列,最后要你依次给出这N个球的重量(这里要注意不是叫你给出编号的顺序,而是1的质量,2的质量...N的质量这样输出,就这里这WA了...)。思路很简单,就是把轻的球加度,然后重的指向轻的,然后度0的就是重的,优先队列要用降序,这样就能保证度为0的比较重的先取出,(这里用倒着存到ans[ ],从头往后读就是编号的正确顺序),但我们要注意他要我们输出重量,所以ans[ ]编号里从最后一个开始依次赋值1~N

代码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ll long long const int N=210;const int INF=1e9;using namespace std;int n,m,degree[N],ans[N],weight[N];    //ans记录倒序的编号,weight记录最终答案vector
g[N];     //邻接表void topo(){ int cnt=0; priority_queue
,less
> q; for(int i=1;i<=n;i++){ if(degree[i]==0) q.push(i); } while(!q.empty()){ int v=q.top(); q.pop(); ans[cnt]=v; cnt++; for(int i=0;i
=0;i--){ weight[ans[i]]=wei++; } for(int i=1;i<=n;i++) printf("%d ",weight[i]); cout<

转载于:https://www.cnblogs.com/KirinSB/p/9409098.html

你可能感兴趣的文章
[Vue-rx] Stream an API using RxJS into a Vue.js Template
查看>>
解决VC几个编译问题的方法——好用
查看>>
SPOJ #11 Factorial
查看>>
City Upgrades
查看>>
“人少也能办大事”---K2 BPM老客户交流会
查看>>
关于七牛进行图片添加文字水印操作小计
查看>>
DataSource数据库的使用
查看>>
CentOS开启samba实现文件共享
查看>>
MSSQL使用sqlbulkcopy批量插入数据
查看>>
证明一个数能被3整除,当且仅当它的各位数的和能被3整除
查看>>
2018秋寒假作业4—PTA编程总结1
查看>>
android自适应屏幕
查看>>
2019-北航面向对象-电梯作业总结
查看>>
SqlHelper
查看>>
初识算法、数据结构
查看>>
Luogu4069 SDOI2016 游戏 树链剖分、李超线段树
查看>>
Java的内部类真的那么难以理解?
查看>>
一文搞懂Java环境,轻松实现Hello World!
查看>>
hash实现锚点平滑滚动定位
查看>>
也谈智能手机游戏开发中的分辨率自适应问题
查看>>