当前位置:网站首页>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;
}
边栏推荐
- Zikko launches new Thunderbolt 4 docking station with both HDMI2.1 and 2.5GbE
- Graphical Hands-on Tutorial--ESP32 OTA Over-the-Air Upgrade (VSCODE+IDF)
- ORB-SLAM3中的优化
- 将博客搬至CSDN
- 深度学习100例 —— 卷积神经网络(CNN)天气识别
- 【LeetCode】98.验证二叉搜索树
- map的一道题目<单词识别>
- Mysql高级篇学习总结14:子查询优化、排序优化、GROUP BY优化、分页查询优化
- 【Idea series】idea configuration
- Disc burning steps
猜你喜欢
随机推荐
Zhihu Data Analysis Training Camp
你知道吗?那些专属于代码的浪漫~
Redis查询缓存
mongo-导出数据到mysql
[Hongke case] Assembling furniture based on 3D camera
Leetcode刷题——路径总和
linux下数据库初始化密码
cubemx stm32 afm3000 module gas flow sensor driver code
Leetcode刷题——构造二叉树(105. 从前序与中序遍历序列构造二叉树、106. 从中序与后序遍历序列构造二叉树)
MySQL最大建议行数2000w, 靠谱吗?
六石编程学:编程中的直线思维与自然思维
浅析深度学习在图像处理中的应用趋势及常见技巧
【黄啊码】MySQL入门—2、使用数据定义语言(DDL)操作数据库
少即是多:视觉SLAM的点稀疏化(IROS 2022)
手搓一个“七夕限定”,用3D Engine 5分钟实现烟花绽放效果
*W3C* 标准组织
小程序实战(三) - head组件的封装与使用
datax oracle to oracle离线json文件
『快速入门electron』之实现窗口拖拽
【LeetCode】899.有序队列









