poj——3687 Labeling Balls
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 14835 | Accepted: 4346 |
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 题意:有T组测试数据,每组测试数据第一行有两个数n,m。代表有n个球,接下来m行,每行有两个数a,b,代表a球比b球轻,让你从小到大输出球的重量,若是相同重量,按照字典序输出。思路:这道题可以用拓扑排序来做、、拓扑排序是一定的,但是我们的拓扑排序里面是要用到大根堆,而且我们是逆向来存的层数、、、首先显而易见是拓扑排序.但是考虑到要让前面的数尽量小。我们就不可以采用正向建图了、、 举个例子: 1 我们得到了下面这样一个图、、 5 4 正向建图得到的点的次序是1 4 5 3 2
1 4 1到n的质量为1 5 4 3 2 4 2 正确答案为1 5 3 4 2 5 3 3 2 为什么?!
我们正向建图时,1出去以后的入度为0的点只有4 5我们并没有考虑到5还指向3于是我们an小根堆先选择了4 这样就会造成错误、、、 所以我们这个地方就要反向建图,当然这也就决定了我们要用大根堆、、、、这样我们每次把标号最大的放在后面就可以很好地解决字典序这个问题
注意:我们这个地方输出的是他们的质量、、、、、
代码:
#include<queue> #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #define N 41000 using namespace std; priority_queue<int>q; int n,m,x,y,s,t,tot,sum,in[N],ans[N],head[N]; int read() { ,f=;char ch=getchar(); ; ch=getchar();} +ch-'; ch=getchar();} return x*f; } struct Edge { int from,to,next; }edge[N]; void add(int x,int y) { tot++; edge[tot].to=y; edge[tot].next=head[x]; head[x]=tot; } void begin() { sum=,tot=; memset(,sizeof(in)); memset(ans,,sizeof(ans)); memset(edge,,sizeof(edge)); memset(head,,sizeof(head)); } int main() { t=read(); while(t--) { begin(); n=read(),m=read(); ;i<=m;i++) { x=read(),y=read(); in[x]++,add(y,x); } ;i<=n;i++) ) q.push(i); tot=n; while(!q.empty()) { x=q.top(),q.pop();ans[x]=tot; tot--;sum++; for(int i=head[x];i;i=edge[i].next) { int t=edge[i].to; in[t]--; ) q.push(t); } } if(sum!=n) printf("-1\n"); else { ;i<n;i++) printf("%d ",ans[i]); printf("%d\n",ans[n]); } } ; }
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: 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 - 贪心 - 拓扑排序
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 ...
随机推荐
- hihocoder 分隔相同字符
思路: 枚举,贪心. 在“合法”的前提下放置越排在前边的字母越好. “合法”:'a' - 'z'中没有一个字母的个数超过当前串剩余长度的一半(偶数情况下)或长度的一半加1(奇数情况下). 实现: #i ...
- 掌握Spark机器学习库-07-回归算法原理
1)机器学习模型理解 统计学习,神经网络 2)预测结果的衡量 代价函数(cost function).损失函数(loss function) 3)线性回归是监督学习
- 'NSUnknownKeyException' … setValue:forUndefinedKey:]: …not key value coding compliant
解决一个问题: 当我添加一个IBout, 报了如下错误 NSUnknownKeyException' … setValue:forUndefinedKey:]: …not key value codi ...
- WebSocket 的一些简单页面推送使用
因为做通信项目的时候,需要实时获取每个分机的当前状态,发现websocket还不错,只是对浏览器的要求比较高, 针对特定用户推送消息,网上有一些 public class GetHttpSession ...
- 洛谷 P3371 【模板】单源最短路径(堆优化dijkstra)
题目描述 如题,给出一个有向图,请输出从某一点出发到所有点的最短路径长度. 输入输出格式 输入格式: 第一行包含三个整数N.M.S,分别表示点的个数.有向边的个数.出发点的编号. 接下来M行每行包含三 ...
- (转)淘淘商城系列——SSM框架整合之表现层整合
http://blog.csdn.net/yerenyuan_pku/article/details/72721120 上文我们一起学习了Service层的整合,本文将教大家如何整合表现层. 我们在t ...
- 理解 call, apply 的用法
callcall() 方法使用一个指定的 this 值和单独给出的一个或多个参数来调用一个函数. function list() { return Array.prototype.slice.call ...
- 【Linux】CentOS tar压缩与解压命令大全
tar命令详解 -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用 ...
- nginx 服务器
1.windows版本的nginx启动报错 No mapping for the Unicode character exists in the target multi-byte code page ...
- element-UI el-table二次封装
Part.1 为什么要二次封装? 这是 Element 网站的 table 示例: <template> <el-table :data="tableData" ...