POJ——T 3687 Labeling Balls
http://poj.org/problem?id=3687
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 14842 | Accepted: 4349 |
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
Source
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <queue> using namespace std; const int M();
const int N();
int head[N],sumedge;
struct Edge
{
int v,next;
Edge(int v=,int next=):v(v),next(next){}
}edge[M];
inline void ins(int u,int v)
{
edge[++sumedge]=Edge(v,head[u]);
head[u]=sumedge;
} priority_queue<int>que;
int rd[N],ans[N],cnt;
inline void init()
{
sumedge=cnt=;
memset(rd,,sizeof(rd));
memset(ans,,sizeof(ans));
memset(head,,sizeof(head));
memset(edge,,sizeof(edge));
} int AC()
{
int t; scanf("%d",&t);
for(int n,m,if_;t--;init())
{
scanf("%d%d",&n,&m);
for(int u,v;m--;ins(v,u))
scanf("%d%d",&u,&v),rd[u]++;
for(int i=;i<=n;i++)
if(!rd[i]) que.push(i);
for(int u,v,weight=n;!que.empty();)
{
u=que.top(); que.pop();
ans[u]=weight--;cnt++;
for(int i=head[u];i;i=edge[i].next)
{
v=edge[i].v;
if(--rd[v]==) que.push(v);
}
}
if(cnt!=n) puts("-1");
else
{
for(int i=;i<n;i++)
printf("%d ",ans[i]);
printf("%d\n",ans[cnt]);
}
}
return ;
} int I_want_AC=AC();
int main(){;}
POJ——T 3687 Labeling Balls的更多相关文章
- [ACM] POJ 3687 Labeling Balls (拓扑排序,反向生成端)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10161 Accepted: 2810 D ...
- 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 ...
- POJ 3687 Labeling Balls(反向拓扑+贪心思想!!!非常棒的一道题)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16100 Accepted: 4726 D ...
- POJ 3687 Labeling Balls()
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9641 Accepted: 2636 Descri ...
- poj 3687 Labeling Balls【反向拓扑】
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12246 Accepted: 3508 D ...
- POJ 3687 Labeling Balls (top 排序)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15792 Accepted: 4630 D ...
- poj——3687 Labeling Balls
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14835 Accepted: 4346 D ...
- poj 3687 Labeling Balls(拓扑排序)
题目:http://poj.org/problem?id=3687题意:n个重量为1~n的球,给定一些编号间的重量比较关系,现在给每个球编号,在符合条件的前提下使得编号小的球重量小.(先保证1号球最轻 ...
- POJ 3687 Labeling Balls 逆向建图,拓扑排序
题目链接: http://poj.org/problem?id=3687 要逆向建图,输入的时候要判重边,找入度为0的点的时候要从大到小循环,尽量让编号大的先入栈,输出的时候注意按编号的顺序输出重量, ...
随机推荐
- [读书笔记] Python数据分析 (二) 引言
1. 数据分析的任务:数据读写,数据准备(清洗,修整,规范化,重塑,切片切块,变形),转换,建模计算,呈现(模型/数据) 2. 数据集: bit.ly的1.usa.gov数据:URL缩短服务bit ...
- code-reading-notes--xml 解析
- H5图片上传、压缩
1.注册input file标签的onchange事件: 2.检查图片格式: 3.检查图片大小: 4.压缩图片 5.上传图片至服务器: 前端代码: document.getElementById('i ...
- dbgview
这两天在看一个问题,matlab打不开摄像头,总是报错. 尝试抓包,打印,分析代码,一直没有找出问题,后来用dbgview打印出来调试信息,找到了问题点. 不得不说,这个工具真不错,以前从来不知道. ...
- SQL SERVER-identity | @@identity | scope_identity
主键自增 IDENTITY(1,1),MS SQL Server 使用 IDENTITY 关键字来执行 auto-increment 任务. 在上面的实例中,IDENTITY 的开始值是 1,每条新记 ...
- unity3d进程通信利用WM_COPYDATE和HOOK
hello,近期用unity做了进程通信,应该是和c++的PC端实现通信,才開始一头雾水,后来实现了才知道好繁杂......先感谢对我提供帮助的百度,谷歌以及游戏圈的大大们. 在进程通信中非常多方法, ...
- call to OpenGL ES API with no current context 和Fatal signal 11
近日在用cocos2dx3.4的时候使用了JNI调用,发现一个现象 当不使用jni的时候全然正常.使用了jni后回去的全部文字都变成黑块,而且有概率程序崩溃.附带出了两个log call to Ope ...
- XP单机版安装金蝶K3的13.1版本号,金蝶K3Wise安装步骤,安装成功
在我们安装金蝶K3时往往会出现各种报错.主要是由于我们的Windows Xp操作系统是Ghost版本号.或者是windows XP HOME或者是精简版,因此某些组件在系统里没有.导致我们安装金蝶K3 ...
- MyBatis数据持久化(二)建立数据库会话
上篇文章中介绍了我们使用mybatis前的准备工作,并在程序中通过jdbc与mysql数据库成功建立连接,接下来介绍如何使用mybatis与数据库建立会话.我们需要以下几个步骤: 1.在build p ...
- Win10运行在哪里,Win10的运行怎么打开
方法/步骤 1 唯一的方法是同时按下WIN+X键组合,如下图所示 步骤阅读 2 在弹出菜单可以看到运行了!如下图所示 步骤阅读 3 运行对话框出来了,如下图所示 步骤阅读 4 还有一个方法,点击桌面左 ...