HDU 1285 确定比赛名次 拓扑排序模板题
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 确定比赛名次 拓扑排序模板题的更多相关文章
- HDU.1285 确定比赛名次 (拓扑排序 TopSort)
HDU.1285 确定比赛名次 (拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 只不过这道的额外要求是,输出字典序最小的那组解.那么解决方案就是 ...
- ACM: HDU 1285 确定比赛名次 - 拓扑排序
HDU 1285 确定比赛名次 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u De ...
- hdu 1285 确定比赛名次 拓扑排序
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛 ...
- hdu 1285 确定比赛名次 (拓扑)
确定比赛名次 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- HDOJ 1285 确定比赛名次(拓扑排序)
Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委 ...
- [ACM] hdu 1285 确定比赛 (拓扑排序)
确定比赛 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- HDU1285-确定比赛名次-拓扑排序板子题
有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接获得每个队的比赛成绩,只知道 ...
- HDU 1285 确定比赛名次(拓扑排序模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285 题目大意:有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行 ...
- 正向与反向拓扑排序的区别(hdu 1285 确定比赛名次和hdu 4857 逃生)
确定比赛名次 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submis ...
随机推荐
- Jquery实现兼容各大浏览器的Enter回车切换输入焦点的方法
<script type="text/javascript"> $('input:text:first').focus(); document.onkeydown = ...
- 第三章 Git的入门 - 读书笔记
Android驱动月考3 第三章 Git的入门 - 读书笔记 对于Github,这是全世界最大的开源平台,你可以把你做的项目在这里开源,把你发现的一些新技术在这里开源,向全世界的开发者们分享,大家都彼 ...
- Ajax跨域请求
客户端前台: //浏览器支持跨域访问,置于Ajax方法前$.support.cors = true; 服务器后台: //设置服务器端对于CORS的跨域请求支持,置于后台方法第一行 Response.A ...
- linux命令小常识
作为一个tester我们必须要会linux,也许你会说不用也可以.那么我想问,你部署测试环境在哪里,你下载war包并部署war包呢,你看日志在哪里? 基于测试需要用到liunx,我这里只针对需要用到的 ...
- ajax options
非同一域名的ajax post请求,浏览器会自动发送http options的请求检查是否允许跨域访问
- 【MongoDB】MongoDB 3.2 SCRAM-SHA-1验证方式
新版本已取消addUser方法,改使用createUser方法 官方地址:https://docs.mongodb.com/manual/tutorial/create-users/ 官方地址:htt ...
- Maven多环境打包
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- 在Spring下集成ActiveMQ
1.参考文献 Spring集成ActiveMQ配置 Spring JMS异步发收消息 ActiveMQ 2.环境 在前面的一篇ActiveMQ入门实例中我们实现了消息的异步传送,这篇博文将如何在spr ...
- sql评估期已过如何解决该问题
点击开始-所有程序-Microsoft SQL Server 2008-配置工具-SQL Server 安装中心然后点击左侧的维护,在点击右侧的版本升级,接着按照提示一直点下一步,到产品密钥的时候输入 ...
- LSTM 分类器笔记及Theano实现
相关讨论 http://tieba.baidu.com/p/3960350008 基于教程http://deeplearning.net/tutorial/lstm.html LSTM基本原理http ...