//并不理解为什么需要反向建图,由大到小倒序确定排名。感觉正向由小到大和反向由大到小应该是一样的。

解:拓排+贪心,反向建边,先找排名靠后的(now,不知道为什么)

 #include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<vector>
#include<map>
#include<stack>
#include<string> using namespace std; vector <int> G[];
int T,n,m;
int a[],b[];
int d[];
int now; int main(){
scanf("%d",&T);
for (int cas=;cas<T;cas++){
scanf("%d%d",&n,&m);
for (int i=;i<=n;i++){
G[i].clear();
d[i]=;
}
for (int i=;i<m;i++){
int x,y;
scanf("%d%d",&x,&y);
d[x]++;
G[y].push_back(x);
}
now=n;
for (int i=;i<n;i++){
for (int j=n;j>=;j--){
if (d[j]==){
d[j]=-;
a[now--]=j;
int sz=G[j].size();
for (int k=;k<sz;k++){
d[G[j][k]]--;
}
break;
}
}
}
if (now!=){
printf("-1\n");
continue;
}
for (int i=;i<=n;i++){
b[a[i]]=i;
}
for (int i=;i<=n;i++){
printf("%d%c",b[i],i==n?'\n':' ');
}
}
return ;
}
/*
1
6 5
6 1
1 5
4 5
2 4
3 4 5
4 0
4 1
1 1
4 2
1 2
2 1
4 1
2 1
4 1
3 2
*/

cdoj 排名表 拓扑排序 排名输出 贪心的更多相关文章

  1. cdoj 1150 排名表 拓扑排序

    排名表 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/1150 Descrip ...

  2. HDU-4857 逃生(反向拓扑排序 + 逆向输出)

    逃生 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...

  3. hdu 4857 逆向拓扑排序+反向输出

    /*一组测试实例 4 4 2 3 1 2 4 */ #include<stdio.h> #include<string.h> #include<queue> usi ...

  4. Problem 1014 xxx游戏 暴力+拓扑排序

    题目链接: 题目 Problem 1014 xxx游戏 Time Limit: 1000 mSec Memory Limit : 32768 KB 问题描述 小M最近很喜欢玩XXX游戏.这个游戏很简单 ...

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

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

  6. hdu 3342 Legal or Not(拓扑排序) HDOJ Monthly Contest – 2010.03.06

    一道极其水的拓扑排序……但是我还是要把它发出来,原因很简单,连错12次…… 题意也很裸,前面的废话不用看,直接看输入 输入n, m表示从0到n-1共n个人,有m组关系 截下来m组,每组输入a, b表示 ...

  7. poj 3683(2-sat+拓扑排序)

    Priest John's Busiest Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11127   Accep ...

  8. POJ3687 Labeling Balls(拓扑排序\贪心+Floyd)

    题目是要给n个重量1到n的球编号,有一些约束条件:编号A的球重量要小于编号B的重量,最后就是要输出字典序最小的从1到n各个编号的球的重量. 正向拓扑排序,取最小编号给最小编号是不行的,不举出个例子真的 ...

  9. hdu-5695 Gym Class(贪心+拓扑排序)

    题目链接: Gym Class Time Limit: 6000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) ...

随机推荐

  1. Java GC 日志详解(转)

    Java GC日志可以通过 +PrintGCDetails开启 以ParallelGC为例 YoungGC日志解释如下(图片源地址:这里) : FullGC(图片源地址:这里): http://blo ...

  2. 快速配置 Samba 将 Linux 目录映射为 Windows 驱动器

    原文链接 samba client ubuntu redhat ubuntu gui tools 1,列出某个IP地址所提供的共享文件夹 smbclient -L 198.168.0.1   2,在s ...

  3. [转]openlayer+geoserver实现WFS操作

    From:http://liushaobo2005.blog.163.com/blog/static/253056702011541462372/ wfs是OGC的标准规范,主要用于提供对矢量地理数据 ...

  4. get方式中文乱码问题

    <a target="_blank" href="ftpFileAction!downloadFile.action?filename=测试.xml"&g ...

  5. GetBuffer与ReleaseBuffer的用法,CString剖析

    转载: http://blog.pfan.cn/xman/43212.html GetBuffer()主要作用是将字符串的缓冲区长度锁定,releaseBuffer则是解除锁定,使得CString对象 ...

  6. spring boot + velocity中文乱码解决方式

    在application.properties文件中,加入如下配置: spring.velocity.properties.input.encoding=UTF-8spring.velocity.pr ...

  7. 何使用CSS写出一个下拉菜单。

    导航菜单是每个网站所必备的功能,也是每个学习制作网站的朋友所必须接触的,如何用css样式制作一个简单漂亮的二级下拉菜单呢? 下面为大家分享一下我的经验 方法步骤: 第一步  : 首页我们打开Subli ...

  8. javascript——touch事件介绍与实例演示

      分类: javascript2014-02-12 16:42 1742人阅读 评论(0) 收藏 举报 touch事件touchmovetouchstarttouchend 前言 诸如智能手机和平板 ...

  9. UILabel头文件常见属性

    text : default is nil 文本属性,默认值是 nil @property(nullable, nonatomic,copy) NSString *text; font : defau ...

  10. HTML6注册表单输入日志标题

    一.找到元素. var d = document.getElementById("") var d = document.getElementsByName("" ...