当前位置:网站首页>POJ3687Labeling Balls题解
POJ3687Labeling Balls题解
2022-08-04 11:21:00 【bj_hacker】
题目
链接
http://poj.org/problem?id=3687
字面描述
Labeling Balls
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 19619 Accepted: 5624
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
POJ Founder Monthly Contest – 2008.08.31, windy7926778
思路
可以先筛选编号大的球进行拓扑排序,注意图要反建
代码实现
#include<cstdio>
#include<string.h>
#include<cstring>
#include<stack>
using namespace std;
const int maxn=200+10;
int t,n,m;
int indegree[maxn],topo[maxn];
bool map[maxn][maxn];
inline bool Topo_sort(){
for(int i=n;i>=1;i--){
int temp=-1;
for(int j=n;j>=1;j--){
if(!indegree[j]){
temp=j;
break;
}
}
if(temp==-1)return false;
indegree[temp]=-1;
topo[temp]=i;
for(int j=1;j<=n;j++){
if(map[temp][j])indegree[j]--;
}
}
return true;
}
int main(){
scanf("%d",&t);
while(t--){
memset(indegree,0,sizeof(indegree));
memset(topo,0,sizeof(topo));
memset(map,false,sizeof(map));
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
if(!map[y][x]){
map[y][x]=true;
indegree[x]++;
}
}
if(!Topo_sort())printf("-1\n");
else {
for(int i=1;i<=n;i++)printf("%d ",topo[i]);
printf("\n");
}
}
return 0;
}
边栏推荐
- C#/VB.NET:在 Word 中设置文本对齐方式
- 3-5年以上的功能测试如何进阶自动化?
- 【Idea series】idea configuration
- Advanced transcriptome analysis and R data visualization hot registration (2022.10)
- 从零开始Blazor Server(7)--使用Furion权限验证
- datax oracle to oracle incremental synchronization
- 命令模式(Command)
- 北京大学,新迎3位副校长!其中一人为中科院院士!
- MySql数据库入门的基本操作
- map的一道题目<单词识别>
猜你喜欢
随机推荐
拦截器,文件流,下载文件?
数据库对象-视图;存储过程
WPF 截图控件之画笔(八)「仿微信」
apache dolphin scheduler 文件dolphinscheduler-daemon.sh详解
[Flight Control Development Advanced Course 7] Crazy Shell Open Source Formation UAV - Formation Flight
123
Use pytest hook function to realize automatic test result push enterprise WeChat
你值得拥有的登录注册页面(附赠源码)
Xilinx VIVADO 中 DDR3(Naive)的使用(2)读写设计
datax oracle to oracle离线json文件
手搓一个“七夕限定”,用3D Engine 5分钟实现烟花绽放效果
SkiaSharp 之 WPF 自绘 粒子花园(案例版)
Learn to use the basic interface of set and map
Win11文件类型怎么改?Win11修改文件后缀的方法
单调栈一些题目练习
手搓一个“七夕限定”,用3D Engine 5分钟实现烟花绽放效果
Mysql高级篇学习总结14:子查询优化、排序优化、GROUP BY优化、分页查询优化
MySQL 45 讲 | 10 MySQL为什么有时候会选错索引?
中介者模式(Mediator)
知乎数据分析训练营









