当前位置:网站首页>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;
}
边栏推荐
- Leetcode brush questions - 543. Diameter of binary trees, 617. Merging binary trees (recursive solution)
- 数字知识库及考学一体化平台
- 【LeetCode】700.二叉搜索树
- Mysql数据类型
- Camunda overall architecture and related concepts
- *W3C* Standards Organization
- What is the principle of thermal imaging temperature measurement?Do you know?
- WPF 截图控件之画笔(八)「仿微信」
- 【LeetCode】701.二叉搜索树中的插入操作
- MySQL不提供数组,只能做成表吗?
猜你喜欢

What is the terminal privilege management

秒云成功入选《2022爱分析 · 银行数字化厂商全景报告》,智能运维能力获认可

入门MySql表的增删查改

Win11怎么重装显卡驱动程序?Win11显卡驱动怎么卸载重装?

【虹科案例】基于3D相机组装家具

audio_policy_configuration.xml配置文件详解

God Space - the world's first Web3.0-based art agreement creative platform, broadening the boundaries of multi-art integration

MySql数据库入门的基本操作

Zikko上市同时搭载HDMI2.1和2.5GbE新款雷电4扩展坞

数据库对象
随机推荐
*W3C* 标准组织
Leetcode brush questions - binary search tree related topics (98. Verify binary search tree, 235. The nearest common ancestor of binary search tree, 1038. From binary search tree to bigger sum tree, 5
God Space - the world's first Web3.0-based art agreement creative platform, broadening the boundaries of multi-art integration
【无标题】
【LeetCode】701.二叉搜索树中的插入操作
cat /proc/kallsyms found that the kernel symbol table values are all 0
关于架构的思考
*SEO*
浅析深度学习在图像处理中的应用趋势及常见技巧
[Hongke case] Assembling furniture based on 3D camera
*W3C* Standards Organization
Advanced transcriptome analysis and R data visualization hot registration (2022.10)
Graphic and text hands-on tutorial--ESP32 MQTT docking EMQX local server (VSCODE+ESP-IDF)
你值得拥有的登录注册页面(附赠源码)
MATLAB程序设计与应用 3.1 特殊矩阵
【LeetCode】232.用栈实现队列
Camunda overall architecture and related concepts
Xilinx VIVADO 中 DDR3(Naive)的使用(3)仿真测试
Zikko上市同时搭载HDMI2.1和2.5GbE新款雷电4扩展坞
Jenkins User Manual (1) - Software Installation