E. Pashmak and Graph
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Pashmak's homework is a problem about graphs. Although he always tries to do his homework completely, he can't solve this problem. As you know, he's really weak at graph theory; so try to help him in solving the problem.

You are given a weighted directed graph with n vertices and m edges. You need to find a path (perhaps, non-simple) with maximum number of edges, such that the weights of the edges increase along the path. In other words, each edge of the path must have strictly greater weight than the previous edge in the path.

Help Pashmak, print the number of edges in the required path.

Input

The first line contains two integers nm (2 ≤ n ≤ 3·105; 1 ≤ m ≤ min(n·(n - 1), 3·105)). Then, m lines follows. The i-th line contains three space separated integers: uiviwi (1 ≤ ui, vi ≤ n; 1 ≤ wi ≤ 105) which indicates that there's a directed edge with weight wi from vertex ui to vertex vi.

It's guaranteed that the graph doesn't contain self-loops and multiple edges.

Output

Print a single integer — the answer to the problem.

Examples
input
3 3
1 2 1
2 3 1
3 1 1
output
1
input
3 3
1 2 1
2 3 2
3 1 3
output
3
input
6 7
1 2 1
3 2 5
2 4 2
2 5 2
2 6 9
5 4 3
4 3 4
output
6
Note

In the first sample the maximum trail can be any of this trails: .

In the second sample the maximum trail is .

In the third sample the maximum trail is .

/*
显而易见的贪心——小的边排前面……所以,先按照边长排序,然后dp,以当前边的尾节点为路径的结尾,然后一个边的首段点为u,尾端点为v,
裸地转移就是dp[v]=max(dp[v],dp[u]+1)
注意边相等要单独处理,然后就AC了……
*/
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
const int N=3e5+;
struct edge{int x,y,z;}e[N<<];
int n,m;int f[N],g[N];
bool operator <(const edge &a,const edge &b){
return a.z<b.z;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++) scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].z);
sort(e+,e+m+);
for(int j,i=;i<=m;i++){
for(j=i;e[j].z==e[i].z;j++) g[e[j].y]=max(g[e[j].y],f[e[j].x]+);
for(j=i;e[j].z==e[i].z;j++) f[e[j].y]=g[e[j].y];
i=j-;
}
printf("%d\n",*max_element(f+,f+n+));
return ;
}

CodeForces - 459E Pashmak and Graph[贪心优化dp]的更多相关文章

  1. Codeforces 459E Pashmak and Graph(dp+贪婪)

    题目链接:Codeforces 459E Pashmak and Graph 题目大意:给定一张有向图,每条边有它的权值,要求选定一条路线,保证所经过的边权值严格递增,输出最长路径. 解题思路:将边依 ...

  2. Codeforces 459E Pashmak and Graph:dp + 贪心

    题目链接:http://codeforces.com/problemset/problem/459/E 题意: 给你一个有向图,每条边有边权. 让你找出一条路径,使得这条路径上的边权严格递增. 问你这 ...

  3. Codeforces 459E Pashmak and Graph

    http://www.codeforces.com/problemset/problem/459/E 题意: 给出n个点,m条边的有向图,每个边有边权,求一条最长的边权上升的路径的长度. 思路:用f存 ...

  4. 【贪心优化dp决策】bzoj1571: [Usaco2009 Open]滑雪课Ski

    还有贪心优化dp决策的操作…… Description Farmer John 想要带着 Bessie 一起在科罗拉多州一起滑雪.很不幸,Bessie滑雪技术并不精湛. Bessie了解到,在滑雪场里 ...

  5. CodeForces 311 B Cats Transport 斜率优化DP

    题目传送门 题意:现在有n座山峰,现在 i-1 与 i 座山峰有 di长的路,现在有m个宠物, 分别在hi座山峰,第ti秒之后可以被带走,现在有p个人,每个人会从1号山峰走到n号山峰,速度1m/s.现 ...

  6. 『数组的最大代价 贪心优化DP』

    数组的最大代价(51nod 1270) Description 数组A包含N个元素A1, A2......AN.数组B包含N个元素B1, B2......BN.并且数组A中的每一个元素Ai,都满足1 ...

  7. Codeforces 570E - Pig and Palindromes - [滚动优化DP]

    题目链接:https://codeforces.com/problemset/problem/570/E 题意: 给出 $n \times m$ 的网格,每一格上有一个小写字母,现在从 $(1,1)$ ...

  8. Codeforces 643C Levels and Regions 斜率优化dp

    Levels and Regions 把dp方程列出来, 把所有东西拆成前缀的形式, 就能看出可以斜率优化啦. #include<bits/stdc++.h> #define LL lon ...

  9. CodeForces 834D The Bakery(线段树优化DP)

    Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredient ...

随机推荐

  1. iOS边练边学--UITabBarController的简单使用

    一.UITabBarController的使用步骤 初始化UITabBarController 设置UIWindow的rootViewController为UITabBarController 根据具 ...

  2. WCF客户端获取服务器返回数据报错

    错误信息:An error occurred while receiving the HTTP response to http://127.0.0.1/SIHIS/Infection/PubExec ...

  3. .net垃圾回收

    垃圾回收器(gc)用来在.NET中进行内存管理,特别是它可以恢复正在运行的应用程序需要的内存. .NET运行库采用的方法是垃圾回收器,这是一个程序,其目的是清理内存.方法是所有动态请求的内存都分配到堆 ...

  4. JS实现点击表头表格自动排序(含数字、字符串、日期)

    这篇文章主要介绍了利用JS如何实现点击表头后表格自动排序,其中包含数字排序.字符串排序以及日期格式的排序,文中给出了完整的示例代码,并做了注释,相信大家都能看懂,感兴趣的朋友们一起来看看吧. < ...

  5. TCP/IP和Socket的关系

    要写网络程序就必须用Socket,这是程序员都知道的.而且,面试的时候,我们也会问对方会不会Socket编程?一般来说,很多人都会说,Socket编程基本就是listen,accept以及send,w ...

  6. Checked Exception & Unchecked Exception

    查Spring事务管理时看到一句话: Spring使用声明式事务处理,默认情况下,如果被注解的数据库操作方法中发生了unchecked异常,所有的数据库操作将rollback:如果发生的异常是chec ...

  7. 静态变量加前缀 s_(表示 static)

    静态变量加前缀 s_(表示 static). 例如: void Init(…) { static int s_initValue; // 静态变量 … } #include <iostream& ...

  8. C++ 接口(抽象类)

    C++ 接口(抽象类)接口描述了类的行为和功能,而不需要完成类的特定实现. C++ 接口是使用抽象类来实现的,抽象类与数据抽象互不混淆,数据抽象是一个把实现细节与相关的数据分离开的概念. 如果类中至少 ...

  9. VMWare -- winscp实现windows主机和Ubuntu虚拟机之间文件复制(通过ftp协议)

    我们经常需要将本地的文件上传到远程的Ubuntu 14.04服务器上,或者把远程Ubuntu 14.04服务器上的文件下载到本地,这就需要用到vsftpd来搭建FTP服务,现在介绍一下如何在Ubunt ...

  10. caffe源代码分析--Blob类代码研究

    作者:linger 转自须注明转自:http://blog.csdn.net/lingerlanlan/article/details/24379689 数据成员 shared_ptr<Sync ...