Problem E. Pashmak and Graph

时间限制 1000 ms   内存限制 256 MB

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.

输入数据

The first line contains two integers n, m(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: ui, vi, wi(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.

输出数据

Print a single integer — the answer to the problem.

样例

样例说明

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

In the second sample the maximum trail is 3768c0b932f06c7909b3274ec2948856eebd3afd.png.

In the third sample the maximum trail is d00ae6213d8848614e992eec0c4a5dbeac879e69.png.

提交

请先 登录

© 2025 FAQs Contact About