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. WPF 绑定的校验

    为进行校验,必须准备一个RangeValidationRule类,该类继承自ValidationRule 该类实现如下: class RangeValidationRule : ValidationR ...

  2. Unreal Engine 4 一些小技巧或提示

    怎样获取当前地图的名字 在任意Actor类里,GetWorld()->GetName()就可以获得当前地图的名字

  3. 使用Visual Assistant X创建C程序注释模板

    本文将讲解C程序注释模板的使用背景.创建方法,并在结束时进行总结. 1.使用背景 在项目开发过程中,为方便组内其他成员能够快速学习自己编写的代码,需要对自己写的函数添加注释.在正规的软件开发流程中,一 ...

  4. 如何用Visual Studio 2013 (vs2013)编写C语言程序 (转)

    http://blog.sina.com.cn/s/blog_964ee6730101jvvi.html

  5. JavaScript能干什么?

    真的是长见识了,JavaScript居然能做除了WEB界面外运行于任何平台的APP,而且可以做服务器端,还有天理和王法吗?JavaScript到底有多神奇,要不要重新上路,老程序员何去何从,自然要先网 ...

  6. Git self-learning

    ---恢复内容开始--- 使用后的总结 git config --global user.name "" #设置和查看用户名git config --global user.ema ...

  7. 火币网api的nodejs实现

    var request = require('request'); var crypto = require('crypto'); var config = { api_url: 'https://a ...

  8. Android7.0 新特性

    删除了三个隐式广播 1.监听网络变化的广播 CONNECTIVITY_ACTION,在Manifest.xml中注册的 不会受到,在前台运行的程序 registerReceiver()注册的广播可以接 ...

  9. MFC创建文件和文件夹

    1.使用PathIsDirectory判断文件夹是否存在需要引用下面头文件: #include "shlwapi.h"#pragma comment(lib,"shlwa ...

  10. 解决ASP.Net第一次访问慢的处理(IIS8)

    本篇经验以IIS8,Windows Server 2012R2做为案例. IIS8 运行在 Windows Server 2012 and Windows 8 版本以上的平台上. IIS中应用程序池和 ...