You are given a directed graph with n nodes and m edges, with all edges having a certain weight.

There might be multiple edges and self loops, and the graph can also be disconnected.

You need to choose a path (possibly passing through same vertices multiple times) in the graph such that the weights of the edges are in strictly increasing order, and these edges come in the order of input. Among all such paths, you need to find the the path that has the maximum possible number of edges, and report this value.

Please note that the edges picked don't have to be consecutive in the input.

Input

The first line contains two integers n and m (1 ≤ n ≤ 100000,1 ≤ m ≤ 100000) — the number of vertices and edges in the graph, respectively.

m lines follows.

The i-th of these lines contains three space separated integers aibi and wi (1 ≤ ai, bi ≤ n, 0 ≤ wi ≤ 100000), denoting an edge from vertex ai to vertex bi having weight wi

Output

Print one integer in a single line — the maximum number of edges in the path.

Examples

Input
3 3
3 1 3
1 2 1
2 3 2
Output
2
Input
5 5
1 3 2
3 2 3
3 4 5
5 4 0
4 5 8
Output
3

Note

The answer for the first sample input is 2: . Note that you cannot traverse  because edge  appears earlier in the input than the other two edges and hence cannot be picked/traversed after either of the other two edges.

In the second sample, it's optimal to pick 1-st, 3-rd and 5-th edges to get the optimal answer: .

 题意:问树上最长链大小,这条链满足从起点向终点,是按照给出的顺序,而且长度严格递增。

思路:忽略空间时间问题,先假设线段树可以实现。

N棵线段树,每棵开100000个叶子节点,第i棵树的j叶子表示以i节点为尾,最后一条边长度是j的最大链。

合并为主席树。然后DP乱搞。

怎么合并呢? 假设现在新加了一条有向边,(u->v,len),那么把第v棵线段树,长度[1,len-1]的节点都更新node[v][i]=max(node[v][i],node[u][i]+1);

AC in one go!cheers。

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int rt[maxn],cnt;
struct node
{
int l,r,val;
node(){ l=r=val=;}
node(int L,int R,int V):l(L),r(R),val(V){}
}s[maxn*];
int query(int Now,int L,int R,int l,int r)
{
if(l<=L&&r>=R) return s[Now].val;
int res=,Mid=(L+R)>>;
if(l<=Mid) res=max(res,query(s[Now].l,L,Mid,l,r));
if(r>Mid) res=max(res,query(s[Now].r,Mid+,R,l,r));
return res;
}
void update(int &Now,int pre,int L,int R,int pos,int val)
{
Now=++cnt; s[Now]=node(s[pre].l,s[pre].r,max(s[pre].val,val));
if(L==R) return ; int Mid=(L+R)>>;
if(pos<=Mid) update(s[Now].l,s[pre].l,L,Mid,pos,val);
else update(s[Now].r,s[pre].r,Mid+,R,pos,val);
}
int main()
{
int N,M,i,u,v,c,ans=;
scanf("%d%d",&N,&M);
for(i=;i<=M;i++){
scanf("%d%d%d",&u,&v,&c); c+=; //防止线段树越界
int tmp=query(rt[u],,,,c-);
update(rt[v],rt[v],,,c,tmp+);
ans=max(tmp+,ans);
}
printf("%d\n",ans);
return ;
}

CodeForces960F:Pathwalks (主席树+DP)的更多相关文章

  1. 【CodeForces】960 F. Pathwalks 主席树+动态规划

    [题目]F. Pathwalks [题意]给定n个点m条边的有向图,可能不连通有重边有自环.每条边有编号 i 和边权 wi ,求最长的路径(可以经过重复节点)满足编号和边权都严格递增.n,m,wi&l ...

  2. CodeForces - 597C:Subsequences (主席树+DP)

    For the given sequence with n different elements find the number of increasing subsequences with k + ...

  3. CodeForces - 960F Pathwalks —— 主席树(n棵线段树)

    题目链接:https://vjudge.net/problem/CodeForces-960F You are given a directed graph with n nodes and m ed ...

  4. 主席树[可持久化线段树](hdu 2665 Kth number、SP 10628 Count on a tree、ZOJ 2112 Dynamic Rankings、codeforces 813E Army Creation、codeforces960F:Pathwalks )

    在今天三黑(恶意评分刷上去的那种)两紫的智推中,突然出现了P3834 [模板]可持久化线段树 1(主席树)就突然有了不详的预感2333 果然...然后我gg了!被大佬虐了! hdu 2665 Kth ...

  5. SRM12 T2夏令营(分治优化DP+主席树 (已更新NKlogN)/ 线段树优化DP)

     先写出朴素的DP方程f[i][j]=f[k][j-1]+h[k+1][i] {k<i}(h表示[k+1,j]有几个不同的数)  显然时间空间复杂度都无法承受   仔细想想可以发现对于一个点 i ...

  6. Codeforces 833B The Bakery(主席树 + 决策单调性优化DP)

    题目链接 The Bakery 题目大意:目标是把$n$个数分成$k$组,每个组的值为这个组内不同的数的个数,求$k$个组的值的和的最大值. 题目分析: 这道题我的解法可能和大众解法不太一样……我用主 ...

  7. BZOJ 4367 [IOI2014]holiday (决策单调DP+主席树+分治)

    题目大意:略 题目传送门 神题,不写长题解简直是浪费了这道题 贪心 考虑从0节点出发的情况,显然一直往前走不回头才是最优策略 如果起点是在中间某个节点$s$,容易想到,如果既要游览$s$左边的某些景点 ...

  8. Codeforces 960 二进制构造子序列 完全二叉树shift模拟 主席树/MAP DP

    A #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #def ...

  9. LOJ 6435 「PKUSC2018」星际穿越——DP+倍增 / 思路+主席树

    题目:https://loj.ac/problem/6435 题解:https://www.cnblogs.com/HocRiser/p/9166459.html 自己要怎样才能想到怎么做呢…… dp ...

随机推荐

  1. 【jar】JDK将单个的java文件打包为jar包,并引用到项目中使用【MD5加密】

    ==================================================================================================== ...

  2. Android 自定义录音、播放动画View,让你的录音浪起来

    最近公司项目有一个录音的录制和播放动画需求,然后时间是那么紧,那么赶紧开撸. 先看效果图 嗯,然后大致就是这样,按住录音,然后有一个倒计时,最外层一个进度条,还有一个类似模拟声波的动画效果(其实中间的 ...

  3. nginx list directory

    使用 http autoindex 模块列出 目录, 例如 需要将 /var/www 下的 resourcepacks 目录以 http 的方式 暴露         这样设置 nginx       ...

  4. 简化动态MERGE的SQL计算

    MSSQL.ORACLE等数据库支持MERGE语句更新表.但表结构未知时,因为缺乏集合类数据.用存储过程获得表结构再动态拼出SQL很麻烦,代码会有几十行之多:相同原因,用Java等高级语言实现也不简单 ...

  5. c#高级编程笔记----委托

    因为定义委托基本上是定义一个新类,所以可以在定义类的任何相同地方定义委托,也就是说,可以在另一个类的内部定义,也可以在任何类的外部定义,还可以在名称空间中把委托定义为顶层对象.根据定义的可见性,和委托 ...

  6. VS快速生成JSON数据类

    1.复制Json数据 2.

  7. 基于Django做权限控制

    一.权限信息初始化 二.中间件操作 三.自定义标签 补充:数据表设计,源码下载,其它

  8. nodejs-runoob

    是否安装成功nodejs - node -v 是否安装成功npm - npm -v

  9. JavaScript读书笔记(4)-变量、作用域和内存问题

    1.ECMAScript数据类型分为:基本类型值和引用类型值: ECMAScript中所有函数的参数都是按值传递的: 检查对象的类型:varible instanceof constructor Al ...

  10. disabled和readonly

    项目中,有一个input控件,input的值需要通过点击一个javascript链接,从弹出的对话框中所列出的项中选择.而不能从input框中直接输入. 刚开始将input的disabled属性设置为 ...