poj 3687
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 今天学习拓扑排序 其实这就是求出出度为0的点 逆向的拓扑排序
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = ; int tu[maxn][maxn], out[maxn], ans[maxn];
int t, n, m;
int main() {
scanf("%d", &t);
while(t--) {
scanf("%d %d", &n, &m);
memset(ans, , sizeof(ans));
memset(tu, , sizeof(tu));
memset(out, , sizeof(out));
for (int i = ; i < m; i++ ) {
int a, b;
scanf("%d %d", &a, &b);
if (!tu[a][b]) {
tu[a][b] = ;
out[a]++;
}
}
int cnt = n;
priority_queue<int>q;
for (int i = ; i <= n ; i++)
if (!out[i]) q.push(i);
while(!q.empty()) {
int temp = q.top();
q.pop();
ans[temp] = cnt--;
for (int i = ; i <= n ; i++) {
if (tu[i][temp]) {
out[i]--;
if (!out[i]) q.push(i);
}
}
}
if (cnt > ) printf("-1\n");
else {
for (int i = ; i <= n ; i++)
printf("%d ", ans[i]);
printf("\n");
}
}
return ;
}
poj 3687的更多相关文章
- poj 3687(拓扑排序)
http://poj.org/problem?id=3687 题意:有一些球他们都有各自的重量,而且每个球的重量都不相同,现在,要给这些球贴标签.如果这些球没有限定条件说是哪个比哪个轻的话,那么默认的 ...
- Poj(3687),拓扑排序,
题目链接:http://poj.org/problem?id=3687 题意:n个重量为1~n的球,给定一些编号间的重量比较关系,现在给每个球编号,在符合条件的前提下使得编号小的球重量小.(先保证1号 ...
- poj 3687 Labeling Balls(拓扑排序)
题目:http://poj.org/problem?id=3687题意:n个重量为1~n的球,给定一些编号间的重量比较关系,现在给每个球编号,在符合条件的前提下使得编号小的球重量小.(先保证1号球最轻 ...
- POJ 3687 Labeling Balls 逆向建图,拓扑排序
题目链接: http://poj.org/problem?id=3687 要逆向建图,输入的时候要判重边,找入度为0的点的时候要从大到小循环,尽量让编号大的先入栈,输出的时候注意按编号的顺序输出重量, ...
- [ACM] POJ 3687 Labeling Balls (拓扑排序,反向生成端)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10161 Accepted: 2810 D ...
- poj 3687 Labeling Balls - 贪心 - 拓扑排序
Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 to N ...
- POJ 3687 Labeling Balls(反向拓扑+贪心思想!!!非常棒的一道题)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16100 Accepted: 4726 D ...
- POJ 3687:Labeling Balls(优先队列+拓扑排序)
id=3687">Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10178 Acc ...
- POJ 3687 Labeling Balls()
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9641 Accepted: 2636 Descri ...
- POJ 3687 逆序拓扑
额.题目大意:有N个球.编号和重量都是唯一不重复的.然后.给你m个pair a和b,表示编号为a的点一定比编号为b的点轻.然后捏.输出每个点对应的重量.关键是要求.如果有多种可能性的话,输出让序号小的 ...
随机推荐
- 第1章-Struts2 概述 --- Struts2和MVC
(一)Struts2和MVC的关系图: (1)控制器---FilterDispatcher 用户请求首先达到前段控制器(FilterDispatcher).FilterDispatcher负责根据用户 ...
- EJB 介绍
EJB 编辑 EJB是sun的服务器端组件模型,设计目标与核心应用是部署分布式应用程序.凭借java跨平台的优势,用EJB技术部署的分布式系统可以不限于特定的平台.EJB (Enterprise ...
- 初识JAVA——流程控制之if语句
if语句的流程控制主要分为3种:1,单分支结构:if(){……}: 2,双分支结构:if(){……}else{……}; 3,多分枝结构:if(){……}else if(){……}…… 其中作为if语句 ...
- 关于.net 保存 decimal类型数据到SQLServer2012数据库时自动取整的问题
公司同事问我有没有遇到过decimal类型数据入库时,会自动取整的问题(比如12.3入库后值是12,12.8入库后值是13,入库后自动四舍五入自动取整): 之前就遇到过从数据去decimal类型数据时 ...
- DDGScreenShot—图片擦除功能
写在前面 图片擦除功能,也是运用图片的绘制功能, 将图片绘制后,拿到相应的图片.当然,有一涨底图更明显 实现代码如下 /** ** 用手势擦除图片 - imageView --传图片 - bgView ...
- java之jsp实现动态网页
动态页面,说白了,就是根据一定的信息(条件)去改变呈现给用户的内容. 而这里所提到的一定的信息,通常就是指,在一个表单中用户所输入的信息. 先来看一个我们常见的用户登录界面吧. 在这里我们可以看到一共 ...
- 架构之微服务设计(Nginx + Upsync)
Upsync,微博开源基于Nginx容器动态流量管理方案 . Nginx 以其超高的性能与稳定性,在业界获得了广泛的使用,微博的七层就大量使用了 Nginx .结合 Nginx 的健康检查模块,以及动 ...
- CentOS6系列系统启动常见故障排查与解决方法
情景一.内核文件损坏 /boot/vmlinuz-2.6.32-642.el6.x86_64 内核文件 1.故障现象 2.解决方法:挂载光盘,进入rescue(救援)模式 3.选择--English- ...
- Xamarin.Android 使用百度地图获取定位信息
最近做一个项目,web端使用百度地图,PDA使用手持机自带的GPS定位系统获取经纬度,然后再百度地图上显示该经纬度会有一定距离的差异,这里就像可乐的瓶子拧上雪碧的盖子,能拧的上却不美观.所以为了数据的 ...
- SpringMVC--入门案例
一.SpringMVC介绍 SpringMVC和Struts都属于表现层框架, 是Spring的一部分,Spring的整体结构如下: 1.1 SpringMVC的处理流程 下图是SpringMVC的执 ...