题目链接: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 旅行计划】 题解的更多相关文章

  1. luogu P1137 旅行计划

    题目描述 小明要去一个国家旅游.这个国家有N个城市,编号为1-N,并且有M条道路连接着,小明准备从其中一个城市出发,并只往东走到城市i停止. 所以他就需要选择最先到达的城市,并制定一条路线以城市i为终 ...

  2. Luogu P1137 旅行计划 【拓扑排序+Dp】By cellur925

    题目传送门 由于满足游览先后顺序从西到东的性质,我们很自然的想到用拓扑排序处理出一个合理的游览顺序. 然鹅,之后呢? 事实上,拓扑排序常与Dp相结合,解决后效性.我们就可以在每次拓扑入队的时候更新答案 ...

  3. 洛谷P1137 旅行计划

    P1137 旅行计划 题目描述 小明要去一个国家旅游.这个国家有N个城市,编号为1-N,并且有M条道路连接着,小明准备从其中一个城市出发,并只往东走到城市i停止. 所以他就需要选择最先到达的城市,并制 ...

  4. 洛谷 P1137 旅行计划

    旅行计划 待证明这样dp的正确性. #include <iostream> #include <cstdio> #include <cstring> #includ ...

  5. csps模拟83最大异或和简单的括号序列旅行计划题解

    题面:https://www.cnblogs.com/Juve/articles/11733280.html 最大异或和: 简单博弈,小Q一定不会输,如果异或和为0,则平局,因为无论小Q如何拿,小T都 ...

  6. 【luogu P5022 旅行】 题解

    题目连接:https://www.luogu.org/problemnew/show/P5022 \(NOIP2018 DAY2T1\) 考场上只写了60分,很容易想到当 m = n - 1 时的树的 ...

  7. 洛谷P1137 旅行计划 解题报告(拓扑排序+DP)

    我看了一下其他大佬的题解,大部分都是拓扑排序加上DP.那么我想有的人是不明白为什么这么做的,拓扑排序有什么性质使得可以DP呢?下面我就提一下. 对一个有向无环图(Directed Acyclic Gr ...

  8. 洛谷——P1137 旅行计划

    https://www.luogu.org/problem/show?pid=1137 题目描述 小明要去一个国家旅游.这个国家有N个城市,编号为1-N,并且有M条道路连接着,小明准备从其中一个城市出 ...

  9. P1137 旅行计划-----洛谷

    用了STL,其他的没什么,赤裸裸的拓扑排序 下面给出代码 #include<cstdio> #include<iostream> #include<vector> ...

随机推荐

  1. SEH结构

    首先有几点问题 1.在后文中看到的PE的节中的配置信息表Load configuration是对SEH回调函数的注册,那么Exception Table是加载的什么信息. 2.什么时候走进系统异常,什 ...

  2. centos6.5 源码编译 mysql5.6.21

    1.yum安装各个依赖包 [root@WebServer ~]# yum -y install gcc gcc-devel gcc-c++ gcc-c++-devel autoconf* automa ...

  3. Java入门系列-06-运算符

    这篇文章为你搞懂2个问题 java 中的常用运算符有哪些?如何使用? 这些运算符的运算优先级是怎样的? 算数运算符 明显是做数学运算的,包括以下符号: + 加法运算 敲一敲: public class ...

  4. C#程序执行时间

    Stopwatch类 using System.Diagnostics; static void Main(string[] args) { Stopwatch stopWatch = new Sto ...

  5. Java Web基础——Action+Service +Dao三层的功能划分 (转载)

    原文:https://blog.csdn.net/inter_peng/article/details/41021727 仅供自己学习使用: 1. Action/Service/DAO简介: Acti ...

  6. 转载:解决IE下a标签会触发window.onbeforeunload的问题

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...

  7. sass的安装

    sass的安装 1.当ruby安装完成后,在自己电脑开始菜单下面找到ruby的控制台"cmd"; 2.然后直接在命令行中输入 gem install sass 3.在安装过程中 可 ...

  8. node:fs-extra模块

    var fs = require('fs-extra'); //复制 并会覆盖已有文件 fs.copy('./demo/index.html','./demo/index2.html' ,(err) ...

  9. angular怎么样注销事件

    angular怎么样注销事件 $scope.$on("$destroy", function() { //清除配置,不然scroll会重复请求 }) 在Controller中监听$ ...

  10. 隐藏 google 地图 Logo 隐藏 百度 地图 Logo

    Google 地图 var isFirstLoad=true; //地图瓦片加载完成之后的回调 google.maps.event.addListener(map, 'tilesloaded', fu ...