当前位置:网站首页>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刷题——路径总和

Graphic and text hands-on tutorial--ESP32 MQTT docking EMQX local server (VSCODE+ESP-IDF)

Leetcode——利用先序遍历特性完成114. 二叉树展开为链表

*iframe*

化繁为简!阿里新产亿级流量系统设计核心原理高级笔记(终极版)

面试蚂蚁(P7)竟被MySQL难倒,奋发图强后二次面试入职蚂蚁金服

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

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

多行函数;group_by分组;having分组后筛选;单表查询总结

Advanced transcriptome analysis and R data visualization hot registration (2022.10)
随机推荐
强烈推荐一款优秀且通用的后台管理系统
Redis查询缓存
Events in August | 51CTO's 17th Anniversary Celebration, post a blog post to get gifts such as tea sets/notebooks/T-shirts!
使用函数
cubemx stm32 afm3000 module gas flow sensor driver code
mongo-导出数据到mysql
化繁为简!阿里新产亿级流量系统设计核心原理高级笔记(终极版)
DB2查看执行过长的SQL
ECCV 2022 | 清华&腾讯AI Lab提出REALY: 重新思考3D人脸重建的评估方法
datax oracle to oracle incremental synchronization
datax oracle to oracle离线json文件
Rust 从入门到精通04-变量
使用json-server快速搭建本地数据接口
浅析深度学习在图像处理中的应用趋势及常见技巧
map的一道题目<单词识别>
Mysql高级篇学习总结13:多表连接查询语句优化方法(带join语句)
从零开始Blazor Server(7)--使用Furion权限验证
遍历Map的四种方法
音频编辑 合唱
少即是多:视觉SLAM的点稀疏化(IROS 2022)