POJ-3687 Labeling Balls
来源:互联网 发布:电商推荐算法 编辑:程序博客网 时间:2024/05/17 11:56
题目链接:https://vjudge.net/problem/POJ-3687
这是一道拓扑排序的题,但是要求编号小的优先质量小,刚开始没理解,后来明白了
就是把每个球的质量按顺序写出来字典序最小,而不是球的质量大小顺序字典序最小
理解了之后就好做了,还要注意一个坑,可能会有重复的a,b出现,这时入度不能加一
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;const int N=200+10;bool g[N][N];int n,m,t;int ind[N],topo[N];bool topsort(){ for(int i=n;i>=1;i--){ int k; for(k=n;k>=1;k--) if(ind[k]==0){ topo[k]=i; ind[k]--; break; } if(k<1) return false; for(int j=1;j<=n;j++) if(g[k][j]) ind[j]--; } return true;}int main(){int T,a,b;scanf("%d",&T);while(T--){memset(g,false,sizeof(g));memset(ind,0,sizeof(ind));scanf("%d%d",&n,&m);for(int i=0;i<m;i++){scanf("%d%d",&a,&b);if(!g[b][a]) //看有没有重复的 {g[b][a]=true;ind[a]++;}}if(topsort()){for(int i=1;i<n;i++)printf("%d ",topo[i]);printf("%d\n",topo[n]);}else printf("-1\n");}return 0;}
阅读全文
0 0
- poj 3687 Labeling Balls
- POJ 3687 Labeling Balls
- poj 3687 Labeling Balls
- POJ 3687 Labeling Balls
- poj-3687-Labeling Balls
- POJ 3687 Labeling Balls
- POJ 3687 Labeling Balls
- POJ 3687 Labeling Balls
- poj 3687 Labeling Balls
- POJ 3687 Labeling Balls
- poj 3687 Labeling Balls
- poj 3687 Labeling Balls
- poj 3687 Labeling Balls
- POJ 3687 Labeling Balls
- poj 3687 Labeling Balls
- poj 3687 Labeling Balls
- poj 3687 Labeling Balls
- POJ【3687】Labeling Balls
- leetcode 11. Container With Most Water
- adb介绍以及常用命令
- Angular4.X 报错: Can't bind to 'ngModel' since it isn't a known property of 'input'
- java获取线程的返回值
- spring学习笔记叁——spring的bean注解
- POJ-3687 Labeling Balls
- IntelliJ IDEA 提交代码到 GitHub
- 智能指针
- 用户的浏览商品记录功能代码
- Python 爬虫实战案例 : 微信公众号的爬取
- android之自定义简单不平凡的对话框
- Spring Cloud与Spring Boot微服务实战
- TensorFlow:ImportError: libcudnn.so.*: cannot open shared object file: No such file or dictionary
- PAT 1072. 开学寄语