HDU 4857 逃生(拓扑排序)
拓扑排序
一.定义
对一个有向无环图(Directed Acyclic Graph简称DAG)G进行拓扑排序,是将G中所有顶点排成一个线性序列,使得图中任意一对顶点u和v,若<u,v> ∈E(G),则u在线性序列中出现在v之前。
通常,这样的线性序列称为满足拓扑次序(Topological Order)的序列,简称拓扑序列。
注意:
1)只有有向无环图才存在拓扑序列;
2)对于一个DAG,可能存在多个拓扑序列(此题已经规定了数字的优先级,所以答案唯一);
二.拓扑序列算法思想
(1)从有向图中选取一个没有前驱(即入度为0)的顶点,并输出之;
(2)从有向图中删去此顶点以及所有以它为尾的弧;
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include<functional>
#define MAXSIZE 100005
using namespace std; vector<int>G[MAXSIZE];
priority_queue<int ,vector<int>, less<int> > q;
int in[MAXSIZE]; int main()
{
int T,n,m,a,b;
scanf("%d",&T);
while(T--)
{
memset(in,,sizeof(in));
vector<int> ans;
for(int i=;i<MAXSIZE;i++)
G[i].clear();
scanf("%d%d",&n,&m);
while(m--)
{
scanf("%d%d",&a,&b);
in[a]++;
G[b].push_back(a);
}
for(int i=;i<=n;i++)
if(!in[i]) q.push(i);
while(!q.empty())
{
int u=q.top();
ans.push_back(u);
q.pop();
int len=G[u].size();
for(int i=;i<len;i++)
{
int v=G[u][i];
in[v]--;
if(!in[v])
q.push(v);
} }
int len=ans.size();
for(int i=len-;i>=;i--)
printf("%d%c",ans[i],i==?'\n':' ');
}
return ;
}
HDU 4857 逃生(拓扑排序)的更多相关文章
- hdu 4857 逃生 拓扑排序+PQ,剥层分析
pid=4857">hdu4857 逃生 题目是求拓扑排序,但不是依照字典序最小输出,而是要使较小的数排在最前面. 一開始的错误思路:给每一个点确定一个优先级(该点所能到达的最小的点) ...
- hdu 4857 逃生 拓扑排序+逆向建图
逃生 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Descr ...
- hdu 4857 逆向拓扑排序+反向输出
/*一组测试实例 4 4 2 3 1 2 4 */ #include<stdio.h> #include<string.h> #include<queue> usi ...
- HDU.2647 Reward(拓扑排序 TopSort)
HDU.2647 Reward(拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 这道题有一点变化是要求计算最后的金钱数.最少金钱值是888,最少的 ...
- HDU 4857 逃生 (反向拓扑排序 & 容器实现)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4857 逃生 Time Limit: 2000/1000 MS (Java/Others) Mem ...
- 正向与反向拓扑排序的区别(hdu 1285 确定比赛名次和hdu 4857 逃生)
确定比赛名次 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submis ...
- HDU 4857 逃生(反向建边的拓扑排序+贪心思想)
逃生 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...
- HDU 4857 逃生 【拓扑排序+反向建图+优先队列】
逃生 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission ...
- hdu 4857 逃生
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4857 逃生 Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能 ...
随机推荐
- nginx buffered to a temporary 解决
今天开启了nginx的error_log,发现了三个配置问题: 问题一: 2011/07/18 17:04:37 [warn] 2422#0: *171505004 an upstream respo ...
- 114. Flatten Binary Tree to Linked List(M)
. Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. For ...
- (贪心)nyoj448-寻找最大数
题目描述: 请在整数 n 中删除m个数字, 使得余下的数字按原次序组成的新数最大, 比如当n=92081346718538,m=10时,则新的最大数是9888 输入描述: 第一行输入一个正整数T,表示 ...
- SQLMAP UDF提权
SQLMAP UDF提权 1.连接mysql数据打开一个交互shell: sqlmap.py -d mysql://root:root@127.0.0.1:3306/test --sql-s ...
- Redis作为lru缓存作用
当 Redis 作为缓存使用时,当你添加新的数据时,有时候很方便使 Redis 自动回收老的数据.LRU 实际上是被唯一支持的数据移除方法.Redis 的 maxmemory 指令,用于限制内存使用到 ...
- MySQL数据库优化_limit_2
limit豫union一起使用时的优化 cp_order_exit数据行数:142951 cp_order_exit_led数据行数:20876 查询:这条 查询将会把 cp_order_exit中的 ...
- 【.net】The source was not found, but some or all event logs could not be searched
1.案发现场: 注册服务的时候 2.解决方案: 用管理员身份运行CMD,再注册服务: I had to run Command Prompt with Administrator Rights.
- 【英文文档】 Installing Go from source Go语言官方编译指南 2019.02.27
Introduction Go is an open source project, distributed under a BSD-style license. This document expl ...
- OPCServer:使用KEPServer
实际OPCServer使用(450M,中文,破解时间限制):KEPServer V6,百度网盘 ,密码: ykj2 本文为Java实现OPC通信的一部分 KEPServer中文官网 KEPServer ...
- JVM垃圾回收机制与内存回收
暂时转于:https://blog.csdn.net/qq_27035123/article/details/72857739 垃圾回收机制 GC是垃圾回收机制,java中将内存管理交给垃圾回收机制, ...