Labeling Balls(拓扑排序wa)
| 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:
- No two balls share the same label.
- 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)的更多相关文章
- [ACM] POJ 3687 Labeling Balls (拓扑排序,反向生成端)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10161 Accepted: 2810 D ...
- POJ3687.Labeling Balls 拓扑排序
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13201 Accepted: 3811 Descr ...
- poj 3687 Labeling Balls(拓扑排序)
题目:http://poj.org/problem?id=3687题意:n个重量为1~n的球,给定一些编号间的重量比较关系,现在给每个球编号,在符合条件的前提下使得编号小的球重量小.(先保证1号球最轻 ...
- PKU 3687 Labeling Balls(拓扑排序)
题目大意:原题链接 给出N个未编号的质量各不相同的球,以及它们质量轻重的大小关系,给它们从1-N贴标签编号,无重复.问是否存在可行的编号方法,不存在输出-1, 如果存在则输出唯一一种方案,此方案是使得 ...
- Java实现Labeling Balls(拓扑排序的应用)
1 问题描述 给出一些球,从1N编号,他们的重量都不相同,也用1N标记加以区分(这里真心恶毒啊,估计很多WA都是因为这里),然后给出一些约束条件,< a , b >要求编号为 a 的球必须 ...
- [poj3687]Labeling Balls_拓扑排序
Labeling Balls poj-3687 题目大意:给出一些球之间的大小关系,求在满足这样的关系下,编号小的尽量比编号大的球的方案. 注释:1<=N(球的个数)<=200,1< ...
- Labeling Balls(拓扑)
http://poj.org/problem?id=3687 看题意看了半天没看懂怎么回事,看完Discuss彻底凌乱了..后来看了题解才懂,就是逆向建图+拓扑排序,建图时要判重边. #include ...
- POJ3687——Labeling Balls(反向建图+拓扑排序)
Labeling Balls DescriptionWindy has N balls of distinct weights from 1 unit to N units. Now he tries ...
- 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 ...
随机推荐
- makefile死磕笔记
开始我会插播一段我如何学习makefile的废话,如果不想听的话,请直接跳到我的makefile教程. 首先得先说明学习makefile真是一个痛苦的过程,尤其是用干巴巴的看书来学习的过程,简直可以用 ...
- 通过jpegoptim批量压缩文件
#!/bin/sh filelist=$(ls) for file in $filelist do if [ -d $file ] then du -h $file /usr/local/bin/jp ...
- Buffer Cache(缓冲区缓存)篇:keep缓冲区池(保留池)
Buffer Cache可以有三个池 默认缓冲区池 keep缓冲区池 recycling缓冲区池 --保留池和回收池可以独立于sga中的其他缓存分配内存.创建表的时候可以在storage子句中使用b ...
- C语言入门(20)——使用VC2013对C语言进行调试
软件调试过程中,有时会一些逻辑和内存访问方面的问题,如果没有调试器的帮助,找出何处代码导致这块内存被更改是一件非常麻烦的事情.恰当运用数据断点可以快速帮我们定位问题的所在. 1.VC的调试快捷键 F5 ...
- 如何在Root的手机上开启ViewServer,使得HierachyViewer能够连接
前期准备: 关于什么是Hierarchy Viewer,请查看官方文档:http://developer.android.com/tools/debugging/debugging-ui.html.个 ...
- poj1477---搭积木
#include<stdio.h> #include<stdlib.h> int main() { int n,i; int bricks[55],set=0; while(s ...
- setObject与setValue的区别
在使用NSMutableDictionary的时候经常会使用setValue forKey与setObject forKey,他们经常是可以交互使用的,代码中经常每一种的使用都有.1.先看看setVa ...
- mongodb创建、更新、删除
1.插入操作 user = {"username":"lcq","sex":"man"} db.user.insert( ...
- [置顶] 浅析objc的消息机制
学习ios的同学都知道ojbc一种runtime的语言,runtime表明函数的真正执行的时候来确定函数执行的.这样的好处就是我们能很灵活的设计我们的代码,也能在看似合法的情况下做一些非常有意思的事情 ...
- 还是log4net的使用
最近做个项目要用到日志系统,这这可把我给难住了,后来问了下度娘,发现只有你想不到的,没有那些找不到的开源组件,后来发现了log4net,但是我是控制台程序,没有个实例还真不好搞,想想还是看看他的运行过 ...