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 ...
随机推荐
- Congos
http://hi.baidu.com/tag/cognos%E7%B3%BB%E7%BB%9F%E7%AE%A1%E7%90%86/feeds http://www.blogjava.net/mlh ...
- yum 安装 5.6
http://www.cnblogs.com/XBlack/p/5178758.html
- C语言入门(17)——C语言数组应用的一个实例
本篇通过一个实例介绍使用数组的一些基本模式.问题是这样的:首先生成一列0-9的随机数保存在数组中,然后统计其中每个数字出现的次数并打印,检查这些数字的随机性如何.随机数在某些场合(例如游戏程序)中是非 ...
- libcurl提交表单上传文件
不多说了,curl的http上传文件代码示例,有需要的可以参考. int http_post_file(const char *url, const char *user, const char *p ...
- strlen、strcmp、strcat、strcpy、memcpy基础函数的实现
最近实习+投简历+琐事弄得自己忙的不行不行的,终于今天可以开始记录一些东西了... 1.strlen函数的实现 int strlen(const char *str){ assert(str!=NUL ...
- error LNK2019: 无法解析的外部符号
前些日子电脑系统崩了,重装了了下,原有的项目环境得重新搭建,总是在链接时提示:error LNK2019: 无法解析的外部符号………… 起初以为是库没包含全,可发现不是 有想了下可能是库的包含次序有问 ...
- AndroidUI 引导页面的使用
一个应用程序都少不了欢迎页面和引导页面,本文主要讲如何制作一个引页面: 首页所有的目录结构: 新建Welcome引导页面和Activity: <RelativeLayout xmlns:andr ...
- ios常用的框架(源自知乎上的回答)
首先,学习这类开源项目的主要目的是为了实现产品汪需求,如果不是这个目的,完全可以看Explore GitHub,当前最火的开源项目都在这里,当然你需要过滤一下语言. 好了,介绍几个希望能帮助到你. 普 ...
- linux下创建用户并且限定用户主目录
Linux 系统是一个多用户多任务的分时操作系统,任何一个要使用系统资源的用户,都必须首先向系统管理员申请一个账号,然后以这个账号的身份进入系统.用户的账号 一方面可以帮助系统管理员对使用系统的用户进 ...
- Adroid: getExternalStorageDirectory 不一定是你想要的外部存储SdCard
前情提要:我的测试机是华为荣耀6,我装过一个16G的内存卡 因为要面试的需要,我的一个演示项目用的是android本地的WebService.然而写好的webService部署到本地上,应用怎么获取数 ...