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. linux下open-vswitch安装卸载操作

    一. ovs 从源码编译安装: 安装依赖项: ? 1 2 3 4 5 6 7 8 9 10 11 # apt install make # apt install gcc # apt install ...

  2. IDEAL启动项目的时候报java.lang.NoClassDefFoundError: javax/servlet/Filter错误

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...

  3. webconfig标签收集

    在web项目启动时,很多因为vs没有报错,而页面跑不出来的情况,无法调试找到错误, 可以在webconfig中添加一个标签,运行项目就可以在页面显示错误 <customErrors mode=& ...

  4. windows yii2 配置redis

    1 安装redis https://blog.csdn.net/gaotanpan3666/article/details/83047081 安装redis 2  yii安装redis扩展 compo ...

  5. Excel VBA ——如何导出数据到excel表格

    sub OutPut() Dim FileTitle, MyPath, MyFullName As String Application.ScreenUpdating = false '关闭表格公式的 ...

  6. Json压缩工具

    一般的json文件拥有很多的空格和注释,虽然读起来比较方便,但是在运行的时候是要占一些内存的. 于是json压缩工具就应运而生了,这个工具是用java做的,原理是: 1:在Eclipse中导出一个可运 ...

  7. linux安装jdk1.8.0_91

      1,创建一个目录,安装jkd. # mkdir -pv /usr/local/jdk 2,按照需要下载jdk版本. 下载地址: https://www.oracle.com/technetwork ...

  8. [转]真正的中国天气api接口xml,json

    转自:http://blog.csdn.net/fancylovejava/article/details/26102635 我只想说现在网上那几个api完全坑爹有木有??? 官方的申请不来有木有,还 ...

  9. JAVA跨域CORS

    写了一个前端vue调用后端Java的接口,报“Access-Control-Allow-Origin”问题.

  10. 对TCP三次握手的思考

    从第一次了解到TCP的连接方式到现在有一年半了,但好像一直没有把三次握手的由来弄透彻,解释上总有一些瑕疵在,现在来说说昨晚的思考. 对于一个通信链路,由于种种原因发送方发送的消息未必能传达到接收方,所 ...