uva10305 拓扑排序
来源:互联网 发布:魔兽世界7.0掉落数据库 编辑:程序博客网 时间:2024/05/22 12:52
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做的方法,思路也蛮清晰的
就不贴出来了
0 0
- UVa10305拓扑排序
- 拓扑排序 uva10305
- UVA10305拓扑排序
- uva10305 拓扑排序
- uva10305-拓扑排序
- uva10305 拓扑排序
- Uva10305-拓扑排序
- uva10305 拓扑排序经典dfs
- UVA10305 Ordering Tasks 拓扑排序
- Uva10305 Ordering Tasks(拓扑排序)
- UVA10305 Ordering Tasks(拓扑排序)
- uva10305(拓扑排序dfs)
- UVA10305 Ordering Tasks【DFS】【拓扑排序】
- UVa10305 - Ordering Tasks拓扑排序 天书啊
- Uva10305 - Ordering Tasks(拓扑排序经典例题)
- uva10305- Ordering Tasks(拓扑排序)
- uva10305(拓扑
- uva10305--拓扑
- Centos端口转发
- js刷新页面方法大全
- Intellij 强制更新快照版jar包
- 转载:TreeSet详解
- 路径问题系列之 POJ 1915 Knights Moves(BFS)
- uva10305 拓扑排序
- 大多数人不了解HTML5时代的网页设计
- react-native ScrollView的简单应用
- 月相的研究
- 注册 Gmail,验证手机号码的时候提示“此号码不能用于验证”,怎么解决?
- GetHashCode 要你到底何用
- oracle 子父表关联查询以父表作为查询分页对象
- lua cocos2dx UIManager
- STM32-待机唤醒实验