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 ...
随机推荐
- IO-02
/** 2 *A2-IO-02. 整数四则运算(10) 3 *C语言实现 4 *测试已通过 5 */ #include "stdio.h" #include "stdli ...
- ASP.NET自定义控件加载资源WebResource问题
最近项目用日期控件,想把My97的资源文件跟TextBox封装成一个DatePicker控件,其实很简单的意见事情,但是还是用了一天多的时间,主要的问题就是解决资源文件加载的问题.通过一天多的努力,得 ...
- java应用程序远程登录linux并执行其命令(ssh jar包)
http://www.ganymed.ethz.ch/ssh2/在这个网址下载一个调用ssh和scp命令的jar包. 然后,就可以写程序了.将上面的jar包导入MyEclipse,下面是一个类的实例代 ...
- centos6.4 ceph安装部署之ceph object storage
preface: ceph-deploy does not provide a rapid installation for Ceph Object Storage install Configura ...
- DB_Links创建际删除
创建 create database link gadata0008 connect to gadata0008 identified by "gold" using 'ORA11 ...
- win下vm10+mac os 10.9安装遇到问题
在windows 8下安装vm10.0.0+mac os 10.9遇到问题记录例如以下: 一.因为之前我装的vm9+mac os 10.7: 二.准备安装mac os 10.9,把vm9换成vm10: ...
- BaseAdapter 注意的关键点!
BaseAdapter 我们一般就是继承然后重写自定义,然后listview set进去即可! 数据改变的时候,我们习惯这样: public void update(List list) { ...
- 取得正在运行的Activity
在main.xml中: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: ...
- UINavigationController技巧<一>——修改返回按钮的标题
UINavigationController 一般push到另一界面后,返回按钮标题便是上一页面的title,但是对于push的第一页或者是上一页面没有title的,返回按钮标题便是默认back,如图 ...
- Swfit中视图跳转
.跳转到任一UIViewController var sb = UIStoryboard(name: "Main", bundle:nil) var vc = sb.instant ...