UVA-10305 Ordering Tasks (拓扑排序)
题目大意:给出n个点,m条关系,按关系的从小到大排序。
题目分析:拓扑排序的模板题,套模板。
kahn算法:
伪代码:
Kahn算法:
摘一段维基百科上关于Kahn算法的伪码描述:
L← Empty list that will contain the sorted elements
S ← Set of all nodes with no incoming edges
while S is non-empty
do
remove a node n from S
insert n into L
foreach node m with an edge
e from nto m do
remove edge e from thegraph
ifm has no other incoming edges
then
insert m into S
if graph has edges
then
return error (graph has at least onecycle)
else
return L (a topologically sortedorder)
维护一个入度为0的点的集合S,一个初始为空的集合L,L存放排好序的序列。将集合S中的一个点加入集合L后,在S中删除该点并破坏所有从该点出发的边,若被破坏的边的另一端点的入度为0,则加入S,一直处理到S为空。若仍有边存在,则存在环路,反之,集合L中的元素便是按拓扑序排放的。时间复杂度为O(E+V)。
代码如下:
# include<iostream>
# include<cstdio>
# include<queue>
# include<vector>
# include<cstring>
# include<algorithm>
using namespace std; int mp[105][105],n,m,d[105];
vector<int>l;
queue<int>q; int judge(int u)
{
int cnt=0;
for(int i=1;i<=n;++i)
if(mp[i][u])
++cnt;
return cnt;
} void solve()
{
l.clear();
while(!q.empty()){
int u=q.front();
q.pop();
l.push_back(u); for(int i=1;i<=n;++i){
if(mp[u][i]){
mp[u][i]=0;
--d[i];
if(d[i]==0)
q.push(i);
}
}
}
for(int i=0;i<n;++i)
printf("%d%c",l[i],(i==n-1)?'\n':' ');
} int main()
{
int a,b;
while(scanf("%d%d",&n,&m)&&(n+m))
{
memset(mp,0,sizeof(mp));
while(m--){
scanf("%d%d",&a,&b);
mp[a][b]=1;
} while(!q.empty())
q.pop();
for(int i=1;i<=n;++i){
d[i]=judge(i);
if(d[i]==0)
q.push(i);
}
solve();
}
return 0;
}
基于dfs的拓扑排序:
同样摘录一段维基百科上的伪码:
L ← Empty list that will contain the sorted nodes
S ← Set of all nodes with no outgoing edges
for each node n in S
do
visit(n)
function visit(node n)
if n has not been visited yet
then
mark n as visited
for each node m with an edgefrom m to ndo
visit(m)
add n to L
维护一个出度为0的点的集合S,一个初始为空的集合L,L存放排好序的序列。对于集合S中的一个点e,先将所有应该排在e前面的点放到集合L之后,再将点e放入集合L。时间复杂度为O(E+V)。
代码如下:
# include<iostream>
# include<cstdio>
# include<queue>
# include<vector>
# include<cstring>
# include<algorithm>
using namespace std; vector<int>l;
queue<int>q;
int n,m,mp[105][105],mark[105]; bool judge(int u)
{
for(int i=1;i<=n;++i)
if(mp[u][i])
return false;
return true;
} void visit(int u)
{
if(!mark[u]){
mark[u]=1;
for(int i=1;i<=n;++i)
if(mp[i][u])
visit(i);
l.push_back(u);
}
} void solve()
{
l.clear();
memset(mark,0,sizeof(mark));
while(!q.empty()){
int u=q.front();
q.pop();
visit(u);
}
for(int i=0;i<n;++i)
printf("%d%c",l[i],(i==n-1)?'\n':' ');
} int main()
{
int a,b;
while(scanf("%d%d",&n,&m)&&(n+m))
{
memset(mp,0,sizeof(mp));
while(m--)
{
scanf("%d%d",&a,&b);
mp[a][b]=1;
} while(!q.empty())
q.pop();
for(int i=1;i<=n;++i){
if(judge(i))
q.push(i);
}
solve();
}
return 0;
}
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
随机推荐
- poj3764 The XOR Longest Path【dfs】【Trie树】
The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10038 Accepted: ...
- ubuntu16.04下用笔记本摄像头和ROS编译运行ORB_SLAM2的单目AR例程
要编译ORB_SLAM2的ROS例程首先需要安装ROS,以及在ROS下安装usb_cam驱动并调用,最后搭建ORB_SLAM2. 1.ROS的安装 我的电脑安装的是ubuntu16.04系统,所以我安 ...
- TC/IP协议簇
TCP/IP: 数据链路层:ARP,RARP网络层: IP,ICMP,IGMP传输层:TCP ,UDP,UGP应用层:Telnet,FTP,SMTP,SNMP. OSI:物理层:EIA/TIA-232 ...
- Python开发【笔记】:探索Python F-strings
F-strings 在python3.6.2版本中,PEP 498 提出一种新型字符串格式化机制,被称为“字符串插值”或者更常见的一种称呼是F-strings(主要因为这种字符串的第一个字母是f) 简 ...
- Python开发【笔记】:sort排序大法
浅谈排序 程序中经常用到排序函数,Python 提供了 sort 和 sorted 函数,一个原地排序,一个返回排序后的新结果 1.参数 函数原型: sort([cmp[, key[, reverse ...
- SQL基础--查询之一--单表查询
SQL基础--查询之一--单表查询
- 【Loadrunner】通过loadrunner录制时候有事件但是白页无法出来登录页怎么办?
loadrunner录制脚本时候有事件但是一直白页怎么办? 解决办法:依次进行下方1.2.方法操作,如果还不行再进行3的操作. 1.勾选下图IE浏览器的Internet选项中“启用第三方浏览器拓展*” ...
- python模块之pyMySql
目录: 安装 使用 一.安装 本模块为python第三方模块,需要单独安装.作用为调用mysql接口执行模块 pip3 install pyMySql 二.使用 1.执行SQL #!/usr/bin/ ...
- [css]单/多行居中&字体设置
行高和字号 行高 CSS中,所有的行,都有行高.盒模型的padding,绝对不是直接作用在文字上的,而是作用在"行"上的. line-height: 40px; 文字,是在自己的行 ...
- 3.6 Templates -- Binding Element Class Names(绑定元素类名)
1. 一个HTML元素的class属性可以像其他属性一样被绑定: <div class={{priority}}> Warning! </div> 生成的HTML <di ...