当前位置:网站首页>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;
}
边栏推荐
- 剑指长城炮? 长安全新皮卡官方谍照
- 音频编辑 合唱
- Graphic and text hands-on tutorial--ESP32 MQTT docking EMQX local server (VSCODE+ESP-IDF)
- 使用函数
- Business collocations
- 中介者模式(Mediator)
- datax oracle to oracle incremental synchronization
- What is the principle of thermal imaging temperature measurement?Do you know?
- 临床研究方法学,到现场,到数据真实发生的地方 | 对话数智 x 张维拓
- *iframe*
猜你喜欢

Disc burning steps

【机器学习】:如何对你的数据进行分类?

map的一道题目<单词识别>

北京大学,新迎3位副校长!其中一人为中科院院士!

ESP8266-Arduino编程实例-MQ3酒精传感器驱动
![[Flight Control Development Advanced Course 7] Crazy Shell Open Source Formation UAV - Formation Flight](/img/58/19a50af5e187df0f37a1a3298c029b.png)
[Flight Control Development Advanced Course 7] Crazy Shell Open Source Formation UAV - Formation Flight

The use of DDR3 (Naive) in Xilinx VIVADO (1) to create an IP core

【Qt】解决 “由于找不到Qt5Cored.dll,无法继续执行代码”(亲测有效)

Win11 file types, how to change?Win11 modify the file suffix

萌宠来袭,如何让“吸猫撸狗”更有保障?
随机推荐
map的一道题目<单词识别>
从零开始Blazor Server(7)--使用Furion权限验证
Learn to use the basic interface of set and map
喂,你知道节流是什么吗?
A topic of map
*iframe*
音频编辑 合唱
3-5年以上的功能测试如何进阶自动化?
【Idea series】idea configuration
使用json-server快速搭建本地数据接口
Graphical Hands-on Tutorial--ESP32 OTA Over-the-Air Upgrade (VSCODE+IDF)
【LeetCode】232.用栈实现队列
Maple 2022 software installation package download and installation tutorial
子查询
Oracle中对临时表空间执行shrink操作
中介者模式(Mediator)
Using .NET to simply implement a high-performance clone of Redis (2)
【黄啊码】MySQL入门—2、使用数据定义语言(DDL)操作数据库
Leetcode brush questions - 543. Diameter of binary trees, 617. Merging binary trees (recursive solution)
Camunda overall architecture and related concepts