【luogu P1137 旅行计划】 题解
题目链接:https://www.luogu.org/problemnew/show/P1137
topsort + DP
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 100001;
struct edge{
int next, from, to;
}e[maxn<<2];
int head[maxn], cnt, indrg[maxn], f[maxn], n, m;
queue<int> q;
void add(int u, int v)
{
e[++cnt].to = v;
e[cnt].next = head[u];
head[u] = cnt;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i = 1; i <= m; i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
indrg[v]++;
}
for(int i = 1; i <= n; i++)
if(indrg[i] == 0)
{
f[i] = 1;
q.push(i);
}
while(!q.empty())
{
int now = q.front(); q.pop();
for(int i = head[now]; i; i = e[i].next)
{
f[e[i].to] = max(f[e[i].to], f[now]+1);
if(--indrg[e[i].to] == 0) q.push(e[i].to);
}
}
for(int i = 1; i <= n; i++)
printf("%d\n",f[i]);
return 0;
}
【luogu P1137 旅行计划】 题解的更多相关文章
- luogu P1137 旅行计划
题目描述 小明要去一个国家旅游.这个国家有N个城市,编号为1-N,并且有M条道路连接着,小明准备从其中一个城市出发,并只往东走到城市i停止. 所以他就需要选择最先到达的城市,并制定一条路线以城市i为终 ...
- Luogu P1137 旅行计划 【拓扑排序+Dp】By cellur925
题目传送门 由于满足游览先后顺序从西到东的性质,我们很自然的想到用拓扑排序处理出一个合理的游览顺序. 然鹅,之后呢? 事实上,拓扑排序常与Dp相结合,解决后效性.我们就可以在每次拓扑入队的时候更新答案 ...
- 洛谷P1137 旅行计划
P1137 旅行计划 题目描述 小明要去一个国家旅游.这个国家有N个城市,编号为1-N,并且有M条道路连接着,小明准备从其中一个城市出发,并只往东走到城市i停止. 所以他就需要选择最先到达的城市,并制 ...
- 洛谷 P1137 旅行计划
旅行计划 待证明这样dp的正确性. #include <iostream> #include <cstdio> #include <cstring> #includ ...
- csps模拟83最大异或和简单的括号序列旅行计划题解
题面:https://www.cnblogs.com/Juve/articles/11733280.html 最大异或和: 简单博弈,小Q一定不会输,如果异或和为0,则平局,因为无论小Q如何拿,小T都 ...
- 【luogu P5022 旅行】 题解
题目连接:https://www.luogu.org/problemnew/show/P5022 \(NOIP2018 DAY2T1\) 考场上只写了60分,很容易想到当 m = n - 1 时的树的 ...
- 洛谷P1137 旅行计划 解题报告(拓扑排序+DP)
我看了一下其他大佬的题解,大部分都是拓扑排序加上DP.那么我想有的人是不明白为什么这么做的,拓扑排序有什么性质使得可以DP呢?下面我就提一下. 对一个有向无环图(Directed Acyclic Gr ...
- 洛谷——P1137 旅行计划
https://www.luogu.org/problem/show?pid=1137 题目描述 小明要去一个国家旅游.这个国家有N个城市,编号为1-N,并且有M条道路连接着,小明准备从其中一个城市出 ...
- P1137 旅行计划-----洛谷
用了STL,其他的没什么,赤裸裸的拓扑排序 下面给出代码 #include<cstdio> #include<iostream> #include<vector> ...
随机推荐
- HashMap的结构算法及代码分析
HashMap算是日常开发中最长用的类之一了,我们应该了解它的结构跟算法: 参考文章: http://blog.csdn.net/vking_wang/article/details/14166593 ...
- POJ 2570 Fiber Network
Description Several startup companies have decided to build a better Internet, called the "Fibe ...
- TCP-Java--图谱
- Coursera 机器学习 第9章(上) Anomaly Detection 学习笔记
9 Anomaly Detection9.1 Density Estimation9.1.1 Problem Motivation异常检测(Density Estimation)是机器学习常见的应用, ...
- JS实现多少小时前,多少天前...
最近需要实现题目的功能,因为我的时间戳是PHP生成的,所以转换JS时间戳需要乘1000,废话不多说,看下面的代码把! 大家可以判断一下传进来的值是否为数值型,还有判断是否比当前的时间戳大!可以根据结果 ...
- 白话SpringCloud | 第七章:分布式配置中心的使用
前言 介绍完服务的容错保护处理,接下来我们来了解下关于分布式配置中心的相关知识和使用.众所周知,随着项目的越来越多,日益庞大,每个子项目都会伴随着不同的配置项,于此也就多了很多的配置文件.倘若某些配置 ...
- AtCoder Grand Contest 023 C - Painting Machines
Description 一个长度为 \(n\) 的序列,初始都为 \(0\),你需要求出一个长度为 \(n-1\) 的排列 \(P\), 按照 \(1\) 到 \(n\) 的顺序,每次把 \(P_i\ ...
- Unity手册-Unity概述
Unity概述 Unity是一个强大引擎,他自带的多种工具可以满足你多种需求. 这种编辑器是直观的可定制的,让你的工作更大的自由. 原文 Unity is a powerful engine with ...
- 6、图标:icon
1.图标 /* ---html----*/ <ion-content text-center class="icons-basic-page"> <ion-row ...
- MVC5 model常见的写法
1.数据库表中为ID的字段 [Key] //关键字 [Required] //不为空 [Display(Name = "ID")] public int id { get; set ...