uva10305 拓扑排序
2016-07-26 16:56
399 查看
John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task isonly possible if other tasks have already been executed.
题干很短,意思很简单,拓扑排序。
我的方法是用队列做,将入度为零的点进队,每次出队更新入度,继续将入度为零的点进队。
就不贴出来了
题干很短,意思很简单,拓扑排序。
我的方法是用队列做,将入度为零的点进队,每次出队更新入度,继续将入度为零的点进队。
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> #include <string> #include <vector> #include <queue> using namespace std; typedef long long ll; vector<int> mp[105]; vector<int> ans; int ru[105]; int n,m; void init() { for(int i=0; i<=n; ++i) { mp[i].clear(); } memset(ru, 0, sizeof(ru)); } int main() { int x,y; while(scanf("%d %d",&n,&m)&&(n||m)) { init(); ans.clear(); queue<int> q; for(int i=0;i<m;i++) { scanf("%d %d",&x,&y); mp[x].push_back(y); ++ru[y]; } for(int i=1; i<=n; i++) if(!ru[i]) q.push(i); while(!q.empty()) { int t=q.front(); q.pop(); ans.push_back(t); for(int i=0;i<mp[t].size();i++) { --ru[mp[t][i]]; if(ru[mp[t][i]]==0) q.push(mp[t][i]); } } printf("%d",ans[0]); for(int i=1;i<ans.size();i++) { printf(" %d",ans[i]); } printf("\n"); } }貌似还有一种用dfs做的方法,思路也蛮清晰的
就不贴出来了
相关文章推荐
- 详解图的应用(最小生成树、拓扑排序、关键路径、最短路径)
- 简单的四则运算
- 数的奇偶性
- ACMer博客瀑布流分析
- ACM程序设计大赛题目分类
- 2015年acm国内排名
- 计算字符串最后一个单词长度
- ACM网址
- 1272 小希的迷宫
- 1272 小希的迷宫
- hdu 1250 大数相加并用数组储存
- 矩阵的乘法操作
- 蚂蚁爬行问题
- 蚂蚁爬行问题
- 求两个数的最大公约数【ACM基础题】
- 打印出二进制中所有1的位置
- 杭电题目---一只小蜜蜂
- 初学图论-Kahn拓扑排序算法(Kahn's Topological Sort Algorithm)
- HDOJ 1002 A + B Problem II (Big Numbers Addition)
- 初学ACM - 半数集(Half Set)问题 NOJ 1010 / FOJ 1207