http://acm.hdu.edu.cn/showproblem.php?pid=1285

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
int n, m;
const int maxn = + ;
struct node {
int u, v;
int tonext;
}e[maxn * ];
int first[maxn];
int num;
int in[maxn];
void add(int u, int v) {
++num;
e[num].u = u;
e[num].v = v;
e[num].tonext = first[u];
first[u] = num;
}
struct DAG {
int cur;
bool operator < (const struct DAG & rhs) const {
return cur > rhs.cur;
}
DAG(int ccur) : cur(ccur) {};
};
vector<int>ans;
priority_queue<struct DAG> que;
bool DAG_sort() {
for (int i = ; i <= n; ++i) { //节点号小的在前
if (in[i] == ) que.push(DAG(i));
}
while (!que.empty()) {
int cur = que.top().cur;
que.pop();
ans.push_back(cur);
for (int i = first[cur]; i; i = e[i].tonext) {
int v = e[i].v;
in[v]--;
if (in[v] == ) que.push(DAG(v));
}
}
if (ans.size() < n) return false; //有环
return true;
}
void init() {
num = ;
memset(first, , sizeof first);
ans.clear();
memset(in, , sizeof in);
}
void work() {
init();
for (int i = ; i <= m; ++i) {
int u, v;
scanf("%d%d", &u, &v);
add(u, v);
in[v]++;
}
DAG_sort();
for (int i = ; i < ans.size() - ; ++i) {
printf("%d ", ans[i]);
}
printf("%d\n", ans.back());
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
while (scanf("%d%d", &n, &m) != EOF) work();
return ;
}

HDU 1285 确定比赛名次 拓扑排序模板题的更多相关文章

  1. HDU.1285 确定比赛名次 (拓扑排序 TopSort)

    HDU.1285 确定比赛名次 (拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 只不过这道的额外要求是,输出字典序最小的那组解.那么解决方案就是 ...

  2. ACM: HDU 1285 确定比赛名次 - 拓扑排序

     HDU 1285 确定比赛名次 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u De ...

  3. hdu 1285 确定比赛名次 拓扑排序

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛 ...

  4. hdu 1285 确定比赛名次 (拓扑)

    确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  5. HDOJ 1285 确定比赛名次(拓扑排序)

    Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委 ...

  6. [ACM] hdu 1285 确定比赛 (拓扑排序)

    确定比赛 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  7. HDU1285-确定比赛名次-拓扑排序板子题

    有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接获得每个队的比赛成绩,只知道 ...

  8. HDU 1285 确定比赛名次(拓扑排序模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285 题目大意:有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行 ...

  9. 正向与反向拓扑排序的区别(hdu 1285 确定比赛名次和hdu 4857 逃生)

    确定比赛名次 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

随机推荐

  1. 在Linux上编写C#程序

    自从C#开源之后,在Linux编写C#程序就成了可能.Mono-project就是开源版本的C#维护项目.在Linux平台上使用的C#开发工具为monodevelop.安装方式如下: 首先需要安装一些 ...

  2. Xcode磁盘空间大清理

    我的设备是Macbook Air 13' Mid 2011,128G SSD.最近开始有些存储压力了,用Clean My Mac清理一部分旧文件后,决定对Xcode动手. 移除对旧设备的支持 影响:可 ...

  3. 实现类似QQ对话聊天功能脚本

    var skin : GUISkin; var showChat = false;private var inputField = "";private var display = ...

  4. iOS开发 GET、POST请求方法(NSURLConnection篇)

    Web Service使用的主要协议是HTTP协议,即超文本传输协议. HTTP/1.1协议共定义了8种请求方法(OPTIONS.HEAD.GET.POST.PUT.DELETE.TRACE.CONN ...

  5. Eclipse使用tomcat的原理

    1. 当我们使用Eclipse将项目部署到Tomcat的时,我们发现,在Tomcat下的webapps目录下并没有我们创建好的项目,但是当通过Eclipse启动服务器后,项目却真的可以访问到,这是为什 ...

  6. (转)对《30个提高Web程序执行效率的好经验》的理解

    阅读了博客园发布的IT文章<30个提高Web程序执行效率的好经验>,这30条准则对我们web开发是非常有用的,不过大家可能对其中的一些准则是知其然而不知其所以然. 下面是我对这些准则的理解 ...

  7. 转 Selenium+Python+Eclipse网页自动化集成环境配置(附简单的测试程序)

    1 JDK.Python环境变量配置  下载JDK http://www.oracle.com/technetwork/java/javase/downloads/index.html,直接双击安装, ...

  8. Foundation ----->NSArray

    .数组的创建     //注意:在OC的数组中,只能够存放对象 //    NSArray *array = [NSArray arrayWithObject:12];错误          //创建 ...

  9. java8之接口增强

    Java8是由oracle公司于2014年3月正式发布,它是继java5 以来最重要的发布版本.Java8包含了很多新的特性,可以简化开发,提升代码的可读性. Java8的一些重要新特性包括: l 接 ...

  10. Eclipse 4.2 (Juno) 'Cannot create a server using the selected type' in Tomcat 7 - Stack Overflow.html

    [太神奇了,真的可以呀] 原文:http://stackoverflow.com/questions/13423593/eclipse-4-2-juno-cannot-create-a-server- ...