UVa 10305 Ordering Tasks【拓扑排序】
题意:给出n件事情,m个二元组关系,求它们的拓扑序列
用的队列来做
#include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
using namespace std; #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i) typedef long long LL;
const int INF = (<<)-;
const int mod=;
const int maxn=;
int ecnt;
int n,m; int g[][],in[],ans[maxn]; void toposort(){
queue<int> q; for(int i=;i<=n;i++)
if(in[i]==) q.push(i); int cnt=;
while(!q.empty()){
int tmp=q.front();q.pop();
ans[++cnt]=tmp; for(int j=;j<=n;j++){
if(g[tmp][j]){
in[j]--;
if(in[j]==) q.push(j);
}
}
} printf("%d",ans[]);
for(int i=;i<=cnt;i++)
printf(" %d",ans[i]);
printf("\n");
} int main(){
while(scanf("%d %d",&n,&m)!=EOF){
if(n==&&m==) break; memset(g,,sizeof(g));
memset(in,,sizeof(in)); while(m--){
int u,v;
cin>>u>>v;
g[u][v]=;
in[v]++;
}
toposort();
}
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(拓扑排序)
题意:给定优先关系进行拓扑排序. 分析:将入度为0的点加入优先队列,并将与之相连的点入度减1,若又有度数为0的点,继续加入优先队列,依次类推. #pragma comment(linker, &quo ...
- 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
随机推荐
- Recovering unassigned shards on elasticsearch 2.x——副本shard可以设置replica为0在设置回来
Recovering unassigned shards on elasticsearch 2.x 摘自:https://z0z0.me/recovering-unassigned-shards-on ...
- sql server 数据库distinct的用法
Distinct:用来过滤重复记录.往往只用它来返回不重复记录的条数,而不是用它来返回不重记录的所有值.其原因是distinct只有用二重循环查询来解决,而这样对于一个数据量非常大的站来说,无疑是会直 ...
- 10 quick tips for Redis
Redis is hot in the tech community right now. It's come a long way from being a small personal proje ...
- requests 常见方法总结
请求设置:requests.get/post ( url, data={}, params={}, headers={}, timeout=0.01, files={} Session() ...
- django 分组统计遇见的问题
在使用 django 的时候发现了一个坑 例如: In [54]: print(F.objects.all().values("age").annotate(fff=Count(& ...
- docker mysql镜像忽略表名大小写
原文:docker mysql镜像忽略表名大小写 1.安装mysql镜像 docker pull mysql/mysql-server 2.运行mysql docker run --net=host ...
- python中的try...except...finally函数
异常Error 我们在写代码的时候,经常会遇见程序抛出Error无法执行的情况 一般情况下,在Python无法正常处理程序时就会发生一个异常.异常是Python对象,表示一个错误.当Python脚本发 ...
- dos2unix和unix2dos命令使用【转】
dos2unix, unix2dos 用来实现 DOS <=> UNIX text file 转换 aptitude install sysutils 行末: DOS 格式 0d 0a U ...
- securefx连接linux后文件夹中文乱码问题解决
首先在选项中设置字符编码为UTF-8 然后在全局选项中找到Securefx的配置文件 进入到该目录中,选择“Sessions”: 在“Sessions”中找到链接地址的ini文件,并用文本编辑器打开: ...
- Mybaties下的分页功能的实现
jsp页面 <!-- 页码 --> <div class="ipRListNav2"> <a href="zyxx.do?findZyxx& ...