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. PhoneGap_百度百科

    PhoneGap编辑 PhoneGap是一个用基于HTML,CSS和JavaScript的,创建移动跨平台移动应用程序的 快速开发平台.它使开发者能够利用iPhone,Android,Palm,Sym ...

  2. lstm作的第一首诗,纪念

    黄獐春风见破黛,十道奇昌犹自劳. 开领秦都色偏早,未知长柳是来恩. 争时欲下花木湿,早打红筵枝上香. 酣质矫胶麦已暮,丝窗瑞佩满含龙. 感觉有点意思哈,花木对应枝上,还有点对仗的意味. 于是接着又弄了 ...

  3. [c#菜鸟]lambda表达式

    what 一.定义 Lambda 表达式是一种可用于创建 委托 或 表达式目录树 类型的 匿名函数 .通过使用 lambda 表达式,可以写入可作为参数传递或作为函数调用值返回的本地函数.(微软) 理 ...

  4. iOS UI08_TableView界面传值

    实现两个界面之间内容的传递 // // MainViewController.m // UI08_TableView界面传值 // // Created by dllo on 15/8/7. // C ...

  5. react className 有多个值时的处理 / react 样式使用 百分比(%) 报错

    1.react className 有多个值时的处理 <fieldset className={`${styles.formFieldset} ${styles.formItem}`}> ...

  6. QC3.0快充技术详解

    QC3.0 智能手机的电池容量愈来愈大,除了省电能力外,充电速度更成为用户愈来愈重视的特点.高通(Qualcomm)的 Quick Charge 快充技术已成为业界的典范之一,继 Quick Char ...

  7. javascript 高级编程系列 - 函数

    一.函数创建 1. 函数声明 (出现在全局作用域,或局部作用域) function add (a, b) { return a + b; } function add(a, b) { return a ...

  8. css-装饰

    css -在标签上设置style样式 background-color:#2356a1 height:48px -编写样式方法 1.标签的style属性 2.鞋子head里面,style标签中编写 - ...

  9. C# 之 集合ArrayList

    .NET Framework提供了用于数据存储和检索的专用类,这些类统称集合. 这些类提供对堆栈.队列.列表和哈希表的支持.大多数集合类实现系统的接口.以下我们主要来讲一下ArrayList.     ...

  10. SAP-ABAP系列 第二篇SAP ABAP开发基础

    第二章SAP ABAP开发基础 1.ABAP数据类型及定义 ABAP程序中共包含8种基本数据类型定义, 类型名称 描述 属性 C Character Text (字符类型) 默认长度=1,默认值 = ...