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
随机推荐
- Oracle下Update语句
Update更新 格式: update table _name(表名) set col_name(列名) =expr(表达式) where conti(条件) 案例1: update 语法可以用新值 ...
- 朴素贝叶斯算法的python实现 -- 机器学习实战
import numpy as np import re #词表到向量的转换函数 def loadDataSet(): postingList = [['my', 'dog', 'has', 'fle ...
- 为什么 要弄清楚 mysql int(5) int(11) bigint 自建mysql主键id python random 科学计数法
场景: 有1.2亿条问答数据,相同问题的不同答案为不同条的数据,且该表数据逐日递增: 第三方需求(不合理): 将问题.答案数据分别放入问题表.答案表: 问题表的主键为整数,在答案表中,每行数据有相应的 ...
- NGINX:sticky模块实现基于cookie的负载均衡
Sticky模块 简述: 之前公司部署了一套网站及时发布系统,架构如下图所示:Nginx做前端代理,发布系统用tomcat运行,一台共享存储,一台数据库服务器:由于网站及时发布系统涉及到了用户登录操作 ...
- arc 和 非arc兼容
1,选择项目中的Targets,选中你所要操作的Target, 2,选Build Phases,在其中Complie Sources中选择需要ARC的文件双击, 并在输入框中输入:-fobjc-arc ...
- 怎么应对 domino文档损坏然后损坏文档别删除导致数据丢失
对于domino 有个机制是同步 ..然后如果文档被损坏之后会通过同步或者压缩 之类的 然后将损坏文档删除 那么这样就有个风险..知识管理文档会被删除. 并且删除了之后管理员如果不仔细看日志的话也不会 ...
- tcp socket http(复制的)
物理层-- 数据链路层-- 网络层-- IP协议 传输层-- TCP协议 会话层-- 表示层和应用层-- ...
- 系统管理命令之who am i
who am i 显示的是实际用户的用户名,即用户登陆的时候的用户ID.此命令相当于who -m. 用Linux的术语来解释就是:(实际用户=uid,即user id.有效用户=euid,即effec ...
- capistranorb
远程服务器自动部署工具 https://capistranorb.com/
- 在Mybatis中使用连表查询的一次实际应用
以前在工作中很少使用多表关联查询,对连表查询的具体作用和使用场景也没有很直观的认识,通过这次在项目中的实际应用,对此有了一定的认识,特记录如下. 关联表介绍: 分别是属性表attr_info.属性值表 ...