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的点轻.然后捏.输出每个点对应的重量.关键是要求.如果有多种可能性的话,输出让序号小的 ...
随机推荐
- TopShelf安装多实例
Topshelf 安装多实例命令: .\ConsoleApp1.exe -instance "newinstallname" install 多实例有一个好处就是容灾,当一个服务部 ...
- 详解URL的组成
很久很久以来,我对浏览器地址栏的信息了解的甚少,只知道域名(估计不知道是这么叫).唉...真是很汗颜啊!在软件专业都混了两年了,还是个菜鸟.说真的,有的时候觉得计算机这个领域真的真的有太多的东西要学了 ...
- nginx简单安装设置
1.Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器.Nginx是由Igor Sysoev为俄罗斯访问量第二 ...
- mongodb备份还原脚本
同步 echo off ,%-%date:~,%-%date:~,% set bak_dir=mongo_na_world_svn2win_%time_dir% set fromdb=db_erago ...
- 【.NET Core】ASP.NET Core之IdentityServer4(1):快速入门
[.NET Core]ASP.NET Core之IdentityServer4 本文中的IdentityServer4基于上节的jenkins 进行docker自动化部署. 使用了MariaDB,EF ...
- ArcticCore重构-VALIDATE_%
基于官方arc-stable-9c57d86f66be,AUTOSAR版本3.1.5 基本问题 Arctic Core中使用了VALIDATE, VALIDATE_RV, VALIDATE_NO_RV ...
- EOS技术研究:合约与数据库交互
智能合约操作链数据库是很常见的应用场景.EOS提供了专门的工具来做这件事(相当于Ethereum的leveldb),专业术语叫做持久化API,本文将完整严密地介绍这个工具以及对它的使用测试. 关键字: ...
- ORC文字识别软件破解版
下载地址:http://pan.baidu.com/s/1bnCiXdl 点击 然后可以免费用了ABBYY了!!
- restTemplate设置访问超时
(一)RestTemplate 客户端 1.RestTemplate 是Spring的封装,需要spring的包 spring-web-3.0.7.RELEASE.jar 2.客户端代码: /** * ...
- Spring Boot Favicon配置
http://blog.csdn.net/xiaolyuh123/article/details/72403226