POJ 3687 Labeling Balls (top 排序)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 15792 | Accepted: 4630 |
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 思路:
这题是很明显的top排序,只是这个排序有一点技巧。
那就是逆序排序,为什么要这么做?
我们知道,top排序删掉一个点,那么就会解锁后面的某些点,虽然正向操作,可以让当前的数字最小,但是不一定让后面解锁的更优。此时你要时刻记得,入队的顺序,并不是你输出的顺序。
而如果采用逆序的话,小的元素一定可以留到最后,再进行标号(当然标号也要大到小呀)。毕竟让字典序最小,是从小的元素开始算起的。
代码:
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<queue>
using namespace std;
vector<int>u[208];
int n,m,num;
int ss[208];
int in[208];
void init()
{
memset(in,0,sizeof(in));
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
u[i].clear();
}
int x,y;
for(int i=1;i<=m;i++){
scanf("%d%d",&x,&y);
u[y].push_back(x);
in[x]++;
}
} void Top_sort()
{
priority_queue<int> q;
queue<int>ans;
for(int i=1;i<=n;i++){
if(in[i]==0){q.push(i);}
}
int t;
while(!q.empty()){
t=q.top();q.pop();
ans.push(t);
int siz=u[t].size();
for(int i=0;i<siz;i++){
in[u[t][i]]--;
if(in[u[t][i]]==0){
q.push(u[t][i]);
}
}
}
int num=n;
if(ans.size()!=n){printf("-1\n");return;}
else{
while(!ans.empty()){
t=ans.front();ans.pop();
ss[t]=num--;
} printf("%d",ss[1]);
for(int i=2;i<=n;i++){
printf(" %d",ss[i]);
}
printf("\n");
}
} int main()
{
int T;
scanf("%d",&T);
while(T--){
init();
Top_sort();
}
}
POJ 3687 Labeling Balls (top 排序)的更多相关文章
- [ACM] POJ 3687 Labeling Balls (拓扑排序,反向生成端)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10161 Accepted: 2810 D ...
- poj 3687 Labeling Balls(拓扑排序)
题目:http://poj.org/problem?id=3687题意:n个重量为1~n的球,给定一些编号间的重量比较关系,现在给每个球编号,在符合条件的前提下使得编号小的球重量小.(先保证1号球最轻 ...
- 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
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14835 Accepted: 4346 D ...
- POJ 3687 Labeling Balls()
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9641 Accepted: 2636 Descri ...
- 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 要逆向建图,输入的时候要判重边,找入度为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 ...
随机推荐
- Lodop打印旋转180度 倒着打
方法1:打印出来后,直接把纸张倒过来.如果本身是白纸,打印机出纸内容是倒着的,可以打出来后手动倒着把纸张正过来.如果本身不是白纸,需要打印的纸张上有背景,调整进纸方向.(如果是卷纸,卷纸背景是反的,查 ...
- Web API2 使用默认Identity
当您选择个人账户在Web API项目模板,项目包含一个令牌授权服务器验证用户凭证和问题.下面的图显示了相同的凭证流的Web API组件. 发送一个未经授权的请求 首先,运行应用程序并单击按钮调用的AP ...
- vuejs 单文件组件.vue 文件
vuejs 自定义了一种.vue文件,可以把html, css, js 写到一个文件中,从而实现了对一个组件的封装, 一个.vue 文件就是一个单独的组件.由于.vue文件是自定义的,浏览器不认识,所 ...
- [离散时间信号处理学习笔记] 10. z变换与LTI系统
我们前面讨论了z变换,其实也是为了利用z变换分析LTI系统. 利用z变换得到LTI系统的单位脉冲响应 对于用差分方程描述的LTI系统而言,z变换将十分有用.有如下形式的差分方程: $\displays ...
- Freemake Video Converter视频转换软件下载地址及去广告
下载地址:http://download.freemake.net/FreemakeOriginals2/LS/FreemakeVideoConverterFull.exe 去片头及片尾广告:删除安装 ...
- jQuery 操作input select,checkbox
input $("#add_device_owner_id").val() $("#add_device_owner_id").val("d" ...
- Spring MVC启动过程(1):ContextLoaderListener初始化
此文来自https://my.oschina.net/pkpk1234/blog/61971 (写的特别好)故引来借鉴 Spring MVC启动过程 以Tomcat为例,想在Web容器中使用Spirn ...
- G - Balanced Lineup POJ - 3264 线段树最大最小值区间查询模版题
题意 给出一个序列 每次查询区间的max-min是多少 思路:直接维护max 和min即可 写两个query分别查最大最小值 #include<cstdio> #include< ...
- 【CF633D】Fibonacci-ish
题目描述 小y最近迷上了fibonacci数列,他定义了一种数列叫类fibonacci数列: 1.这个数列包含至少\(2\)个元素 2.\(f_0\)和\(f_1\)是任意选取的 3.\(f_{n+2 ...
- 【题解】 bzoj2982: combination (Lucas定理)
题面戳我 Solution 板子题 Code //It is coded by ning_mew on 7.25 #include<bits/stdc++.h> #define LL lo ...