hdu 4857 逃生【反向拓扑排序】
2015-08-13 21:06
363 查看
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=4857
题意:中文不解释
解法:
input:
1
3 1
3 1
answer:
3 1 2
而不是
2 3 1
所以逆向建边 拓扑
代码:
http://acm.hdu.edu.cn/showproblem.php?pid=4857
题意:中文不解释
解法:
input:
1
3 1
3 1
answer:
3 1 2
而不是
2 3 1
所以逆向建边 拓扑
代码:
[code]#include<stdio.h> #include<iostream> #include<math.h> #include<stdlib.h> #include<ctype.h> #include<algorithm> #include<vector> #include<string> #include<queue> #include<stack> #include<set> #include<map> #include<string.h> using namespace std; int n, m; int a, b; int in[30010]; int ans[30010]; vector<int> g[30010]; struct cmp { bool operator()(int &a, int &b) { return a < b; } }; int main() { int t; scanf("%d",&t); while (t--) { scanf("%d%d",&n,&m); memset(in, 0, sizeof(in)); for (int i = 1; i <= n; i++) g[i].clear(); while (m--) { scanf("%d%d",&a,&b); g[b].push_back(a); in[a]++; } priority_queue<int,vector<int>,cmp> que; while (!que.empty()) que.pop(); for (int i = 1; i <= n; i++) if (in[i] == 0) que.push(i); int cnt = 0; while (!que.empty()) { int t = que.top(); que.pop(); ans[cnt++] = t; for (int i = 0; i < g[t].size(); i++) { in[g[t][i]] --; if (in[g[t][i]] == 0) que.push(g[t][i]); } } for (int i = cnt - 1; i >= 0; i--) { printf("%d",ans[i]); if (i == 0) printf("\n"); else printf(" "); } } return 0; }
相关文章推荐
- hdoj 5094 Maze 【BFS + 状态压缩】 【好多坑】
- GDOI2016模拟8.13总结
- 线程属性:pthread_addr_t
- 技能编辑器
- 127. Word Ladder
- 取消svn关联
- php上传文件到服务器 (注意权限)
- Exchange partition分区结构的“乾坤挪移”
- 排版软件Latex环境搭建
- LintCode-买卖股票的最佳时机
- G++/C++ 手动扩栈
- java方法传值还是传引用的问题
- 2015多校.Zero Escape (dp减枝 && 滚动数组)
- Git最快捷的方法. 自家用
- coj 1030:素数槽
- 【COCI 2012】Toy
- Objective-C 苹果开发文档 01 Introduction
- Application创建全局数组
- poj 3620 avoid the lakes(dfs)
- 菜鸟第四天:Linux文件管理命令详解