POJ 3687 Labeling Balls()
Labeling Balls
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 9641
Accepted: 2636
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
[Submit] [Go Back] [Status] [Discuss]
::做这道题,原先的思路就是从前面往后扫,入度为0的就赋予未被使用的最小值,可是这样无法得到字典序最小。
看了题解才知道,这道题要逆向建图,且从后往前扫,入度为0的赋予当前未使用的最大值。
1: #include <iostream>
2: #include <cstdio>
3: #include <cstring>
4: #include <algorithm>
5: using namespace std;
6: typedef long long ll;
7: const int maxn=40010;
8: int head[maxn],in[220],ans[220],vis[220];
9: int cnt,n,m;
10:
11: struct node
12: {
13: int u,v,p;
14: }e[maxn];
15:
16: int topo()
17: {
18: memset(vis,0,sizeof(vis));//标记数组
19: int k=n;
20: int i,j,u;
21: for(i=1; i<=n; i++)
22: {
23: for(u=n; u>0; u--)
24: {
25: if(!vis[u]&&in[u]==0)
26: break;
27: }
28: if(u<=0) return 0;
29: vis[u]=1;
30: ans[u]=k--;
31: for(j=head[u]; j!=-1; j=e[j].p)
32: {
33: in[e[j].v]--;
34: }
35: }
36: return 1;
37: }
38:
39: int main()
40: {
41: int T;
42: scanf("%d",&T);
43: while(T--)
44: {
45: int ok=1,a,b;
46: cnt=0;
47: scanf("%d%d",&n,&m);
48: memset(head,-1,sizeof(head));
49: memset(in,0,sizeof(in));
50: while(m--)
51: {
52: scanf("%d%d",&a,&b);
53: e[cnt].u=b;
54: e[cnt].v=a;
55: e[cnt].p=head[b];
56: head[b]=cnt++;
57: in[a]++;
58: if(a==b) ok=0;
59: }
60: if(ok==0||topo()==0)
61: printf("-1\n");
62: else
63: {
64: for(int i=1; i<=n; i++)
65: printf("%d ",ans[i]);
66: printf("\n");
67: }
68: //printf("\n");
69: }
70: return 0;
71: }
POJ 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(反向拓扑+贪心思想!!!非常棒的一道题)
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: 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 - 贪心 - 拓扑排序
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(拓扑排序)
题目:http://poj.org/problem?id=3687题意:n个重量为1~n的球,给定一些编号间的重量比较关系,现在给每个球编号,在符合条件的前提下使得编号小的球重量小.(先保证1号球最轻 ...
- POJ 3687 Labeling Balls 逆向建图,拓扑排序
题目链接: http://poj.org/problem?id=3687 要逆向建图,输入的时候要判重边,找入度为0的点的时候要从大到小循环,尽量让编号大的先入栈,输出的时候注意按编号的顺序输出重量, ...
- poj 3687 Labeling Balls(拓补排序)
Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them ...
随机推荐
- mysql数据库入门
在很多地方都有人提到MySQL这个数据,之前没有接触过的mysql数据库的童鞋们可以跟我一起走进mysql的世界. http://hovertree.com/menu/mysql/ 安装我就不多说了, ...
- 消灭Bug!18款最佳的问题跟踪管理应用程序
摘要:工欲善其事,必先利其器,对于开发者来说,处理Bug是一件比较头疼的事,那么如何高效地解决Bug,选择一款合适的Bug跟踪处理工具会让你事半功倍. 对于开发者来说,Bug往往是他们最头疼的问题.有 ...
- Ajax学习资源大全[本来是转载的,但是现在我增加了很多]
本欲放转载区,但是这样一文章放那里基本是没有用的,帮助不了任何人!所以放新手了!! 我一般非经典或者自己用不上不转载,所以如果你不幸看见了的话,恰恰你又对AJAX有兴趣的话不防看下,也许对你有用的!! ...
- Fluent NHibernate and Mysql,SQLite,PostgreSQL
http://codeofrob.com/entries/sqlite-csharp-and-nhibernate.html https://code.google.com/archive/p/csh ...
- PFold.js 折叠纸片
PFold.js是一款折叠纸片插件,支持定义折叠纸牌数量.折叠动画效果.折叠方向,而且还支持折叠结束后回调方法. 在线实例 效果一 效果二 效果三 使用方法 <div id="uc-c ...
- Form.action传值问题
通过浏览器地址栏输入url并通过?传递参数请求资源时,?后面的参数叫做 "查询字符串",会触发后台Servlet的doGet(),因为通过浏览器地址栏直接访问的方式是GET方式. ...
- Microsoft SQL Server,附加数据库 错误:Error 916解决方法
错误信息:错误提示:标题: Microsoft SQL Server Management Studio Express —————————— 无法为此请求检索数据. (Microsoft.SqlS ...
- 使用CodeMirror在浏览器中实现编辑器的代码高亮效果
使用CodeMirror在浏览器中实现编辑器的代码高亮效果 在网站后台管理中希望能够对网站的样式表css与js文件以及模板html进行管理,在编辑的时候只是以普通文本展示又太普通,显得好难看,于是便在 ...
- arcgis union 0x80040218
我利用ArcGis中的union工具将两张图层叠加时,系统总是提示失败,这是什么原因?希望高手能够解决这个问题.图片是系统提示,表示看不懂哪里出错了? 你必须使用数据管理-要素-修复几何,源数据存在不 ...
- 对于Oracle analyze table的使用总结 . 对于Oracle analyze table的使用总结 .
对于Oracle analyze table的使用总结 . 对于Oracle analyze table的使用总结 . analyze table 一般可以指定分析: 表,所有字段,所有索引字段,所有 ...