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 ...
随机推荐
- Java第一次实训
package com.wsy.work; public class JudgeNumber { public static void main(String[] args) { int a = 5; ...
- Monkey脚本API简介
一.API简介 LaunchActivity(pkg_name, cl_name):启动应用的Activity.参数:包名和启动的Activity. Tap(x, y, tapDuration): 模 ...
- Intel HD Graphics 620 (华硕FL5900U) Windows7下安装显卡驱动
我们的牙膏大厂Intel,不但会挤牙膏,现在连驱动也不给你用了.KabyLake也就是第七代酷睿处理器,集成了Intel HD Graphics 620核显,核显(显卡)驱动程序只支持到Win10这一 ...
- js的常用文档对象,document
1.document的概念:window的子对象,由于DOM对象模型的默认对象就是window,因此Window对象中的方法和子对象不需要通过Window来引用. - 2.document的组成:属性 ...
- 【python练习题】程序4
# 题目:输入某年某月某日,判断这一天是这一年的第几天? import time year = input('输入年份: \n') month = input('输入月份: \n') day = in ...
- BZOJ4205卡牌配对——最大流+建图优化
题目描述 现在有一种卡牌游戏,每张卡牌上有三个属性值:A,B,C.把卡牌分为X,Y两类,分别有n1,n2张. 两张卡牌能够配对,当且仅当,存在至多一项属性值使得两张卡牌该项属性值互质,且两张卡牌类别不 ...
- Mail.Ru Cup 2018 Round 3
A:签到 #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> # ...
- Codeforces Global Round 1 自闭记
A:签到. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> ...
- Codeforces962F Simple Cycles Edges 【双连通分量】【dfs树】
题目大意: 给出一个无向图,问有哪些边只属于一个简单环. 题目分析: 如果这道题我们掌握了点双连通分量,那么结论会很显然,找到每个点双,如果一个n个点的点双正好由n条边构成,那么这些边都是可以的. 这 ...
- bzoj 2429: [HAOI2006]聪明的猴子 (最小生成树)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2429 思路:就是找最小生成树最大的一条边,最小生成树的性质,最后加入的那条边就是最大的 实现 ...