Labeling Balls
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9028   Accepted: 2444

Description

Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 toN 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 withb".

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 nextM line each contain two integersa and b indicating the ball labeled witha must be lighter than the one labeled withb. (1 ≤ a, bN) 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 labelN. 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
#include<iostream>
#include<stdio.h>
#include<queue>
using namespace std;
typedef struct v{ int vex;
v *next; }V;
V*p;
typedef struct h{ int indegree;
v *next ;
}H;
int n,result[300],tot,ans[300];
priority_queue<int ,vector<int>,less<int> > q;
H team[50000];
bool visit[305][305];
void topsort()
{
int i,ci,sum,a,b;
ci=1;
for(i=1;i<=n;i++)
{ if(team[i].indegree==0)
q.push(i); }
sum=n; while(!q.empty())
{
sum--;
a=q.top();
q.pop();
result[ci++]=a;
for(p=team[a].next;p!=0;p=p->next)
{ b=p->vex;
if(--team[b].indegree==0)
q.push(b);
} }
// printf("%d\n",sum);
if(sum>0)
{
printf("-1\n");
}
else
{
int nn=n;
for(i=1;i<=n;i++)
{ ans[result[i]]=nn--;//这是排序
}
for(i=1;i<n;i++) printf("%d ",ans[i]);//这是重量
printf("%d\n",ans[i]); } }
int main()
{ int i,m,a,b,T,j; scanf("%d",&T);
while(T--)
{
while(!q.empty())
{ q.pop();
} scanf("%d%d",&n,&m);
for(i=0;i<=n;i++)
for(j=0;j<=n;j++)
{ visit[i][j]=false;
} for(i=0;i<=n;i++)
{
team[i].indegree=0;
team[i].next=NULL;
result[i]=0;
}
while(m--)
{ scanf("%d%d",&b,&a);//反向建表
if(visit[a][b])
continue;
visit[a][b]=true;
team[b].indegree++;
p=new V;
p->vex=b;
p->next=team[a].next;
team[a].next=p; } topsort(); } return 0;
}
												

poj3687的更多相关文章

  1. [poj3687]Labeling Balls_拓扑排序

    Labeling Balls poj-3687 题目大意:给出一些球之间的大小关系,求在满足这样的关系下,编号小的尽量比编号大的球的方案. 注释:1<=N(球的个数)<=200,1< ...

  2. 两个很经典的拓扑排序题目POJ3687+HDU1285

    一.题目链接 POJ:http://poj.org/problem?id=3687 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1285 二.思路 这两 ...

  3. 拓扑排序+不是字典序的优先级排列(POJ3687+HDU4857)

    一.前言 在过去的一周里结束了CCSP的比赛,其中有一道题卡了我9个小时,各种调错都没法完整的调处来这题,于是痛下决心开始补题,这个是计划的一部分.事实上,基于错误的理解我写了若干发拓扑排序+字典序的 ...

  4. ACM/ICPC 之 拓扑排序-反向(POJ3687)

    难点依旧是题意....需要反向构图+去重+看题 POJ3687-Labeling Balls 题意:1-N编号的球,输出满足给定约束的按原编号排列的重量序列,如果有多组答案,则输出编号最小的Ball重 ...

  5. POJ3687 Labeling Balls(拓扑排序\贪心+Floyd)

    题目是要给n个重量1到n的球编号,有一些约束条件:编号A的球重量要小于编号B的重量,最后就是要输出字典序最小的从1到n各个编号的球的重量. 正向拓扑排序,取最小编号给最小编号是不行的,不举出个例子真的 ...

  6. poj3687 拓扑序

    题意:有编号 1-n 的球,每个球的质量不同,质量从 1 到 n 不等,给出一系列比较,分别是两个编号的球的大小关系,求一个序列满足上述关系,并且从编号 1 开始依次选择可选的最小质量,输出每个球的质 ...

  7. POJ3687——Labeling Balls(反向建图+拓扑排序)

    Labeling Balls DescriptionWindy has N balls of distinct weights from 1 unit to N units. Now he tries ...

  8. POJ3687 Labeling Balls(拓扑)

    题目链接. 题目大意: N个球,从1-N编号,质量不同,范围1-N,无重复.给出小球间的质量关系(<), 要求给每个球贴标签,标签表示每个球的质量.按编号输出每个球的标签.如果解不唯一,按编号小 ...

  9. POJ-3687 Labeling Balls(拓扑)

    不一样的拓扑排序 给定一些标记为1到n的数, 求出满足a < b 的序列, 如果有多个输出, 按先标签1往前的位置, 然后按标签2往前的位置, 对于每个标签, 位置都尽量往前. 因为位置要往前, ...

随机推荐

  1. word编辑器解码集合

    $(document).ready(function () { $(".content").each(function () { var content = $(this).htm ...

  2. 11月下旬poj其他题

    poj1000,poj1003,poj1004,poj1064,poj1218 水题 poj1012:0<k<14——漂亮的打表 poj1651:与能量项链很像的dp poj1159:回文 ...

  3. 转载:iOS 推送的服务端实现

    参考网址1: iOS消息推送机制的实现 http://www.cnblogs.com/qq78292959/archive/2012/07/16/2593651.html 参考网址2: iOS 推送的 ...

  4. Toast 用于一个页面有多个提示

    private Toast mToast; 2 初始化 mToast = Toast.makeText(this,"",Toast.LENGTH_SHORT); 3 方法 priv ...

  5. UVA 1663 Purifying Machine (二分图匹配,最大流)

    题意: 给m个长度为n的模板串,模板串由0和1和*三种组成,且每串至多1个*,代表可0可1.模板串至多匹配2个串,即*号改成0和1,如果没有*号则只能匹配自己.问:模板串可以缩减为几个,同样可以匹配原 ...

  6. apache开源项目-- UIMA

    UIMA (Unstructured Information Management applications) 是一个软件系统,用来分析大量的非结构化信息从而发掘中对最终用户有用的知识点,一个最典型的 ...

  7. java/python中获取当前系统时间,并与字符串相互转换格式,或者转化成秒数,天数等整数

    java转换成秒数 Date类有一个getTime()可以换回秒数,例如: public class DateToSecond { public static void main(String[] a ...

  8. ftp在shell脚本中的使用方法

    1. ftp自动登录批量下载文件. #####从ftp服务器上的/home/data 到 本地的/home/databackup#####!/bin/bashftp -n<<!open 1 ...

  9. UI篇--android实现底部按钮布局

    1.采用LinearLayout布局: <LinearLayout android:id="@+id/main" android:layout_width="fil ...

  10. 前言:关于nagios监控

    前言,关于nagios监控. 这段时间一直在做关于nagios监控,不停的做实验,从而也忽略了书写这方面,今天写一份安装文档花了四个多小时,看来写文档也是一件很麻烦的事情,不过,做过的事情还是需要留下 ...