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

题意:n个重量为1~n的球,给定一些编号间的重量比较关系,现在给每个球编号,在符合条件的前提下使得编号小的球重量小。(先保证1号球最轻,其次2号……)

    这道题每次输入a,b的时候表示的是编号为a的球比编号为b的球轻,最后输出的是从编号 1到编号 n每个小球的重量,如果存在多组解,输出使最小重量尽量

排在前边的那组解,亦即 所有解中 1到 n号球的重量的字典序最小。。。。所以说最后重量 1 到 n 的球的标号的字典序最小的做法是不对的。

分析:拓扑排序,注意根据题的要求,要先保证1号球最轻,如果我们由轻的向重的连边,然后我们依次有小到大每次把重量分给一个入度为0的点,那么在拓扑时我们面对多个入度为0的点,我们不知道该把最轻的分给谁才能以最快的速度找到1号(使1号入度为0),并把当前最轻的分给1号。所以我们要由重的向轻的连边,然后从大到小每次把一个重量分给一个入度为0的点。这样我们就不用急于探求最小号。我们只需要一直给最大号附最大值,尽量不给小号赋值,这样自然而然就会把轻的重量留给小号。

注意重边。

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxm=;
int rd[maxm];
bool g[maxm][maxm],vis[maxm];
int answer[maxm];
int t,n,m;
void tupu()
{
if(m==)
{
for(int i=; i<n; i++)
cout<<i<<' ';
cout<<n<<endl;
return ;
}
memset(vis,false,sizeof(vis));
memset(answer,,sizeof(answer));
int k;
for(int i=n; i>; i--)
{
k=-;
for(int j=n; j>; j--)
{
if(!vis[j]&&rd[j]==) //如果没有点为被访问过且入度为0的点
{
k=j; //记录下标
break;
}
}
if(k==-) //若没有入度为0的点
{
cout<<"-1"<<endl;
return ;
}
vis[k]=true;
answer[k]=i;
for(int j=; j<=n; j++)
{
if(g[k][j])
rd[j]--;
}
}
for(int i=; i<n; i++)
cout<<answer[i]<<" ";
cout<<answer[n]<<endl;
return ;
}
int main()
{
int a,b;
cin>>t;
while(t--)
{
memset(g,false,sizeof(g));
memset(rd,,sizeof(rd));
cin>>n>>m;
for(int i=; i<=m; i++)
{
cin>>a>>b;
if(!g[b][a])
rd[a]++; //重量轻的入度加一
g[b][a]=true;
}
tupu();
}
return ;
}

poj 3687 Labeling Balls(拓补排序)的更多相关文章

  1. POJ 3687 Labeling Balls (top 排序)

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

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

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

  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: 12246   Accepted: 3508 D ...

  8. poj——3687 Labeling Balls

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

  9. POJ 3687 Labeling Balls()

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

随机推荐

  1. 转载:C# socket端口复用-多主机头绑定

    什么是端口复用: 因为在winsock的实现中,对于服务器的绑定是可以多重绑定的,在确定多重绑定使用谁的时候,根据一条原则是谁的指定最明确则将包递交给谁,而且没有权限之分.这种多重绑定便称之为端口复用 ...

  2. 在Maven中新增自定的jar包

    引言: 在软件项目中,Maven提供了一体化的类库管理系统,非常实用.但是,如果新增的类库jar在网络上无法获取到,如何在本地按照Maven的规则添加进来呢?本文将通过一个小例子展示新增过程. 背景介 ...

  3. idea中经常用到的快捷键

    Ctrl+Alt+L   格式化代码 (但是在jsp或js中不给力,不如eclipse好用) Ctrl+G        搜索行数 Ctrl+F12     搜索方法,变量等... Ctrl+F8   ...

  4. SSM商城项目(十三)

    1.   学习计划 1.订单系统 2.提交订单 3.MyCAT 2.   订单系统 2.1. 功能分析 1.在购物车页面点击“去结算”按钮跳转到订单确认页面. a)         展示商品列表 b) ...

  5. parallel Stream 学习

    首先,我们需要了解下ForkJoinPool.ForkJoin框架是从jdk7中新特性,它同ThreadPoolExecutor一样,也实现了Executor和ExecutorService接口.它使 ...

  6. css第四天

    四丶 补充:CSS Display(显示) 与 Visibility(可见性) display属性设置一个元素应如何显示,visibility属性指定一个元素应可见还是隐藏. 隐藏元素 - displ ...

  7. Linux安装jdk.18和tomcat8

    JDK1.8的安装 第一步:下载JDK1.8 1.百度搜索jdk1.8,打开属于oracle官网的搜索结果项 2.先点击接受协议选项,然后选择要下载的64位linux版本jdk,直接单击通过浏览器下载 ...

  8. 如何在 Koa 中获取 client 的 ip 以及 port

    ctx.body.remoteAddress ctx.body.remotePort

  9. ajax获取数据中文显示问号

    技术交流群:816227112 问题: 解决 : 在 response.getWriter() 之前加上 response.setContentType("text/html;charset ...

  10. 设计模式之装饰者模式-java实例

    设计模式之装饰者模式 需求场景 我们有了别人提供的产品,但是别人提供的产品对我们来说还不够完善,我们需要对这个产品的功能进行补强,此时可以考虑使用装饰者模式. 我们已经有了产品,而且这个产品的功能非常 ...