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
随机推荐
- Educational Codeforces Round 69 (Rated for Div. 2)D(DP,思维)
#include<bits/stdc++.h>using namespace std;int a[300007];long long sum[300007],tmp[300007],mx[ ...
- 「Luogu P2824 [HEOI2016/TJOI2016]排序」
一道十分神奇的线段树题,做法十分的有趣. 前置芝士 线段树:一个十分基础的数据结构,在这道题中起了至关重要的作用. 一种基于01串的神奇的二分思想:在模拟赛中出现了这道题,可以先去做一下,这样可能有助 ...
- aws-ec2-upload
EC2上建立文件夹并开放权限 $ mkdir /home/upload$ chmod 777 /home/upload 本机文件 上传 到EC2服务器$ scp -i awsub01.pem noip ...
- 61二叉搜索树的第k个结点
题目描述 给定一颗二叉搜索树,请找出其中的第k大的结点.例如, 5 / \ 3 7 /\ /\ 2 4 6 8 中,按结点数值大小顺序第三个结点的值为4. 思路:二叉搜索树的中序遍历是递增的序列,使用 ...
- 让UITextField全选
[textField selectAll:self];
- [蓝桥杯2015决赛]四阶幻方(DFS + 剪枝)
题目描述 把1~16的数字填入4x4的方格中,使得行.列以及两个对角线的和都相等,满足这样的特征时称为:四阶幻方. 四阶幻方可能有很多方案.如果固定左上角为1,请计算一共有多少种方案. 比如: 1 ...
- 更改windows系统的快捷键方法
众所周知,windows平台有很多快捷键使用非常的别扭. 现在提供windows 平台快捷键替换的绝佳软件:autohotkey 下载链接:http://ahkscript.org/ 中文帮助站点:h ...
- 契约精神、尊重规则、SOP,对制造业来说是蜜糖还是毒药?
契约精神.尊重规则.执行SOP,这些词儿都天然带有光环,也有很多相关的好故事,全球范围内的企业家都对其推崇备至,摆出一副虔诚教徒的模样,事实上,缺乏契约精神.不遵守规则比之缺乏资本.丢掉订单更加不可接 ...
- 如何使用IDEA将maven项目打成war包
1.点击IDEA界面右边的maven project 2.双击package
- Object.getOwnPropertyDescriptors()
Object.getOwnPropertyDescriptors() 前面说过,Object.getOwnPropertyDescriptor方法会返回某个对象属性的描述对象(descriptor). ...