Labeling Balls
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12466   Accepted: 3576

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
题解:反向建图,却是wa。。。先放着吧
wa代码:
 #include<stdio.h>
#include<string.h>
#include<queue>
#define mem(x,y) memset(x,y,sizeof(x))
using namespace std;
const int MAXN=;
const int MAXM=<<;
int head[MAXM];
int que[MAXN],ans[MAXN];
struct Edge{
int from,to,next;
};
Edge edg[MAXM];
int edgnum;
int N;
void add(int u,int v){
Edge E={u,v,head[u]};
edg[edgnum]=E;
head[u]=edgnum++;
}
void topu(){
priority_queue<int>dl;
int top=;
for(int i=;i<=N;i++)
if(!que[i])dl.push(i);
while(!dl.empty()){
int k=dl.top();
dl.pop();
ans[top++]=k;
que[k]=-;
for(int i=head[k];i!=-;i=edg[i].next){
int v=edg[i].to;
que[v]--;
if(!que[v])dl.push(v);
}
}
if(top!=N+)puts("-1");
else{
for(int i=N;i>=;i--){
if(i!=N)printf(" ");
printf("%d",ans[i]);
}
puts("");
}
}
int main(){
int T,M,a,b;
scanf("%d",&T);
while(T--){
scanf("%d%d",&N,&M);
for(int i=;i<=N;i++)ans[i]=i;
mem(que,);
mem(head,-);
edgnum=;
while(M--){
scanf("%d%d",&a,&b);
add(b,a);//
que[a]++;//反向建图
}
topu();
}
return ;
}

Labeling Balls(拓扑排序wa)的更多相关文章

  1. [ACM] POJ 3687 Labeling Balls (拓扑排序,反向生成端)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10161   Accepted: 2810 D ...

  2. POJ3687.Labeling Balls 拓扑排序

    Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13201 Accepted: 3811 Descr ...

  3. poj 3687 Labeling Balls(拓扑排序)

    题目:http://poj.org/problem?id=3687题意:n个重量为1~n的球,给定一些编号间的重量比较关系,现在给每个球编号,在符合条件的前提下使得编号小的球重量小.(先保证1号球最轻 ...

  4. PKU 3687 Labeling Balls(拓扑排序)

    题目大意:原题链接 给出N个未编号的质量各不相同的球,以及它们质量轻重的大小关系,给它们从1-N贴标签编号,无重复.问是否存在可行的编号方法,不存在输出-1, 如果存在则输出唯一一种方案,此方案是使得 ...

  5. Java实现Labeling Balls(拓扑排序的应用)

    1 问题描述 给出一些球,从1N编号,他们的重量都不相同,也用1N标记加以区分(这里真心恶毒啊,估计很多WA都是因为这里),然后给出一些约束条件,< a , b >要求编号为 a 的球必须 ...

  6. [poj3687]Labeling Balls_拓扑排序

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

  7. Labeling Balls(拓扑)

    http://poj.org/problem?id=3687 看题意看了半天没看懂怎么回事,看完Discuss彻底凌乱了..后来看了题解才懂,就是逆向建图+拓扑排序,建图时要判重边. #include ...

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

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

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

随机推荐

  1. oc对象互相引用内存释放解决方案

    1.ARC 一端用strong,一端用weak 2.非ARC 一端用retain,一端用assign

  2. [Django 1.5] Windows + Apache + wsgi配置

    基本步骤 下载安装Apache http://httpd.apache.org/download.cgi. 下载安装modwsgi 模块http://code.google.com/p/modwsgi ...

  3. Android 微信分享信息

    随着微信越来越火,越来越多的应用要求有分享到微信的功能.虽然有很多平台都帮集成有分享功能,比如友盟.但是个人觉得友盟集成的东西太多了,自己封装得太过分了,很多资源文件也要带进去,所以感觉不是怎么好,所 ...

  4. docker 数据管理<1>

    1. 挂载本地的目录到容器里: docker run -itd -v /data/:/data1 centos bash // -v 用来指定挂载目录, :前面的/data为本地目录,:后面的/dat ...

  5. Google的兼容包问题【转】

    转自:http://blog.sina.com.cn/s/blog_3e28c8a50101g14g.html 项目之前好好的,今天开Eclipse,,出错. 错误Error retrieving p ...

  6. POJ 3111 K Best(二分答案)

    [题目链接] http://poj.org/problem?id=3111 [题目大意] 选取k个物品,最大化sum(ai)/sum(bi) [题解] 如果答案是x,那么有sigma(a)>=s ...

  7. C++面向对象编程初步

    1,使用const 指针; const int * pOne; //指向整型常量的指针,指向的值不能修改; int * const pTwo; //指向整型的常量指针,指向的值可以修改,但该指针不能再 ...

  8. 网易云课堂_C语言程序设计进阶_第三周:结构:结构、类型定义、联合

    3.1 枚举 3.2 结构 3.3 类型定义 3.1 枚举 枚举是一种用户定义的数据类型,它用关键字enum以如下语法来表明: enum 枚举类型名字{名字0,...,名字n}; 枚举类型名字通常并不 ...

  9. position:relative可以默认提升元素的z-index;

    position:relative可以默认提升元素的z-index; 相对没有添加position的元素来说:

  10. Haffman编码(haffman树)

    Haffman编码 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 哈弗曼编码大家一定很熟悉吧(不熟悉也没关系,自己查去...).现在给你一串字符以及它们所对应的权值 ...