题意:给出n个人,m个轻重关系,求满足给出的轻重关系的并且满足编号小的尽量在前面的序列

因为输入的是a比b重,但是我们要找的是更轻的,所以需要逆向建图

逆向建图参看的这一篇http://blog.csdn.net/scf0920/article/details/28108243

然后用优先队列来实现的参看的这一篇

http://ycool.com/post/u9ahrwg#algo3

 #include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<stack>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#define mod=1e9+7;
using namespace std; typedef long long LL;
const int maxn=;
int g[maxn][maxn],out[maxn],ans[maxn]; int main(){
int t,n,m,u,v,tmp,i,j;
scanf("%d",&t);
while(t--){
memset(g,,sizeof(g));
memset(out,,sizeof(out));
cin>>n>>m;
for(i=;i<m;i++){
cin>>u>>v;
if(!g[u][v]){
g[u][v]=;
out[u]++;
}
}
priority_queue<int> q;
for(i=;i<=n;i++){
if(out[i]==) q.push(i);
} for(i=n;i>=;i--){
if(q.empty()) break;
u=q.top();q.pop();//取出队列中编号最大的数
ans[u]=i;//使得大的数尽量往后面放
for(j=;j<=n;j++){
if(g[j][u]){
g[j][u]=;
out[j]--;
if(out[j]==) q.push(j);
} }
} if(i) printf("-1\n");
else{
printf("%d",ans[]);
for(i=;i<=n;i++) printf(" %d",ans[i]);
printf("\n");
}
}
return ;
}

go---go----寒假学的拓扑排序都忘得七七八八的说了= =这一题的反向建图再好好理解

POJ 3687 Labeling Balls【拓扑排序 优先队列】的更多相关文章

  1. [ACM] POJ 3687 Labeling Balls (拓扑排序,反向生成端)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10161   Accepted: 2810 D ...

  2. poj 3687 Labeling Balls(拓扑排序)

    题目:http://poj.org/problem?id=3687题意:n个重量为1~n的球,给定一些编号间的重量比较关系,现在给每个球编号,在符合条件的前提下使得编号小的球重量小.(先保证1号球最轻 ...

  3. PKU 3687 Labeling Balls(拓扑排序)

    题目大意:原题链接 给出N个未编号的质量各不相同的球,以及它们质量轻重的大小关系,给它们从1-N贴标签编号,无重复.问是否存在可行的编号方法,不存在输出-1, 如果存在则输出唯一一种方案,此方案是使得 ...

  4. 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 ...

  5. POJ 3687 Labeling Balls(反向拓扑+贪心思想!!!非常棒的一道题)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16100   Accepted: 4726 D ...

  6. poj 3687 Labeling Balls【反向拓扑】

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12246   Accepted: 3508 D ...

  7. poj 3687 Labeling Balls(拓补排序)

    Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them ...

  8. POJ 3687 Labeling Balls (top 排序)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15792   Accepted: 4630 D ...

  9. POJ - 3687 Labeling Balls (拓扑)

    (点击此处查看原题) 题意 此处有n盏灯,编号为1~n,每盏灯的亮度都是唯一的,且在1~n范围之间,现已知m对灯之间的关系:a b ,说明灯a的亮度比灯b小,求出每盏灯的亮度,要求字典序最小(编号小的 ...

随机推荐

  1. IntelliJ IDEA 比较当前版本文件与历史文件

    前言: 写代码修改后怎样比较与历史文件的区别呢?idea提供了2种比较方式(目前笔者所了解到的) 一.SVN的版本比较 二.当前文件与历史版本比较

  2. 【BZOJ】【1324】王者之剑

    网络流/二分图最大点权独立集 Amber(胡伯涛)论文<最小割模型在信息学竞赛中的应用>中的例题…… 感觉这个好神啊,果然是一切皆为网络流……这转化太神奇了 /************** ...

  3. VS2010 创建WindowsService服务

    1.新建一个Windows 服务 2.添加Installer 这一步很重要,在处理完你的业务逻辑后需要添加一个Installer才能是你的Windows服务被安装. 在VS中添加Installer 右 ...

  4. 提升SQL Server速度整理索引碎片

    转载:http://wenku.baidu.com/view/f64c8a707fd5360cba1adbea.html SQL Server2005索引碎片分析和解决方法   毫无疑问,给表添加索引 ...

  5. tomcat 解析(一)-文件解析

    做web项目,最常用的服务器就是Apache的tomcat.虽然一直在用tomcat,但都是仅限在使用的阶段,一直没有深入学习过.想深入学习tomcat,首推的肯定是官网:http://tomcat. ...

  6. model对象之setter方法使用,解决去除空格和将数字转成字符串展示方法

    1.系统中手机号注册的时候,不能含有前后空格.在model对象中过滤~! private String mobile; public String getMobile() { return mobil ...

  7. Nginx正确记录post日志的方法

    Nginx正确记录post日志的方法 事实上可以很简单,这取决于把 access_log 放在哪个 location 里面. 一,放到包含fastcgi_pass或proxy_pass的Locatio ...

  8. Python-aiohttp百万并发

    http://www.aikaiyuan.com/10935.html 本文将测试python aiohttp的极限,同时测试其性能表现,以分钟发起请求数作为指标.大家都知道,当应用到网络操作时,异步 ...

  9. SOAP vs REST

    Both methods are used by many of the large players. It's a matter of preference. My preference is RE ...

  10. http://www.yihaomen.com/article/java/302.htm

    http://www.yihaomen.com/article/java/302.htm