UVA - 10305 Ordering Tasks(拓扑排序)
题意:给定优先关系进行拓扑排序。
分析:将入度为0的点加入优先队列,并将与之相连的点入度减1,若又有度数为0的点,继续加入优先队列,依次类推。
#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {, , -, , -, -, , };
const int dc[] = {-, , , , -, , -, };
const int MOD = 1e9 + ;
const double pi = acos(-1.0);
const double eps = 1e-;
const int MAXN = + ;
const int MAXT = + ;
using namespace std;
int in[MAXN];
vector<int> a[MAXN];
vector<int> ans;
priority_queue<int, vector<int>, greater<int> > q;
int main(){
int n, m;
while(scanf("%d%d", &n, &m) == ){
if(!n && !m) return ;
memset(in, , sizeof in);
ans.clear();
for(int i = ; i < MAXN; ++i) a[i].clear();
while(m--){
int x, y;
scanf("%d%d", &x, &y);
a[x].push_back(y);
++in[y];
}
for(int i = ; i <= n; ++i){
if(in[i] == ){
q.push(i);
}
}
while(!q.empty()){
int t = q.top();
q.pop();
ans.push_back(t);
int len = a[t].size();
for(int i = ; i < len; ++i){
if(--in[a[t][i]] == ){
q.push(a[t][i]);
}
}
}
int len = ans.size();
for(int i = ; i < len; ++i){
if(i) printf(" ");
printf("%d", ans[i]);
}
printf("\n");
}
return ;
}
UVA - 10305 Ordering Tasks(拓扑排序)的更多相关文章
- UVA.10305 Ordering Tasks (拓扑排序)
UVA.10305 Ordering Tasks 题意分析 详解请移步 算法学习 拓扑排序(TopSort) 拓扑排序的裸题 基本方法是,indegree表示入度表,vector存后继节点.在tops ...
- UVa 10305 - Ordering Tasks (拓扑排序裸题)
John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...
- Uva 10305 - Ordering Tasks 拓扑排序基础水题 队列和dfs实现
今天刚学的拓扑排序,大概搞懂后发现这题是赤裸裸的水题. 于是按自己想法敲了一遍,用queue做的,也就是Kahn算法,复杂度o(V+E),调完交上去,WA了... 于是检查了一遍又交了一发,还是WA. ...
- UVA 10305 Ordering Tasks(拓扑排序的队列解法)
题目链接: https://vjudge.net/problem/UVA-10305#author=goodlife2017 题目描述 John有n个任务,但是有些任务需要在做完另外一些任务后才能做. ...
- Ordering Tasks UVA - 10305 图的拓扑排序
John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...
- UVa 10305 Ordering Tasks (例题 6-15)
传送门: https://uva.onlinejudge.org/external/103/10305.pdf 拓扑排序(topological sort)简单题 自己代码的思路来自: ==> ...
- M - Ordering Tasks(拓扑排序)
M - Ordering Tasks Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Descri ...
- Ordering Tasks 拓扑排序
John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...
- uva 10305 ordering tasks(超级烂题)——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABHIAAAHDCAYAAABI5T2bAAAgAElEQVR4nOydPY7svLW1awQGNABHCm
随机推荐
- node指针
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 按钮:制作一个超小按钮
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- CTU Open Contest 2019 AB题
小菜鸡飘过 A: Beer Barrels 题意:给出四个整数:A,B,K,C,:A,B,C都是大于0的个位数,问在所有仅有A或者B组成的K位数中,数字C的个数是多少 思路: 1.先考虑特殊情况: ( ...
- boost::timer demo
#include <iostream> #include <boost/timer.hpp> //timer的头文件 using namespace boost; //打开bo ...
- WDSL文件中的XML元素
WDSL文件中的XML元素 理解起来其实很简单Types指定类型,当然是在后面的Message中需要的类型Message可以理解为函数中的参数,只不过如果一个函数如果有多个参数的时候应该吧这些参数定义 ...
- 多元线性回归算法python实现(非常经典)
对于多元线性回归算法,它对于数据集具有较好的可解释性,我们可以对比不过特征参数的输出系数的大小来判断它对数据的影响权重,进而对其中隐含的参数进行扩展和收集,提高整体训练数据的准确性.整体实现代码如下所 ...
- javac导出参考文档报错 错误: 编码GBK的不可映射字符”
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 按钮:按钮标签
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- P1069 微博转发抽奖
P1069 微博转发抽奖 转跳点:
- 201706 gem 'rails-erd'生成Model关系图
[工具]一张图理清各个model之间关系 安装 Graphviz 2.22+: 终端机中执行 brew install graphviz Gemfile中添加 gem 'rails-erd' 终端机中 ...